ABAP - Internal tables (example)
Performance when accessing internal tables may vary depending on access. How to check your data and the times, we'll show you in this article.
Table of contents
In the previous articles we talked about performance and access. Today we want to clarify the question with you, which of the accesses is faster or easier and how you can measure it for yourself.
Preparation
To prepare the test, we define the tables for access. Each individual table is used for access and represents an access type for us.
" Tables definition
DATA:
ld_tab TYPE string,
lt_sttab TYPE STANDARD TABLE OF t001,
lt_sotab TYPE SORTED TABLE OF t001 WITH UNIQUE KEY bukrs,
lt_hatab TYPE HASHED TABLE OF t001 WITH UNIQUE KEY bukrs,
lt_bitab TYPE STANDARD TABLE OF t001,
lt_ketab TYPE STANDARD TABLE OF t001 WITH NON-UNIQUE SORTED KEY sec COMPONENTS bukrs.
Then we read the data for our access from the T001 (company codes). In this case, it is sufficient to read all entries and then move them, because we want to read against all entries.
" Select data
SELECT *
FROM t001
INTO TABLE @lt_sttab.
" Move data
lt_sotab = lt_sttab.
lt_hatab = lt_sttab.
lt_bitab = lt_sttab.
SORT lt_bitab BY bukrs.
lt_ketab = lt_sttab.
The table lt_bitab is a standard table, so we should sort the table to the defined key, otherwise we can not be sure that the table really has the right sort after data selection.
Execution
For our test we have prepared six cases that we would like to test:
- Standard table
- Sorted table
- Hash table
- Read with binary search
- Standard table with secondary key without usage
- Standard table with secondary key with usage
" Processing methods
DO 6 TIMES.
DATA(ld_index) = sy-index.
" Start measurement
GET RUN TIME FIELD DATA(ld_start).
LOOP AT lt_sttab INTO DATA(ls_data).
" Choose method
CASE ld_index.
WHEN 1.
ld_tab = 'Standard'.
DATA(ls_bukrs) = lt_sttab[ bukrs = ls_data-bukrs ].
WHEN 2.
ld_tab = 'Sorted'.
ls_bukrs = lt_sotab[ bukrs = ls_data-bukrs ].
WHEN 3.
ld_tab = 'Hashed'.
ls_bukrs = lt_hatab[ bukrs = ls_data-bukrs ].
WHEN 4.
ld_tab = 'BinarySearch'.
READ TABLE lt_bitab INTO ls_bukrs
WITH KEY bukrs = ls_data-bukrs
BINARY SEARCH.
WHEN 5.
ld_tab = 'SecondaryKey ohne Angabe'.
ls_bukrs = lt_ketab[ bukrs = ls_data-bukrs ].
WHEN 6.
ld_tab = 'SecondaryKey mit Angabe'.
ls_bukrs = lt_ketab[ KEY sec COMPONENTS bukrs = ls_data-bukrs ].
ENDCASE.
ENDLOOP.
" Finish measurement
GET RUN TIME FIELD DATA(ld_end).
WRITE: / |Messung { ld_tab }: { ld_end - ld_start }|.
ENDDO.
Using Get Runtime, we read the used time at the beginning and at the end and compare the result.
Result
As we described in the last article, the accesses with defined keys or the Binary Search are the fastest. In addition the result after the execution of the report (with approx. 4500 data records) in three different runs, in order to better represent the varying result.
Conclusion
When accessing internal tables, you should always make sure that you have also defined the correct key or that the table does not contain many records. However, as our tests have shown, it does not really matter what kind of key you use, the main thing is that it is defined at all.