Wednesday 10 August 2011

Optimization Techniques in Natural & Adabas - MULTI-FETCH


It’s probably easy to mention what all you need to optimize in a process, but there is a ground rule - it’s upon the developer to really understand what the current program does/want (Know your DATA) and that’s what really opens the Pandora’s box. No more bragging here it goes…

Natural (4.2) and upwards

MULTI-FETCH

Well its MULTI-FETCH factor that I feel is the most powerful of the optimization techniques that one could use in Natural programs and yes one of my favorites available in Natural 4 and upwards only – easy and bang for the buck. It can be used by any of the database access statements like READ, HISTOGRAM OR FIND. What does it do – well that’s the fun part about it, it can be used to fetch multiple records from the database using a single command!! i.e. depending on the multi-fetch factor you mention in the database call.

Well don’t jump your guns just yet – it doesn’t mean if you have 2 million records and you want to fetch all of it, you can just mention a READ MULTI-FETCH 2000000 EMPLOYEE-TABLE – It won’t work and your process will run slower as opposed to what you might want. Although most people mention there is no limit on the MULTI-FETCH factor, I might disagree to the fact. By thumb rule it should not be more than the divisible factor (32K/View-size) with a strict NO usage in UPDATE/STORE/DELETE programs or updation loops - i.e. in case your updation is outside a processing loop. If you use correctly, the process will run faster, use lesser CPU and makes lesser Adabas calls.

Some ground rules of its usage:
  • Multi-Fetch works on READ LOGICAL, HISTOGRAM & FIND statements
  • Multi-Fetch buffer is released when loop ends or when the specific condition is met for the processing loop. E.g.
READ MULTI-FETCH 15 EMPLOYEES WITH CITY = ‘VEGAS’
   IF CITY NE ‘VEGAS’
      ESCAPE BOTTOM
   END-IF
END-READ
In the above code, if we have only 10 records for Vegas as City, we will end up reading 15 records into the multi-fetch buffer including 10 records of City as Vegas and 5 records of what is left in the table instead of the intended 10. The program will sequentially read from buffer one at a time and pass it to the program without issuing a command to the database. The extra records wont effect performance because the Multi-fetch option has already read ahead and given you all the records and placed in buffer. We will look into this in detail later.
  • Do not arbitrarily increase Multi-fetch factor, try calculating it to find the optimized factor value as detailed above.




Now let us look at how this is achievable by an example below. Employees table has 10 records with City as VEGAS. Our usual program looks like this to fetch only records where City is Vegas.

Program without Multi-fetch













Running DBLOG for it, we get the result as below:











Whoa. Looking at the above log, it’s clear that the process made 17 calls to read our eligible 10 records. That’s 11 calls to get the 10 recordsyes 11, because it has to read the 11th record to confirm that the ESCAPE BOTTOM condition has been achieved .I purposefully didn’t print the cpu time for it, because multi-fetch doesn’t show much of a difference when intended result set is only a handful. The result is as below:




Now let us see how it changes when we add a multi-fetch factor
Program with Multi-fetch factor



Now, let us run the DBLOG and see how many call it makes.



Hmm, you might say what difference did it make? It still made 17 calls ...or did it? Check the column marked in red. Actually it didn’t make 11 calls, it just made 2 calls to get the 11 records. The first Multi-fetch call issued by “MA” says it fetched 10 records and put in buffer, the “<A” says it was fetched from buffer. The following “MA” went and fetched 10 records and then subsequently got rejected because the condition of “VEGAS” got dissatisfied.  

So that leaves us with a problem there, we need to change the program such that we read exactly the 10 records we intend to read, so let us see how we will achieve that. Oh no - not just reading exactly 10 records, but also reduce the count of Adabas calls (well ehm, use MULTI-FETCH). With that I introduce a new option “STARTING FROM descriptor TO descriptor value or /super-descriptor with high value”.

Program with MULTI-FETCH & FROM-TO option



Running DBLOG and seeing how many calls, we see as below:



So, yes we did reduce 1 call. Yippee.  So, is that...Well I hope not? Definitely all you Natural-ADABAS lovers might have recognized the issue here. I have used READ throughout not FIND! Well we should be checking that out as well. If the number of records fetched is relatively lower, we could definitely go for FIND and we have FIND MULTI-FETCH option

Program with MULTI-FETCH option & using FIND



Let us run the DBLOG and see what we get for the above programs:



Well finally we did it. The number of Adabas count is down to 15 – or rather 10 calls equate to 1 call to the database identified by “MN” & “<N”. So we are now down to 6 calls by using FIND MULTI-FETCH option from 17 calls of a the first program we wrote. Tada – we hit goldmine, didn’t we.

Note: The MULTI-FETCH factor is 32K/View-size per program. You will have to factor the other views also into the same 32K. So subtract that from the 32K before you calculate the multi-fetch factor for the view you intend to use multi-fetch – this is kind of the tolerance factor which might end up increasing/decreasing the run time of your program. This doesn’t mean a program cannot have multiple multi-fetch factors – it’s upon the developer to find what’s apt to make the process faster, where most of the records are fetched form and how fast your process needs to run. And yeah if you follow process correctly you might be in for the rude shock - You might end up creating a really optimized program.


5 comments:

  1. Hi Ashok,
    Really informative and useful technique..pls share more of such!!

    Himanshu Gupta

    ReplyDelete
    Replies
    1. Naturally Adabas: Optimization Techniques In Natural And Adabas - Multi-Fetch >>>>> Download Now

      >>>>> Download Full

      Naturally Adabas: Optimization Techniques In Natural And Adabas - Multi-Fetch >>>>> Download LINK

      >>>>> Download Now

      Naturally Adabas: Optimization Techniques In Natural And Adabas - Multi-Fetch >>>>> Download Full

      >>>>> Download LINK sQ

      Delete
  2. Your post give me some good ideas, it's really amazing. Keep it up!
    database optimization techniques

    ReplyDelete
  3. Hello thank for de information, 2 questions, first how to calculate the optimun number of multifech y % of record length an d second wich is the problem for use in "UPDATE/STORE/DELETE programs", thanks for your information.
    Repeat is better than for ??
    Thanks newsly

    ReplyDelete
  4. Naturally Adabas: Optimization Techniques In Natural And Adabas - Multi-Fetch >>>>> Download Now

    >>>>> Download Full

    Naturally Adabas: Optimization Techniques In Natural And Adabas - Multi-Fetch >>>>> Download LINK

    >>>>> Download Now

    Naturally Adabas: Optimization Techniques In Natural And Adabas - Multi-Fetch >>>>> Download Full

    >>>>> Download LINK

    ReplyDelete