10-31-2007 11:08 AM
10-31-2007 11:11 AM
before using BINARy SEARCH the internal table must be SORTed on the fields which we are using in READ statement.
Now binary search looks the middle record in the internal table. if the KEY is less than the middle record then it will take first half entries for further search and ingnores second half. otherwise it will take second half for search and ignores first half.
Next repeats the same thing for the remaining entries.
SO every time it will ignore half records of internal table. that's why it is fast.
10-31-2007 11:29 AM
Please note that use binary search only if you are sure that your internal table will be filled with more rows..for less rows it will decrease performance
10-31-2007 12:17 PM
two more comments:
You must take care that the table is sorted, the binary search will also work when the table is not sorted. But then the binary search will not work correctly.
The binary search itself never decreases the performance. However, the sort is costly but depends on the size of table. It is the number of executions of the read which must be checked.
One sort is much more expensive than a few reads. So you must use the reads several times to justify the sort, at least 10 times.
Siegfried
10-31-2007 12:55 PM
The binary search uses the binary search algorithm to search for the target record. This algorithm is not specific to SAP. It is a commonly used algorithm in various types of Software development.
To know more about it please read the following links:
http://en.wikipedia.org/wiki/Binary_search
http://whatis.techtarget.com/definition/0,,sid9_gci349425,00.html
From an ABAP point of view you need to take care of the following.
1) Only standard tables can be used to perform a read using BINARY SEARCH.
2) Use the key work BINARY SEARCH in your read statement.
Read without BINARY SEARCH
READ TABLE t_vbak WITH KEY vbeln = <sales order number>.
The same read statement with BINARY SEARCH
READ TABLE t_vbak WITH KEY vbeln = <sales order number> BINARY SEARCH.
3) Last but probably the most important is that you need to SORT the table on the key fields otherwise BINARY SEARCH will not give the correct results. Remember the BINARY SEARCH is designed only on a sorted array.
The complete code for the above read statement would be
SORT t_vbak BY vbeln.
READ TABLE t_vbak WITH KEY vbeln = <sales order number> BINARY SEARCH.