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 table statement

Former Member
0 Kudos
356

can we use index instead of binary search in read table statement? how the statement work with these two key words?

1 ACCEPTED SOLUTION

Former Member
0 Kudos
64

Hi,

Using KEY and BINARY SEARCH

Key is to specify the exact record in the internal table like where condition in select statement.

e.g.

CLEAR wa_iflot.

READ TABLE git_iflot

INTO wa_iflot

WITH KEY tplnr = <Value want to compare>

BINARY SEARCH.

Here line of internal table git_iflot read based on the value of tplnr.

Using INDEX

Index is to directly aceess that line of the internal table.

e.g.

CLEAR wa_iflot.

READ TABLE git_iflot

INTO wa_iflot

WITH INDEX 2.

in above example it will directly read the line at index 2 in internal table

So we can not replace index and binary search with each other.

Its totally depends on the condition.

Regards,

Ranjit Thakur.

<b>Please Mark The Helpful Answer.</b>

3 REPLIES 3

Former Member
0 Kudos
64

Hi,

Following is the syntax for reading with Index.

READ TABLE <itab> INDEX <idx> <result>.
The system reads the line with the index <idx> from the table <itab>.

DATA: BEGIN OF LINE,
COL1 TYPE I,
COL2 TYPE I,
END OF LINE.

DATA ITAB LIKE SORTED TABLE OF LINE WITH UNIQUE KEY COL1.

FIELD-SYMBOLS <FS> LIKE LINE OF ITAB.

DO 20 TIMES.
  LINE-COL1 = SY-INDEX.
  LINE-COL2 = 2 * SY-INDEX.
  APPEND LINE TO ITAB.
ENDDO.

READ TABLE ITAB ASSIGNING <FS> INDEX 7.

WRITE:   SY-SUBRC, SY-TABIX.
WRITE: / <FS>-COL1, <FS>-COL2.

The output is: 

    0         7
         7         14

The example creates a sorted table ITAB and fills it with 20 lines. The line with index 7 is read and assigned to the field symbol <FS>.

=>

If you read entries from standard tables using a key other than the default key, you can use a binary search instead of the normal linear search. To do this, include the addition BINARY SEARCH in the corresponding READ statements.

READ TABLE <itab> WITH KEY <k1> = <f1>... <kn> = <fn> <result> 
                                                       BINARY SEARCH.

The standard table must be sorted in ascending order by the specified search key.

Hope this helps.

Plz close the thread by rewarding if useful.

Regards

Former Member
0 Kudos
65

Hi,

Using KEY and BINARY SEARCH

Key is to specify the exact record in the internal table like where condition in select statement.

e.g.

CLEAR wa_iflot.

READ TABLE git_iflot

INTO wa_iflot

WITH KEY tplnr = <Value want to compare>

BINARY SEARCH.

Here line of internal table git_iflot read based on the value of tplnr.

Using INDEX

Index is to directly aceess that line of the internal table.

e.g.

CLEAR wa_iflot.

READ TABLE git_iflot

INTO wa_iflot

WITH INDEX 2.

in above example it will directly read the line at index 2 in internal table

So we can not replace index and binary search with each other.

Its totally depends on the condition.

Regards,

Ranjit Thakur.

<b>Please Mark The Helpful Answer.</b>

Former Member
0 Kudos
64

Hi,

You can't always use index instead of binary search. However, the quickest way to access a single line of an internal table is direct access using an index, because the response time is then not linked to the size of the table, and is restricted more or less to the transport costs for a single line.

For hashed tables, the response time is constant. Accessing a table using the hash administration makes the response time around 3 times slower than using index access.

If you use the key to access a table, the response time increase as the number of table entries and the size of the search key increase. Searching using a binary search is considerably quicker than using a linear search. Therefore, in many cases it can be quicker to sort the table and then use the BINARY SEARCH addition.

The runtime required to read a line from a table with 100 entries using the index is around 7 msn (standard microseconds), to read a line using a key of 30 bytes using a binary search, around 25 msn, and without binary search, around 100 msn.

Hope it helps.