ABAP - Internal tables (performance)
The second part for the topic internal tables, their processing and performance in accessing the data. Today with a focus on performance in reading the data.
What you should consider when accessing the new methods is, above all, the topic of performance. If you need to read a lot of data and often from internal tables, these should also be prepared for it.
Reading via the index, if possible, is always the best way. But this often doesn't work that way. In the following, we will explain how it relates with the access to the data.
Read over the index
Reading via the index is always fast, here SAP has its own key that speeds up access to special lines. Nothing has to be adjusted or improved on the performance, this key is always fast.
" Index access DATA(ls_t001) = lt_t001[ 1 ].
Read with keys
It looks a bit more critical when reading a key. Here it should be noted whether the table has a key and is sorted accordingly.
Performance problems make the standard tables in most cases because they either have a "scrap" or "empty" key. Here is only useful to read, if the amount of data is less than 100 records, because everything else is just unbearably slow.
That's why we have put together a list for you to show you the most common fast and slow reads.
- Standard tables with default/empty key
- Tables with secondary key without accessing this key
- Sorted tables
- Hashed tables
- Secondary key with accessing this key
- Read table mit binary search (date have to be sorted)
For a better presentation and visualization of accesses, we would like to introduce you in the third part of this article a simple report, with which you can check/test reads and access times.
If you work with a free key for access and only want to read fields from the table, you should only do so with one field. Quickly you will be tempted to use this access over and over again. But you should remember that each of these requests triggers a re-reading of the internal table, which can lead to performance problems.
" Bad example ls_data-waers = lt_t001[ bukrs = 'ABCD' ]-waers. ls_data-btext = lt_t001[ bukrs = 'ABCD' ]-butxt. ls_data-taxno = lt_t001[ bukrs = 'ABCD' ]-stceg. " Better example DATA(ls_t001) = lt_t001[ bukrs = 'ABCD' ]. ls_data-waers = ls_t001-waers. ls_data-btext = ls_t001-butxt. ls_data-taxno = ls_t001-stceg.
When reading the internal tables, we focused on the topic of New ABAP, in order to highlight the innovations and possible sources of error. When accessing via READ TABLE you can avoid some of the weaknesses of the new features, but then it is also the old familiar programming style.