Skip to Content

Performance: The optimal way to change the content of internal table

In ABAP we can update the content of internal table at least by two ways:

1. Pass over the internal table with LOOP-AT statement, assign the iteration value to FIELD-SYMBOL pointer and update the desired attribute of the structure.

2. Use MODIFY statement for internal table with TRANSPORTING-WHERE addition.

The question is what way is more optimal from performance point of view for large and short tables.

It will be great if some theoretical explanation will be supplied, why exactly this way is better, than the other one.

I tried to check the subject in Tips & Tricks of SE30, but I'm not sure, that these results are statistically reliable.

Thanks.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

6 Answers

  • Best Answer
    Jun 14, 2013 at 08:13 PM

    Thanks guys, let me specify the question with some example.

    Let say I have an internal table, where one of the columns is a language, e.g. «EN-US», «EN-CA», «FR-CA» etc., now I want to replace all language values from «EN-US» to «EN-GB».

    I can do it with:

    1. LOOP AT itab ASSIGNING <wa>.
    2.         IF <wa>-lang = 'EN-US'.
    3.                 <wa>-lang = 'EN-GB'.
    4.         ENDIF.
    5. ENDLOOP.

    or with:

    1. wa-lang = 'EN-GB'.
    2. MODIFY itab FROM wa TRANSPORTING lang WHERE lang = 'EN-US'.

    My question, what is the optimal approach from performance point of view to implement such logic for case with long and short itabs.

    In some places I heard, that the second one is better, but I would like to get some theoretically supported explanation.

    Thanks.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jun 14, 2013 at 10:23 PM

    Hi,

         Field symbols give better performance.But if you want to change the key fields of table we cannot use field symbols because we cannot change key fields of table in the loop using field symbols.

    Regards,

    Sreenivas.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jun 25, 2013 at 08:52 AM

    Hi Mike,

    Field-Symbols are the direct memory locations for the values present in the internal table.

    So, we can avoid the MODIFY statement inorder to change the internal table based upon the WorkArea.

    So, Field Symbols will increase the performance.

    Let me know if any clarification/help required.

    Thanks & Best Regards.

    Pavan Neerukonda.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jun 14, 2013 at 06:42 PM

    Hi Mike,

    The main difference beteween:

    As per your question we can write the following two codes to loop into and modify the internal tables:

    1) LOOP AT itab INTO wa.

              MODIFY itab..........

         ENDLOOP.

    2) LOOP AT itab ASSIGNING <fs>

              Change contents of <fs>

         ENDLOOP.

    In the first case, while using workarea "wa", the internal table records are copied into it per loop iteration. Here just the value of one itab record is copied in wa.

    However, for the 2nd case, while using field-symbol <fs>, it points to a row in itab in each iteration of the loop. It just works as a pointer to the original row in memory. values of the row are not copied here.

    Is that in the first case you are transfering itab data to the work area, line by line. In the second case, you are creating a pointer to each line of the table, and no data transfer is done.

    So here to modify the itab, if we use <fs>, when we change the content of any column though <fs>, it directly changes the value. We dont need to write the MODIFY statement again.

    But while using "wa", since it is not pointing to the original row in memory that is why we need to write the MODIFY statement here.

    To conclude, while modifying internal table, field-symbol is more efficient than using workarea because it reduces the overhead.

    Please see the below link also to get the advantages of field-symbols:

    http://help.sap.com/saphelp_nw70/helpdata/en/fc/eb3605358411d1829f0000e829fbfe/content.htm

    Thanks,

    Arnab

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hello Arnab,

      I understand Mike question as comparing

      loop at itab assigning ... where ... .

      with code

      modfiy itab from ... where ... transporting...

      Regards,

      Jens

  • avatar image
    Former Member
    Jun 14, 2013 at 07:20 PM

    Hello Mike,

    that is a good question.  😀

    As far as I know  there are some aspects to be considered too:

    - type of internal table (hash, ...)

    - kind of where-clause (non-/key-fields, type of compared fields, size of where clause ...)

    - etc.

    This sounds some complex to me. All I could find was tips doing this or doing that, no theoretical and complete explanation 😔.

    Maybe some guy knowing the compilation of the statements to exe can answer here.

    Regards,

    Jens

    Add comment
    10|10000 characters needed characters exceeded

  • Jun 14, 2013 at 07:46 PM

    Hello Mike,

    The syntax are used for different purposes - Like syntax MODIFY.. WHERE is to modify bunch of records at a time, where as statement MODIFY in the LOOP ... MODIFY .. ENDLOOP, would MODIFY single record at a time.

    Now, if you have quite a few records in your LOOP and you want to modify them conditionally say, every alternate row, you would need to use LOOP.. MODIFY.. ENDLOOP. You wont be able to use MODIFY..WHERE. When you use MODIFY in LOOP, you can either use the Field Symbol or the Workarea. FieldSymbols are far better when you have huge records in the table. Read Use of Field-symbols vs Work area | ABAP Help Blog

    When you use the MODIFY..WHERE, the effect is somewhat similar to LOOP AT.. WHERE.. MODIFY.. ENDLOOP. Since 731 you can specify Secondary Keys to your internal table. If you have done so, the MODIFY using the Secondary Key would be faster with compared to MODIFY without the key. The reason is MODIFY.. WHERE would need to scan the entire standard table and the access is not optimal.

    Thanks,
    Naimesh Patel

    Add comment
    10|10000 characters needed characters exceeded