‎2006 Jun 20 11:39 AM
HI everyone,
I am confusing on using Binary search during read a internal table.
For example , I created a internal table called <b>TKOMV</b>.
some of its content are as following.
KNUMV ,, KPOSN ,, STUNR ,, ZAEHK ,, KAPPL ,, KSCHL ,, KDATU
3174 ,, 000010 ,, 11 ,, 1 ,, V ,, PR00 ,, 20051108
3174 ,, 000010 ,, 915 ,, 1 ,, V ,, MWST ,, 20051226
3174 ,, 000010 ,, 940 ,, 1 ,, V ,, VPRS ,, ,, 20051108
In case, i write the statement
READ TABLE TKOMV WITH KEY KNUMV = GT_VBAK-KNUMV
KPOSN = GT_VBAP-POSNR
KSCHL = 'PR00'
BINARY SEARCH.
if GT_VBAK-KNUMV = 3174 , GT_VBAP-POSNR = 000010.
<b>sy-subrc will return 4!!!</b>
However, If i write the statement without Binary search.
READ TABLE TKOMV WITH KEY KNUMV = GT_VBAK-KNUMV
KPOSN = GT_VBAP-POSNR
KSCHL = 'PR00'.
if GT_VBAK-KNUMV = 3174 , GT_VBAP-POSNR = 000010.
<b>sy-subrc will return 0</b> and find the appropriate record.
Why???
I know read table with binary search will consume less repsonse time.
But why it cannot find the appropriate record under this situation??
Thx in advance for all of reply.<u></u>
‎2006 Jun 20 11:44 AM
Hi Boris,
BINAY SEARCH in READ will definitely increase performance.But <b>you need to SORT the internal table with all fields specifying in your READ statement</b>.
Use the following statement before READ statement with BINARY SEARCH.
<b>SORT TKOMV BY KNUMV KPOSN KSCHL.</b>
READ TABLE TKOMV WITH KEY KNUMV = GT_VBAK-KNUMV
KPOSN = GT_VBAP-POSNR
KSCHL = 'PR00'
BINARY SEARCH.
Thanks,
Vinay
‎2006 Jun 20 11:44 AM
Hi Boris,
BINAY SEARCH in READ will definitely increase performance.But <b>you need to SORT the internal table with all fields specifying in your READ statement</b>.
Use the following statement before READ statement with BINARY SEARCH.
<b>SORT TKOMV BY KNUMV KPOSN KSCHL.</b>
READ TABLE TKOMV WITH KEY KNUMV = GT_VBAK-KNUMV
KPOSN = GT_VBAP-POSNR
KSCHL = 'PR00'
BINARY SEARCH.
Thanks,
Vinay
‎2022 Apr 19 9:22 PM
I have same query but when i am using binary search i am getting third record of the internal table and without binary search i am getting first record
can you please explain why
‎2022 Apr 20 11:17 AM
ravikumar.billa1
The answer is simple - you've not sorted your table correctly.
Use a SORTED table or a HASHED table. BINARY SEARCH is largely obsolete. This question was originally asked in 2006 and BINARY SEARCH was already obsolete then.
‎2006 Jun 20 11:44 AM
check if your table is sorted or not.
for binary search table must be sorted.
Regards,
Wasim Ahmed
‎2006 Jun 20 11:45 AM
‎2006 Jun 20 11:44 AM
‎2006 Jun 20 11:45 AM
Hi,
<b>Before using Binary search Sort the internal Table.
SORT TABLE TKOMV BY KNUMV KPOSN KSCHL.</b>
READ TABLE TKOMV WITH KEY KNUMV = GT_VBAK-KNUMV
KPOSN = GT_VBAP-POSNR
KSCHL = 'PR00'
BINARY SEARCH.
May be it will be helpful.
reward points if helpful.
- *****
‎2006 Jun 20 11:46 AM
Hi Boris,
I faced the same problem.
when you use BINARY SEARCH the table you are reading should be sorted only then it will return the correct record
Sameena
Message was edited by: sameena attarwala
‎2006 Jun 20 11:46 AM
Hi Boris,
For using binary search, the internal table needs to be sorted. So, first sort the table and then use binary search.
Regards,
Richa.
‎2006 Jun 20 11:51 AM
Hi ,
Sort the internal table before using the read statement.
Regards
beena
‎2006 Jun 20 11:53 AM
Hi Boris
Just as guys have said, you have to sort your internal table before reading it:
DATA: BEGIN OF ITAB OCCURS 0,
FIELD1,
FIELD2,
.......
END OF ITAB.
...........
SORT ITAB BY FIELD1 FIELD2.
READ TABLE ITAB WITH KEY FIELD1 =
FIELD2 =
BINARY SEARCH.
Anyway the performances improve only if the ITAB has many hits.
Max