How to use Binary search,Move-corresponding in SAP ABAP

How to use Binary search in Read table operation

There are two important points to be considered for Binary search.

  • The table must be sorted using the keys.
  • The order in which the keys are sorted must be similar to the keys mentioned in the Read Table command.

DATA lt_table1 TYPE TABLE OF db1.

DATA ls_structure1 TYPE db1.

SELECT * FROM db1 INTO TABLE lt_table1.

SORT lt_table1 BY field1 field2.

READ TABLE lt_table1 INTO ls_structure1 WITH KEY field1 = iv_value1 field2 = iv_value2 BINARY SEARCH.

Can we use non numeric fields such as character field for Binary search

Non-numeric fields such as character fields can be used in binary search

how to use Move-corresponding in two internal tables

If the target table it_table2 has only few fields compared to source table it_table1. Then please avoid move corresponding to improve the performance.

LOOP AT it_table1 INTO ls_structure1.

              CLEAR ls_structure2.

              MOVE-CORRESPONDING ls_structure1 TO ls_structure2

              APPEND ls_structure2 TO it_table2.

ENDLOOP.

For better performance:

LOOP AT it_table1 INTO ls_structure1.

              CLEAR ls_structure2.

              MOVE ls_structure1-field1 TO ls_structure2-field1

              MOVE ls_structure1-field2 TO ls_structure2-field2

              APPEND ls_structure2 TO it_table2.

ENDLOOP.

Why my values are incorrect with MOVE-CORRESPONDING statement.

What can be the common reason in all loops. All remember to use clear at the beginning of the loop, if you are appending the contents to another internal table. The proper sequence is a) clear the structure b) move the fields from one structure to another c) append the structure to internal table

LOOP AT it_table1 INTO ls_structure1.

              CLEAR ls_structure2. 

              MOVE-CORRESPONDING ls_structure1 TO ls_structure2.

              APPEND ls_structure2 TO it_table2.

ENDLOOP.

In the above loop, the clear statement is missing. This leads to problem, by using the previous entries in the loop.

Is Move and = assignment in ABAP is same.

Both are similar, there is no performance difference between the two.

How to merge internal table in a structure

TYPES: BEGIN OF st_struct,

              Field1 TYPE i,

              lt_tab_inside TYPE STANDARD TABLE OF db1,

           END OF st_struct.

TYPES: tt_table TYPE TABLE OF st_struct.

DATA pt_table1 TYPE tt_table.

Summary: How to improve the performance of the loop operation

  1. Prefer sorted tables, hashed tables if possible
  2. Use Field-symbols instead of work area
  3. Use binary search after sorting the tables
  4. Use indexes to quickly locate a record
  5. Use joins in the select query to reduce internal table operations
  6. Always avoid nested loops by using the loop at and then read table for checking on second tab
  7. Use Move instead of Move-corresponding

What is the difference between Standard table, Sorted table and Hash table

ParameterStandardSortedHashed
Definitioncan be Without Primary keycan be Without Primary keyonly With Primary key
AccessIndex, Primary keyIndex, Primary keyPrimary key
IndexDirect accessDirect access
Primary keyPerformance α No of Data recordsPerformance = Log(no of datarecords)Constant

What is the syntax of Standard table, sorted table and hashed table

Standard table : DATA lt_standard TYPE STANDARD TABLE OF db1 WITH UNIQUE KEY field1 field2

Sorted table: DATA lt_sorted TYPE SORTED TABLE OF db1 WITH UNIQUE KEY field1 field2

Hashed table: DATA lt_hashed TYPE HASHED TABLE OF db1 WITH UNIQUE KEY field1 field2

In the upcoming posts, we will discuss each point in detail. Please feel free to write us for any comments or queries.