Skip to content

Avoid Linear READ TABLE Scans Inside Loops ​

WarningPerformance & Database#EC NO_READ_LOOPRemote Enabled✏️ Edit Rule on GitHub

Overview ​

Avoid linear READ TABLE scans inside loop iterations.

ATC Finding Messages:

  • READ TABLE ... WITH KEY inside a loop scans the table on every pass
  • READ TABLE ... WHERE inside a loop scans the table on every pass

Rationale ​

A READ TABLE that addresses rows through WITH KEY or WHERE walks the internal table entry by entry, so placing it inside a LOOP turns the routine into an O(N*M) scan: doubling both tables quadruples the work, and code that passes a smoke test with fifty rows stalls once production feeds it fifty thousand. Declaring the looked-up table as HASHED or SORTED with the searched component as key, or resolving the values in one pass before the loop, keeps each lookup at constant or logarithmic cost. The rule reports only the linear access forms; reads through WITH TABLE KEY, BINARY SEARCH or INDEX address a declared key and stay unreported.

Non-Compliant ​

abap
LOOP AT lt_orders INTO DATA(ls_order).
  READ TABLE lt_items INTO DATA(ls_item) WITH KEY order_id = ls_order-id.
  APPEND ls_item TO lt_selected.
ENDLOOP.

Compliant ​

abap
DATA lt_items_by_id TYPE HASHED TABLE OF ty_item WITH UNIQUE KEY order_id.
lt_items_by_id = VALUE #( FOR ls_item IN lt_items ( ( ls_item ) ) ).

LOOP AT lt_orders INTO DATA(ls_order).
  READ TABLE lt_items_by_id INTO DATA(ls_item) WITH TABLE KEY order_id = ls_order-id.
  APPEND ls_item TO lt_selected.
ENDLOOP.

Configuration & Thresholds ​

This check can be fine-tuned directly in the ATC Check Variant Editor (CHKV) in Eclipse ADT:

Parameter NameData TypeDefault ValueDescription & Guidance
CHECK_WITH_KEYBooleantrueReport READ TABLE ... WITH KEY inside a loop, because the key is compared against every row. Disable when a codebase only treats the WHERE form as a defect while table types are still being converted.
CHECK_WHEREBooleantrueReport READ TABLE ... WHERE inside a loop, because a WHERE condition never engages a table key and always visits all rows. Disable to allow condition based reads during a migration.

Suppression ​

To suppress findings for this rule, append the pseudo-comment to the statement:

abap
" Standard pseudo-comment:
... "#EC NO_READ_LOOP

" SAP CI_ prefix format:
... "#EC CI_NO_READ_LOOP

Access forms ​

Only READ TABLE statements inside a LOOP, DO or WHILE body are evaluated, and key access is separated from linear access:

Access formInside a loop
READ TABLE lt INTO ls WITH KEY f = v.reported (NO_RD_WKEY)
READ TABLE lt TRANSPORTING NO FIELDS WITH KEY f = v.reported (NO_RD_WKEY)
READ TABLE lt INTO ls WHERE f = v.reported (NO_RD_WHRE)
READ TABLE lt INTO ls WITH TABLE KEY f = v.allowed, addresses a declared key
READ TABLE lt INTO ls WITH KEY f = v BINARY SEARCH.allowed, logarithmic search
READ TABLE lt INTO ls INDEX lv_index.allowed, constant time

Notes ​

  • READ LINE, READ REPORT, READ DATASET and the EML READ ENTITIES form are not internal table reads and are never reported.
  • Keyword tokens are only recognised when they carry no compiler reference, so a variable or component named where or key cannot raise a finding.
  • The loop does not have to iterate over the table being read; a linear read of any table while a loop is open repeats the scan on every pass.
  • Fix a finding by declaring the read table HASHED or SORTED with the searched component as key and reading it with WITH TABLE KEY, or by resolving all lookups in a single pass before the loop.
  • Suppress an intentional read with "#EC NO_READ_LOOP on the statement.

Technical Details ​

Community & Feedback ​

Released under the MIT License.