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: 

Which is faster, sorted table or hashed table...

aris_hidalgo
Contributor
0 Kudos

Moved to correct forum by moderator. Though I suspect this has been answered quite a few times...

Hello Experts,

We are having a confusion here as to what table type to use. The other abaper said that it is better to use

sorted table then read with table key or standard table, sort it then read using binary search. The other said

it is better to use hashed table since the tables that we are getting from are either master tables or we can use the exact

key for example in MSEG where we know the values of MBLNR, MJHAR and ZEILE.

Please enlighten me on this.

Thank you guys and take care!

Edited by: Matt on Jan 21, 2009 5:36 PM

1 ACCEPTED SOLUTION

former_member194613
Active Contributor
0 Kudos

I have writing the blog mentioned above, which should answer most of question.

But to make it simple, I would always recommend SORTED tables.

+ they scale with logarithmic increase

+ but start lower than hashed, which cost always the same independent of the size of the table,

actually they become only for every large tables slightly better than sorted tables

+ sorted tables allow you to access ranges not only single lines as hashed tables do, i.e. also

loops at where are fast

+ standard tables, more or less outdated, the binary search is as fast as the sorted access, but

table must be sorted and should be sorted only once. Sorting is expensive. Either table

is not sorted, then binary search works wrong or binary search is not used, then it is extremely

slow. Fast Loop at where is complicated, see blog.

And of course all table types can be changed!

Siegfried

12 REPLIES 12

Former Member
0 Kudos

hi,

For single read access hashed tables are more optimized as compared to sorted tables.

For partial sequential access sorted tables are more optimized as compared to hashed tables

regards

Jayapriya

SuhaSaha
Advisor
Advisor
0 Kudos

Hello Viraylab,

A very dicey question !!!!

I hope this blog by Siegfried will throw some insight:

[Runtimes of Reads and Loops on Internal Tables|https://www.sdn.sap.com/irj/scn/weblogs?blog=/pub/wlg/7247] [original link is broken] [original link is broken] [original link is broken];

I really found it interesting.

BR,

Suhas

Former Member
0 Kudos

hi

Compared to sorted table its better to use hash table as hashed tables requires only keys and not by indices. hash table ensures that unique key should be there in the table to retrieve data. Which in turn improves std .

In particular hash table supports iterating items in order .By not maintaining indeces, look ups and insertion action can be done faster.

regards

rajye

Hope the content is usefull

Former Member
0 Kudos

hi,

if a table contains relatively few entries but has to deal with many changing acessess,a sorted table can deliver better performance than a hashed table.the use of hashed table only make sense when u have to store large amount of data locally for mostly read access.In a loop , a hashed table always has to be serach completely .because the records usually distributed without any sorting,a sorted table will give better performance after all u loop over the first part of the key or could even make sense to sort the hashed table.In hashed table it is better to enable unique key acess for efficiency.u can rely on this data as it is information given in the sap provided tutorial for certification and i have tried ihe efficiency.....

Former Member
0 Kudos

Hi,

Sorted table is faster than hashed table as data is already in sorted manner.

and for your reading:

Sorted tables

This is the most appropriate type if you need a table which is sorted as you fill it. You fill sorted tables using the INSERT statement. Entries are inserted according to the sort sequence defined through the table key. Any illegal entries are recognized as soon as you try to add them to the table. The response time for key access is logarithmically proportional to the number of table entries, since the system always uses a binary search. Sorted tables are particularly useful for partially sequential processing in a LOOP if you specify the beginning of the table key in the WHERE condition.

Hashed tables

This is the most appropriate type for any table where the main operation is key access. You cannot access a hashed table using its index. The response time for key access remains constant, regardless of the number of table entries. Like database tables, hashed tables always have a unique key. Hashed tables are useful if you want to construct and use an internal table which resembles a database table or for processing large amounts of data.

Also follow these links for better understanding

<<removed_by_moderator>>

Regards:

Alok

Edited by: alok bansal on Jan 21, 2009 8:04 AM

Don't post any kind of materials or links pointing to Spam sites

Edited by: Vijay Babu Dudla on Jan 21, 2009 3:18 AM

Former Member
0 Kudos

go through the below code it will help u

Hashed And Sorted Tables

Point # 1

Consider the following example where HTAB is a hashed table and STAB is a sorted table

DO 250 TIMES.

N = 4 * SY-INDEX.

READ TABLE HTAB INTO WA WITH TABLE KEY K = N.

IF SY-SUBRC = 0.

" ...

ENDIF.

ENDDO.

This runs faster for single read access as compared to the following same code for sorted table

DO 250 TIMES.

N = 4 * SY-INDEX.

READ TABLE STAB INTO WA WITH TABLE KEY K = N.

IF SY-SUBRC = 0.

" ...

ENDIF.

ENDDO.

Point # 2

Similarly for Partial Sequential access the STAB runs faster as compared to HTAB

LOOP AT STAB INTO WA WHERE K = SUBKEY.

" ...

ENDLOOP.

This runs faster as compared to

LOOP AT HTAB INTO WA WHERE K = SUBKEY.

" ...

ENDLOOP.

GrahamRobbo
Active Contributor
0 Kudos

Depends what you are trying to do.

Try it and see.

Cheers

Graham Robbo

Former Member
0 Kudos

This message was moderated.

Former Member
0 Kudos

Hi,

HASHED tables are faster. As number of records are more or less it will take same time.

buT U KNOW ON HASHED TABLE WE CAN'T USE MIDIFY,UPDATE SUCH LIST OF COMMANDS.

cHEERS,

PRAVIN

matt
Active Contributor
0 Kudos

>

> buT U KNOW ON HASHED TABLE WE CAN'T USE MIDIFY,UPDATE SUCH LIST OF COMMANDS.

At best misleading, at worst - WRONG

former_member194613
Active Contributor
0 Kudos

I have writing the blog mentioned above, which should answer most of question.

But to make it simple, I would always recommend SORTED tables.

+ they scale with logarithmic increase

+ but start lower than hashed, which cost always the same independent of the size of the table,

actually they become only for every large tables slightly better than sorted tables

+ sorted tables allow you to access ranges not only single lines as hashed tables do, i.e. also

loops at where are fast

+ standard tables, more or less outdated, the binary search is as fast as the sorted access, but

table must be sorted and should be sorted only once. Sorting is expensive. Either table

is not sorted, then binary search works wrong or binary search is not used, then it is extremely

slow. Fast Loop at where is complicated, see blog.

And of course all table types can be changed!

Siegfried

0 Kudos

An interesting discussion ... and my contribute would be - it depends on what you are doing!

It's very strange when people say that this kind of table is faster or better than the other. That's wrong (in my opinion). There is times, that you should use hashed tables for sure. There is times where you should use sorted. But yes, there is situations when you should use standard tables ...

Sorted tables are faster than standard tables? Yes, no doubt about that. But in some particular situations, you may be forced to use standard tables. Image that you have to use several appends to your table in runtime. Appending a sorted table several times will be slower than doing it in a standard table.

Hashed tables ... the best and fastest .. when you can use it! (don't need a loop, just to get a field description - example: an itab with material descriptions)

It depends ...

Regards,

Valter Oliveira.