Application Development 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 using binary search

Former Member
0 Kudos

Hi,

I have an internal table itab on which I am using READ inside a loop. The READ on itab is being used multiple times inside the loop with different keys.

I have sorted itab with 3 keys before the loop. In one of the READs I am using all the 3 keys but in another READ I am using first 2 keys only.

So is the code fine with BINARY SEARCH on these READs cos I have sorted with 3 keys but Reading with 2 keys in some places.

Please let me know your suggestions.

Thanks,

Abhishek

1 ACCEPTED SOLUTION

former_member212653
Active Contributor
0 Kudos

Yes it will work if you have exactly don what you have said. it will not work if you READ with 2nd and 3rd key.

13 REPLIES 13

Former Member
0 Kudos

Always use READ statement with Binary Search to avoid performance issues.

Thanks

Sudharshan

former_member181995
Active Contributor
0 Kudos

>

> Please let me know your suggestions.

> Abhishek

What Suggestions you need?

I don't think you asked anything in your question.

0 Kudos

I want to know that if I sort an internal table with 3 keys but in the READ I use the first 2 keys then is it fine to BINARY SEARCH in this READ or not as I dont have the full keys in READ as in the SORT.

Thanks,

Abhishek

0 Kudos

You don't need to add BINARY SEARCH to your READ statement when working with a sorted table and read WITH TABLE KEY. It's always an implicit binary search.

If you use the first two fields of a three field table key, performance should be OK. You might not get a unique entry though with READ, so maybe a LOOP AT ... WHERE would be better here?

Thomas

Former Member
0 Kudos

hi,

binary search is pre requisit for read.

check this,

http://help.sap.com/saphelp_nw04/helpdata/EN/fc/eb373d358411d1829f0000e829fbfe/content.htm

Regards,

Anirban

Former Member
0 Kudos

Hi,

This is because when you have sorted the table with 3 keys and using binary search reading with two fields...it might show abnormal results.

Thus before reading the table with two keys ..sort the table with same two keys.

Thanks,

Parul.

Former Member
0 Kudos

Hi,

What are the fields that you are using in Read table WITH KEY, the internal table must be sorted with those KEY fields first. If you want to use multiple Read table statements and each time you need to read it by different keys, then make temporary tables of same type and pass the data to those new internal tables then sort those with keys and use those tables in the loop statement..

Rgds,

Bujji

former_member212653
Active Contributor
0 Kudos

Yes it will work if you have exactly don what you have said. it will not work if you READ with 2nd and 3rd key.

0 Kudos

Hi,

For the binary search, the table must be sorted by the specified search key in ascending order. Otherwise the search will not find the correct row.

former_member212653
Active Contributor
0 Kudos

I don't know whu i couldn't edit my previous post...so I'am posting again.

Try to visualize the binary search algorithm then it will be clear to you that this will work.

0 Kudos

Hello Abishek,

Sorting with three fields and reading with first two fields using binary search will work very much fine.

Regards

Farzan

Former Member
0 Kudos

Hi,

Remember always use the same keys in sorting whenever you are using binary search else reduce performance & data may erronous.

Thanks,

Krishna...

Former Member
0 Kudos

no problem searching with 2 keys. you will only get 1 record where there is the possibility that more records meet your search-values. But that is also possible with 3 keys if they do not uniquely identify each record.

Regards,

Guido