Search code examples
jsoninsertruntimefieldabap

INSERT into itab work area vs field symbols performance?


I was testing out inserts of 150 todos into my table. I did it once with loop in workarea and secondly with field symbols. I thought using field symbols would be faster, but somehow it was 167.333 for field symbols and something like 143.431 (I assume its milliseconds?) for work areas. Am I doing something wrong?

TYPES: BEGIN OF ty_todo,
         mandt     TYPE mandt,
         id        TYPE int4,
         userId    TYPE int4,
         todo      TYPE string,
         completed TYPE abap_bool,
       END OF ty_todo.

DATA lv_dauer TYPE i.
DATA: lt_todo TYPE STANDARD TABLE OF ty_todo WITH DEFAULT KEY.
DATA wa_todo TYPE ty_todo.
DATA: lv_json TYPE string.
FIELD-SYMBOLS: <fs_todo> type ty_todo.

lv_json = `[{"id":1,"todo":"Do something nice for someone I care about","completed":true,"userId":26},...] 

* JSON -> ABAP (iTab)
/ui2/cl_json=>deserialize(
  EXPORTING
    json             = lv_json
*    jsonx            =
    pretty_name      = /ui2/cl_json=>pretty_mode-camel_case
*    assoc_arrays     =
*    assoc_arrays_opt =
*    name_mappings    =
*    conversion_exits =
*    hex_as_base64    =
  CHANGING
    data             = lt_todo
).


DELETE FROM zalm_fs_todos.

get run time field lv_dauer.

*Loop in Workarea
LOOP AT lt_todo into DATA(lv_todo).
  INSERT zalm_fs_todos from lv_todo. 
ENDLOOP.

*Loop mit Field-Symbol
* loop at lt_todo assigning <fs_todo>.  
*      INSERT zalm_fs_todos from <fs_todo>.
*endloop.


get run time field lv_dauer.
Write lv_dauer.

Solution

  • With the variant INSERT … FROM TABLE one can also insert all elements from an internal table at once:

    INSERT zalm_fs_todos FROM TABLE lt_todo.
    

    That should be by magnitudes faster as it only does one roundtrip to the database.

    ———-

    That said, the „benchmark“ done here is pretty meaningless. To get a proper benchmark, do:

    • Run each variant n times, as independently as possible, then use the average (and standard deviation to estimate confidence intervals)

    • Loop over larger tables, for 150 entries I would expect the difference to be in the microseconds, I guess you were measuring another effect here

    Generally field symbols and variables should have the same characteristics when reading and field symbols should be better than writing larger structures. But given that field symbols are a rather exotic construct, I‘d rather prefer variables unless there is a significant difference.