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

doubt in internal tables

Former Member
0 Likes
618

hi experts

could you please tell difference between linear search, binary search and hashed mechanism.

above 3 which is faster and why.

if i use binary search in descending order it will search the records are not could please send why.

hi experts

could you please tell difference between linear search, binary search and hashed mechanism.

above 3 which is faster and why.

if i use binary search in descending order it will search the records are not could please send why.

4 REPLIES 4
Read only

Former Member
0 Likes
567

hi,

hashed is faster compared to others cuz it uses hashing algorithms to access the entries and the response time is independent of the number of entries in the table.

for binary search u have to sort it accoring to the key using which u'll read the internal table and response time is logarithamically proportional to the number of entries in the table.

linear search :response time is directly proportional to the number of entries in the table

          • please reward points if its of any help to u

regards,

madhu

null

Read only

Former Member
0 Likes
567

Hi,

Please check this links.

/people/harry.dietz/blog/2005/10/28/performance-improvement-hints-3-internal-table--fill-and-read

http://www.sap-img.com/abap/what-are-different-types-of-internal-tables-and-their-usage.htm

Hope this will help.

Regards,

Ferry Lianto

Read only

Former Member
0 Likes
567

Hi Sayeed,

Standard table:

The key access to a standard table uses a sequential search. The time required for an access is linearly dependent on the number of entries in the internal table.

You should usually access a standard table with index operations.

Sorted table:

The table is always stored internally sorted by its key. Key access to a sorted table can therefore use a binary search. If the key is not unique, the entry with the lowest index is accessed. The time required for an access is logarithmically dependent on the number of entries in the internal table.

Index accesses to sorted tables are also allowed. You should usually access a sorted table using its key.

Hash table:

The table is internally managed with a hash procedure. All the entries must have a unique key. The time required for a key access is constant, that is it does not depend on the number of entries in the internal table.

You cannot access a hash table with an index. Accesses must use generic key operations (SORT, LOOP, etc.).

Index table:

The table can be a standard table or a sorted table.

Index access is allowed to such an index table. Index tables can be used to define the type of generic parameters of a FORM (subroutine) or a function module.

Just have a look at these links:

http://help.sap.com/saphelp_nw04/helpdata/en/90/8d7304b1af11d194f600a0c929b3c3/frameset.htm

http://help.sap.com/saphelp_nw04/helpdata/en/74/83015785d811d295a800a0c929b3c3/frameset.htm

Also Go through the following Document

1.1 STANDARD table

Key access to a standard table uses a linear search. This means that the time required for a search is in linear relation to the number of table entries.

You should use index operations to access standard tables.

1.2 SORTED table

Defines the table as one that is always saved correctly sorted.

Key access to a sorted table uses a binary key. If the key is not unique, the system takes the entry with the lowest index. The runtime required for key access is logarithmically related to the number of table entries.

1.3 HASHED table

Defines the table as one that is managed with an internal hash procedure

You can only access a hashed table using the generic key operations or other generic operations ( SORT, LOOP, and so on). Explicit or implicit index operations (such as LOOP ... FROM oe INSERT itab within a LOOP) are not allowed.

1.4 INDEX table

A table that can be accessed using an index.

Index table is only used to specify the type of generic parameters in a FORM or FUNCTION. That means that you can't create a table of type INDEX.

Standard tables and sorted tables are index tables.

1.5 ANY table

Any table is only used to specify the type of generic parameters in a FORM or FUNCTION. That means that you can't create a table of type ANY.

Standard, sorted and hashed tables belongs to ANY tables.

2. Defining an internal table

DATA itab TYPE table type of line type [WITH UNIQUE/NON-UNIQUE KEY <key>] [Iinitial size n] [WITH HEADER LINE]

Note: There are also other ways to define an internal table. Please refere to the documentation.

2.1 The KEY option

KEY key1,,keyn :

key1..keyn are fields in the table. The sequence in which you specify the key is significant.

DEFAULT KEY :

The key fields are the standard keys. Note that you can only specify an empty key for tables with access type STANDARD TABLE. The standard key basically comprises all tables fields with character-like types (type ( C, STRING, D, T, N, X, XSTRING). In particular, components with a numeric type (also refer to ABAP numeric types) and table components usually do not belong to the standard key.

Example:

types:

begin of t_makt,

matnr like makt-matnr,

maktx like makt-maktx,

end of t_makt.

data:

  • Define the table

gi_makt type sorted table of t_makt with unique key matnr.

  • Define the work area for the table if necessary

gi_makt type t_makt.

3. Reading internal tables

READ TABLE itab WITH TABLE KEY k1 = v1 k2 = v2 [additions]

Note: In case of more than one match, it is the first match that is selected.

STANDARD TABLE: The system searches from the start of the table. The response time is in linear relation to the number of table entries.

SORTED TABLE: The response time is in logarithmic relation to the number of table entries.

HASHED TABLE: The response time is constant

READ TABLE itab WITH KEY k1 = v1 k2 = v2 [BINARY SEARCH] [additions]

Note: In case of more than one match, it is the first match that is selected.

STANDARD TABLE: If you use the ... BINARY SEARCH addition, the system uses a binary search. Otherwise, the search is sequential. This assumes that the internal table is sorted in ascending order in the sequence of the specified key fields.

SORTED TABLE: If the specified key fields form a left-justified extract of the table key, the search is binary, otherwise sequential.

HASHED TABLE: Sequential search.

READ TABLE itab INDEX i [additions]

Accessing the table entry with the index i.

Additions:

INTO wa - wa is used as output area

ASSIGNING <fs> - The field symbol <fs> is assigned to the entry. This saves the cost of copying the contents in comparison to the first addition. However, this addition does involve table administration costs, and it is therefore only worthwile for lines longer than around 300 bytes.

COMPARING f1...fn - If the system find an entry, the system compares the subfields f1, f2, ... with the corresponding fields of the work area before they are transported into it.

COMPARING ALL FIELDS

TRANSPORTING f1 f2 - If the system finds an entry, it does not transfer all of the subfields (default) into the work area, but only the specified fields f1 f2 ...; the other subfields remain unchanged.

TRANSPORTING NO FIELDS

Example:

loop at gi_mseg into g_mseg.

read table gi_makt

with table key matnr = g_mseg-matnr

into g_makt.

endloop.

Regards

Sreeni

Read only

Former Member
0 Likes
567

<b>Standard Internal Tables</b>

Standard tables have a linear index. You can access them using either the index or the key. If you use the key, the response time is in linear relationship to the number of table entries. The key of a standard table is always non-unique, and you may not include any specification for the uniqueness in the table definition.

This table type is particularly appropriate if you want to address individual table entries using the index. This is the quickest way to access table entries. To fill a standard table, append lines using the (APPEND) statement. You should read, modify and delete lines by referring to the index (INDEX option with the relevant ABAP command). The response time for accessing a standard table is in linear relation to the number of table entries. If you need to use key access, standard tables are appropriate if you can fill and process the table in separate steps. For example, you can fill a standard table by appending records and then sort it. If you then use key access with the binary search option (BINARY), the response time is in logarithmic relation to

the number of table entries.

<b>Sorted Internal Tables</b>

Sorted tables are always saved correctly sorted by key. They also have a linear key, and, like standard tables, you can access them using either the table index or the key. When you use the key, the response time is in logarithmic relationship to the number of table entries, since the system uses a binary search. The key of a sorted table can be either unique, or non-unique, and you must specify either UNIQUE or NON-UNIQUE in the table definition. Standard tables and sorted tables both belong to the generic group index tables.

This table type is particularly suitable if you want the table to be sorted while you are still adding entries to it. You fill the table using the (INSERT) statement, according to the sort sequence defined in the table key. Table entries that do not fit are recognised before they are inserted. The response time for access using the key is in logarithmic relation to the number of

table entries, since the system automatically uses a binary search. Sorted tables are appropriate for partially sequential processing in a LOOP, as long as the WHERE condition contains the beginning of the table key.

<b>Hashed Internal Tables</b>

Hashes tables have no internal linear index. You can only access hashed tables by specifying the key. The response time is constant, regardless of the number of table entries, since the search uses a hash algorithm. The key of a hashed table must be unique, and you must specify UNIQUE in the table definition.

This table type is particularly suitable if you want mainly to use key access for table entries. You cannot access hashed tables using the index. When you use key access, the response time remains constant, regardless of the number of table entries. As with database tables, the key of a hashed table is always unique. Hashed tables are therefore a useful way of constructing and

using internal tables that are similar to database tables.

The hash table search mechanism is not depend on the no of entries in the table. When the table contains more records then hash is best because it itself has an index.

Before going into binary search, the table shd be sorted by default ascending order. I dont think it will work when data is in descending order.

Regards,

U. Uma