Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Processing Internal Data →  Internal Tables →  Processing Statements for Internal Tables →  READ TABLE itab 

Internal Tables, Key Accesses

This example measures the runtime of the statement READ TABLE with various table keys.

Other versions: 7.31 | 7.40 | 7.54

Source Code

REPORT demo_read_table_using_key.

CLASS measure DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS main.
  PRIVATE SECTION.
    CLASS-DATA: m   TYPE i,
                itab TYPE STANDARD TABLE
                     OF i
                     WITH NON-UNIQUE    KEY primary_key
                       COMPONENTS table_line
                     WITH UNIQUE SORTED KEY sorted_key
                       COMPONENTS table_line
                     WITH UNIQUE HASHED KEY hashed_key
                       COMPONENTS table_line
                     ##tabkey[hashed_key][sorted_key],
                out TYPE REF TO if_demo_output.
    CLASS-METHODS: measure_static  IMPORTING key TYPE string,
                   measure_dynamic IMPORTING key TYPE string.
ENDCLASS.

CLASS measure IMPLEMENTATION.
  METHOD main.
    DATA: n TYPE i,
          j TYPE i.
    DATA max TYPE i VALUE 1000.
    cl_demo_input=>request( EXPORTING text  = 'Maximum line number'
                            CHANGING  field = max ).
    out = cl_demo_output=>new(
      )->begin_section( 'Accessing Internal Tables by Keys' ).
    n = 10.
    WHILE n <= max / 10.
      IF n = 10.
        m = 0.
        j = 10.
      ELSE.
        m = n.
        j = 9.
      ENDIF.
      DO j TIMES.
        m += n.
        itab = VALUE #( BASE itab FOR k = 1 UNTIL k > m ( k ) ).
        out->line( ).
        out->begin_section( 'Static:' ).
        measure_static( key = `primary_key` ).
        measure_static( key = `sorted_key`  ).
        measure_static( key = `hashed_key`  ).
        out->next_section( 'Dynamic:' ).
        measure_dynamic( key = `primary_key` ).
        measure_dynamic( key = `sorted_key`  ).
        measure_dynamic( key = `hashed_key`  ).
        out->end_section( ).
        CLEAR itab.
      ENDDO.
      n *= 10.
    ENDWHILE.
    out->display( ).
  ENDMETHOD.
  METHOD measure_static.
    DATA: t1  TYPE i,
          t2  TYPE i,
          t   TYPE p DECIMALS 2,
          idx TYPE i.
    CLEAR t.
    IF key = `primary_key`.
      DO m TIMES.
        idx = sy-index.
        GET RUN TIME FIELD t1.
        READ TABLE itab
                   WITH TABLE KEY primary_key
                     COMPONENTS table_line = idx
                   TRANSPORTING NO FIELDS
                   ##primkey[sorted_key] ##primkey[hashed_key].
        GET RUN TIME FIELD t2.
        t += t2 - t1.
      ENDDO.
    ELSEIF key = `sorted_key`.
      DO m TIMES.
        idx = sy-index.
        GET RUN TIME FIELD t1.
        READ TABLE itab
                   WITH TABLE KEY sorted_key
                     COMPONENTS table_line = idx
                   TRANSPORTING NO FIELDS.
        GET RUN TIME FIELD t2.
        t += t2 - t1.
      ENDDO.
    ELSEIF key = `hashed_key`.
      DO m TIMES.
        idx = sy-index.
        GET RUN TIME FIELD t1.
        READ TABLE itab
                   WITH TABLE KEY hashed_key
                     COMPONENTS table_line = idx
                   TRANSPORTING NO FIELDS.
        GET RUN TIME FIELD t2.
        t += t2 - t1.
      ENDDO.
    ENDIF.
    t /= m.
    out->write( |{ m }   { t }| ).
  ENDMETHOD.
  METHOD measure_dynamic.
    DATA: t1  TYPE i,
          t2  TYPE i,
          t   TYPE p DECIMALS 2,
          idx TYPE i.
    CLEAR t.
    DO m TIMES.
      idx = sy-index.
      GET RUN TIME FIELD t1.
      READ TABLE itab
                 WITH TABLE KEY (key)
                   COMPONENTS table_line = idx
                 TRANSPORTING NO FIELDS.
      GET RUN TIME FIELD t2.
      t += t2 - t1.
    ENDDO.
    t /= m.
    out->write( |{ m }   { t }| ).
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  measure=>main( ).

Description

The table itab is a standard table with a non-unique primary key, a unique secondary sorted key, and a unique secondary hash key. For demonstration purposes, this program hides the syntax warning about different table keys in an internal table having the same components, by using the pragma ##TABKEY.

The program measures the average access time of the statement READ TABLE to individual table rows, dependent on the number of rows in the table. The rows are accessed using the various keys. The keys are specified statically in the method measure_static and dynamically in the method measure_dynamic.

The result demonstrates that the response times for static access are as expected from around 50 to 100 table rows:

  • When a standard table is accessed using the primary key, the average access time rises in a linear fashion with the number of table rows.
  • When a standard table is accessed using a secondary sorted key, the average access time rises in a logarithmic fashion with the number of table rows.
  • When a standard table is accessed using a secondary hash key, the access time is constant.

In dynamic accesses, each access incurs the additional time required to analyze the dynamically specified key.