Tuesday, June 14, 2011

BINARY SEARCH with disadvantage!

In whatever means possible, always use binary search in your ABAP program as what SAP AG suggests. But, there is one thing that you need to look at.


At the back of our mind, we are always reminded that running READ TABLE with and without BINARY SEARCH should always return the same result except for the good performance the latter brings. But, there's a problem underlying binary search during runtime esp. when you are appending or changing keys in your table being read. In my case, I tried running WITH and WITHOUT BINARY search and resulting in a different result. Like the other returns sy-subrc <> 0 and vice-versa. Most of us would say that this is impossible! What happened is that, using binary search will change the sorting of your internal table during runtime when appending data thus, rearranging the orig sort.


Here are some other tips in using BINARY SEARCH.



Syntax: 


SORT itab BY k1 k2 ....


LOOP AT itab2....


READ TABLE itab INTO wa_itab
WITH KEY k1 = itab2-k1
                    k2 = itab2-k2
                     ....
BINARY SEARCH.


APPEND itab <------------ DON't do this!!


.....
ENDLOOP.


N.B.


1. Don't forget to SORT the key fields in order.
2. Arrange the key fields in WITH KEY same with sorting.
3. Put SORT right before READ with BINARY SEARCH or before the loop stament to optimize
4. Find ways not to append entry to your itab being used in read with binary within loop.  
    Suggest to use temporary table.

 

No comments:

Post a Comment