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

Read Table itab using key .....with Binary Search?

Former Member
0 Likes
2,093

What exactly does adding the "Binary Search" syntax do?

1 ACCEPTED SOLUTION
Read only

ferry_lianto
Active Contributor
0 Likes
1,062

Hi Tom,

The binary search algorithm helps faster search of a value in an internal table. It is advisable to sort the internal table before doing a binary search. Binary search repeatedly divides the search interval in half.

If the value to be searched is less than the item in the middle of the interval, the search is narrowed to the lower half, otherwise the search is narrowed to the upper half.

Not Recommended

Read table int_fligh with key airln = 'LF'.

Recommended

Read table int_fligh with key airln = 'LF' binary search.

Hope this will help.

Regards,

Ferry Lianto

7 REPLIES 7
Read only

Former Member
0 Likes
1,062

Basically it uses the binary search logic to find the exact match. In short, it uses the binary search logic to find the exact record from the internal table.

Message was edited by: Anurag Bankley

Read only

ferry_lianto
Active Contributor
0 Likes
1,063

Hi Tom,

The binary search algorithm helps faster search of a value in an internal table. It is advisable to sort the internal table before doing a binary search. Binary search repeatedly divides the search interval in half.

If the value to be searched is less than the item in the middle of the interval, the search is narrowed to the lower half, otherwise the search is narrowed to the upper half.

Not Recommended

Read table int_fligh with key airln = 'LF'.

Recommended

Read table int_fligh with key airln = 'LF' binary search.

Hope this will help.

Regards,

Ferry Lianto

Read only

Former Member
0 Likes
1,062

Hi,

When you specify the binary search it uses this search technique with the fields given in the with key option.

The fields that you specify in the with key option needs to be sorted in the order in which you specify..

Thanks,

Naren

Read only

Former Member
0 Likes
1,062

Hi,

WITH KEY = dobj [BINARY SEARCH]

Effect:,If the addition WITH KEY is followed by a single data object after an "equals" sign, the first line found in the internal table itab is read, whose whole content corresponds to the content of the data object dobj. It must be possible to convert the data object dobj to the line type of the internal table. If the data type of dobj does not correspond to the line type of the internal table, a conversion is performed for the comparison according to the conversion rules.

Regards,

Azaz Ali.

Read only

Former Member
0 Likes
1,062

Hi

It can improve the peformance of READ TABLE statament because a "Binary" strategy is used to read the internal table, but to use this option the table has to be sorted.

SORT ITAB by FIELD1 FIELD2 .... FIELDN

READ TABLE ITAB WITH KEY FIELD1 =

FIELD2 =

.................................

FIELDN =

binary search.

Max

Read only

Former Member
0 Likes
1,062

Hi Tom,

For binary search, you basically sort the internal table first.

Then the internal logic for binary search is that program splits internal table into 2 parts and does match in first part. If required key is not found, then the other half is used for search so in short you are splitting the internal table for lookup. This increases the performance of search and lookup.

Cheers,

Vikram

Please reward for helpful replies!!

Read only

Former Member
0 Likes
1,062

Hi Tom,

1) BINARY SEARCH clause with READ statement improves the performance.

2) You need to SORT the internal table with specific fields before using BINARY SEARCH clause with READ statement

3) Otherwise, it leads to improper results

To know more about READ statement with BINARY SEARCH clause, browse this link

http://www.sts.tu-harburg.de/teaching/sap_r3/ABAP4/read_t01.htm

Thanks,

Vinay