01-30-2013 5:32 PM
Dear gurus,
Can anyone tell me which way is better to access data within a range in an internal table which holds thousands to tens of thousands entries? Thank you!
1. Standard table.
Sort table.
Read table with key1 = value1.
Loop at table from sy-index
where key2 > value2 and key3 < value3.
Get the data. Exit.
End Loop.
2. Sorted table.
Loop at table where key1 = value1
and key2 > value2 and key3 < value3.
(Some document says it will use binary search automatically when looping. Hope it is true.)
Get the data. Exit.
Endloop.
3. Hashed table with unique key.
Loop at table where key1 = value1
and key2 > value2 and key3 < value3.
(No doubt that some hash algorithm will be used here. Can anyone kindly tell me what is the exact hash algorithm adopted by SAP ABAP?)
Get the data. Exit.
Endloop.
And what if the internal table to search through has a lot of keys (far more than 3 keys), say 20 keys to compare? I have found a post, Runtimes of Reads and Loops on Internal Tables (http://scn.sap.com/community/abap/testing-and-troubleshooting/blog/2007/09/12/runtimes-of-reads-and-...). But I still have doubts after reading it.
01-31-2013 10:40 AM
The following SAP documentation answers your query: http://help.sap.com/abapdocu_731/en/abenitab_where_optimization.htm
BR,
Suhas
01-30-2013 6:25 PM
hello,
For your requirement you can go with option 1. Define a standard table and then sort it. while reading entries use binary search. You need not worry about option 2 and 3.
best regards,
swanand
01-30-2013 7:25 PM
Hi Arwen,
1. Standard Table -> oftenly used with Sorting key columns with binary search. Advantage is -> we can use all the Internal table operational keywords in standard tables.
2. Sorted Table -> binary search algorithm is followed by default for Read Table.
3. Hashed Table -> Read time is same for every record irrespective of no of records. But some keywords are not used with Hash table, like Append.
For more info on HASH Algo - plz refer -> http://scn.sap.com/thread/516047
Thanks
Vivek
01-31-2013 4:02 AM
Hi Arwen,
First of all, looking at your requirement, the first approach you've mentioned seems semantically incorrect because the equality on key1 is being checked only once (i.e. in the first loop iteration).
Since you have a large internal table and you have an equality condition (key1 = value1), it would make sense to use an optimized internal table (either sorted or hashed). However, to be sure which one's better, I would recommend that you try out both approaches and then measure the response times (you can use SAT).
01-31-2013 10:32 AM
I suggest to use option 2.
Option 1 has only advantages if you will use the table with different keys and you have to sort it in a different way.
Option 3 I think will not be good, because for the hashed tables you need a full specification of the key.
Option 2 - easier to read. No "exit" statement is necessary inside the loop. And KEY1 will be used for the binary search to quickly locate the first entry in the table.
01-31-2013 10:40 AM
The following SAP documentation answers your query: http://help.sap.com/abapdocu_731/en/abenitab_where_optimization.htm
BR,
Suhas