Skip to Content
author's profile photo Former Member
Former Member

Optimisation of LOOP statement....

I have got to loop thru thousands/lakhs of records in an internal table.

how to optimse the LOOP statement? Can we increase the performance by just declaring the table as HASHED table or do we have to use any INDEX ot HASH-INDEX in the where condition of LOOP stmt?

If so.. can u give me one example as to how to achieve increased performance of LOOP-ENDLOOP stmt....?

any help wud be appreciated and rewarded !!!

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

9 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Nov 30, 2007 at 06:58 AM

    Hi Ramya,

    If you use where condition for a loop..endloop if would be better to use Hash-table. Hash table is a best approach for huge data (more than a lakh records) because at any point of time it would retrieve a record time is constant. if you r not using where condition in loop then there is no use of using this table.

    Reward if helpful.

    Thanks,

    Sam

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Nov 30, 2007 at 11:52 AM
    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Nov 30, 2007 at 03:14 PM

    If you want to loop through only some records from a huge table it would be the best to have this table sorted by fields that you want to use as filter (fields that decide whether you want to process a record in a loop or omit it).

    If your table is not already sorted, you can always sort it with "sort" statement but you must be aware that the sorting process consumes some time too. The best would be to prepare such table in a way that does not require additional sorting.

    Assuming that your table is sorted by the fields you want to use as filter criteria you proceed like this:

    1. read the first record of the table matching the filter - with statement

    read table ... with key <your filtering fields> binary serach

    Binary search is essential here - it gives you fast access to the record, but it requires the table to be sorted by the fields used in "with key" clause.

    The read statement returns SY-SUBRC = 0 if a matching record is found and SY-TABIX equal to the index of the found record of the table.

    2. Next you loop through the table starting from the index obtained above (SY-TABIX). You do not put "where" clause in the loop staement but instead put an "if" staement inside the loop which check the condition and exits the loop if the condition is not satisfied. This way you perform the loop only for records that you require to - the rest of the records are not nessesary to check at all because once the condition gets false you are sure that for the rest of records it will be also false - because you know your table is sorted by the right fields (iwhich is nessesary for this to work properly).

    So the loop staement will be like this:

    loop at ... from SY-TABIX.
      if not <your filtering fields>.
         exit.
      endif.
      ....
      <your record processing here>
      ....
    endloop.
    

    Additionally if yor table contains large records (i mean: a single records ocupies considerable amount of bytes) you may use a loop staement with assigning a field symbol instead of copying the current record data to some workarea, like this:

    loop at ... assigning <field_symbol>
    ....

    regards

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Nov 30, 2007 at 06:58 AM

    in loop statement can not give where condition.

    replace this way.

    loop at it into wa.

    read table itab into wa with key field wa-field.

    endloop.

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Nov 30, 2007 at 07:00 AM

    To optimize treatment of repetitive LOOP with a WHERE clause, we must declare the table as SORTED TABLE and use of keys in the order in the WHERE clause.

    E.g.

    DATA: header TYPE TABLE of type_header,
               posts TYPE SORTED TABLE of type_posts WITH NON-UNIQUE KEY header_key.
    
    LOOP AT header INTO wa_header.
      LOOP AT post INTO wa_post WHERE header_key = wa_header-header_key.
    ...
      ENDLOOP.
    ENDLOOP.

    Just SORT the table wont have any effect. Look at online help on <i><b>Sorted tables</b>

    This is the most appropriate type if you need a table which is sorted as you fill it. You fill sorted tables using the INSERT statement. Entries are inserted according to the sort sequence defined through the table key. Any illegal entries are recognized as soon as you try to add them to the table. The response time for key access is logarithmically proportional to the number of table entries, since the system always uses a binary search. Sorted tables are particularly useful for partially sequential processing in a LOOP if you specify the beginning of the table key in the WHERE condition.</i>

    Regards

    Add a comment
    10|10000 characters needed characters exceeded

    • If all records are to be processed, then its come to memory management.

      - Use field symbols (so thousand of MOVE into work area are removed)

      - Define a sufficient INITIAL SIZE of the table so the memory is allocated in one block (or in a minimal number of memory blocks)

      - Only put in the internal table data relevant to treatment, remove all fields unused.

      - If you can, parallel the process (If CPU are slow, but some processors are idle), or treat by batch of 10000 records if possible. (If Memory is missing)

      Regards

  • Posted on Nov 30, 2007 at 07:02 AM

    Hi,

    If u want to filter out basing on some condition then use

    LOOP AT ITAB WHERE FIELD1 = <SOMETHING>

    ENDLOOP.

    Otherwise

    do like this

    loop at it into wa.

    read table itab into wa with key field = wa-field.

    endloop.

    Regards,

    Nagaraj

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Nov 30, 2007 at 07:03 AM

    Before stmt LOOP, execute SORT by fields that you have in WHERE clause of LOOP.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Nov 30, 2007 at 07:06 AM

    use standard table and sort it and do binary search it will be better instead of using loop where.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Nov 30, 2007 at 03:25 PM

    Looping through large tables is simply a fact of life in ABAP. There are things you can do to improve performance, but you still have to look at all of the records if have no WHERE.

    What you have to do is tune what you are doing <u>inside</u> the loop. Avoid nested loops; avoid SELECTing on non-key fields; move as much processing outside of the loop; etc.

    Rob

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.