Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

The difference from read table with & without Binary search

Former Member
0 Likes
3,304

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>

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,212

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

11 REPLIES 11
Read only

Former Member
0 Likes
2,213

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

Read only

0 Likes
2,212

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

Read only

matt
Active Contributor
0 Likes
2,212

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.

Read only

dani_mn
Active Contributor
0 Likes
2,212

check if your table is sorted or not.

for binary search table must be sorted.

Regards,

Wasim Ahmed

Read only

Former Member
0 Likes
2,212

Before read table, sort table TKOMV by KNUMV KPOSN KSCHL.

Read only

Former Member
0 Likes
2,212

Hi,

binary search is one of the fastest way to find the record exists in the internal table.

TO use BINARY SEARCH, you have to <b>SORT</b> the <b>internal table</b> in ASCENDING/DESCENDING ORDER. Then only you will get the exact results.

For more detail you can refer to below threads:

Rgds,

Read only

Former Member
0 Likes
2,212

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.

- *****

Read only

Former Member
0 Likes
2,212

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

Read only

Former Member
0 Likes
2,212

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.

Read only

Former Member
0 Likes
2,212

Hi ,

Sort the internal table before using the read statement.

Regards

beena

Read only

Former Member
0 Likes
2,212

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