<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Nested loops in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/nested-loops/m-p/2634619#M606077</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Siegfried and Kjetil - at the risk of beating a dead horse:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;First of all, I have been assuming that we are dealing with standard tables only - not sorted. I prefer to use standard tables because they give me more flexibility. I can sort and resort them as needed.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Secondly, for standard tables, except in the most trivial cases, a binary search will be faster. For small tables, it may be not worth the programming effort.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thirdly, SELECTs in loops should be avoided, but I've never seen an example of a program with poor performance where this was the cause. Poorly constructed SELECTs and nested LOOPs (on standard tables) are generally the cause.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Please see:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;lt;a href="/people/rob.burbank/blog/2006/11/16/performance--what-will-kill-you-and-what-will-leave-you-with-only-a-flesh-wound"&amp;gt;Performance - what will kill you and what will leave you with only a flesh wound&amp;lt;/a&amp;gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Fourthly, you don't need a loop to read all entries of a table. In&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;lt;a href="/people/rob.burbank/blog/2006/02/07/performance-of-nested-loops"&amp;gt;Performance of Nested Loops&amp;lt;/a&amp;gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I show one way of accessing all records using a binary search and indexed read on an internal table.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Rob&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 07 Aug 2007 14:07:40 GMT</pubDate>
    <dc:creator>Former Member</dc:creator>
    <dc:date>2007-08-07T14:07:40Z</dc:date>
    <item>
      <title>Nested loops</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/nested-loops/m-p/2634601#M606059</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Do nested loops ( till 2 levels ) also cause for performance issues ?&lt;/P&gt;&lt;P&gt;Please give me detailed info.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Binay.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 01 Aug 2007 14:54:09 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/nested-loops/m-p/2634601#M606059</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2007-08-01T14:54:09Z</dc:date>
    </item>
    <item>
      <title>Re: Nested loops</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/nested-loops/m-p/2634602#M606060</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;Yes, nested loops will give you the performance problem ..&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="http://www.erpgenie.com/abap/performance.htm#Nested%20selects" target="test_blank"&gt;http://www.erpgenie.com/abap/performance.htm#Nested%20selects&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt; Sudheer&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 01 Aug 2007 14:56:40 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/nested-loops/m-p/2634602#M606060</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2007-08-01T14:56:40Z</dc:date>
    </item>
    <item>
      <title>Re: Nested loops</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/nested-loops/m-p/2634603#M606061</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Absolutley - please check:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;lt;a href="/people/rob.burbank/blog/2006/02/07/performance-of-nested-loops"&amp;gt;Performance of Nested Loops&amp;lt;/a&amp;gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Rob&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 01 Aug 2007 14:58:02 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/nested-loops/m-p/2634603#M606061</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2007-08-01T14:58:02Z</dc:date>
    </item>
    <item>
      <title>Re: Nested loops</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/nested-loops/m-p/2634604#M606062</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Depends on what you do in the INNER LOOP.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you are calling a Function module or executing a DB statement then this will obviously have a bad effect on the performance.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Calling an FM is bad since it creats a HUGE stack of CALLING the Function modules.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;And DB statements are most precious in terms of performance you should make sure that they are out side the LOOP statement using better select statements and as few statements as possible.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;IF you just processing the internal tables your are looping then this should not have much effect on the performance.&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Sesh&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 01 Aug 2007 14:59:10 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/nested-loops/m-p/2634604#M606062</guid>
      <dc:creator>seshatalpasai_madala</dc:creator>
      <dc:date>2007-08-01T14:59:10Z</dc:date>
    </item>
    <item>
      <title>Re: Nested loops</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/nested-loops/m-p/2634605#M606063</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;it depends on the data available in the internal tables. if data is less using loop and binary search works well.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;if data is hughe in all internal tables nested loops might cause a problem but can be avoided using binary search and indexes...&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 01 Aug 2007 15:01:58 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/nested-loops/m-p/2634605#M606063</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2007-08-01T15:01:58Z</dc:date>
    </item>
    <item>
      <title>Re: Nested loops</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/nested-loops/m-p/2634606#M606064</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Sesh - the problem is more to do with the size of the internal tables rather than what is going on inside them. If you have two internal tables each with say 10,000 records and loop through them with a nested loop. It will take a lot of time even of there is absolutley no processing in the inner loop.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Nested loops can often be a worse problem than a poorly designed SELECT.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Rob&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 01 Aug 2007 15:05:06 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/nested-loops/m-p/2634606#M606064</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2007-08-01T15:05:06Z</dc:date>
    </item>
    <item>
      <title>Re: Nested loops</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/nested-loops/m-p/2634607#M606065</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Binay,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The parallel cursor is the one of the best and most efficient method in case of nested loops .&lt;/P&gt;&lt;P&gt;It works on the assumption that &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;loop at TAB1.&lt;/P&gt;&lt;P&gt;   loop atTAB2.&lt;/P&gt;&lt;P&gt;  end loop.&lt;/P&gt;&lt;P&gt;endloop.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt; TAB2 contains only entries also contained in TAB1. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hope it help you.&lt;/P&gt;&lt;P&gt;Let me know If you want to know how to use the parallel cursor algorithm.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Praveen&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 01 Aug 2007 17:55:06 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/nested-loops/m-p/2634607#M606065</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2007-08-01T17:55:06Z</dc:date>
    </item>
    <item>
      <title>Re: Nested loops</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/nested-loops/m-p/2634608#M606066</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;please not user nested loop.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;replace as for all entries.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 02 Aug 2007 05:41:15 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/nested-loops/m-p/2634608#M606066</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2007-08-02T05:41:15Z</dc:date>
    </item>
    <item>
      <title>Re: Nested loops</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/nested-loops/m-p/2634609#M606067</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Rob_&amp;gt; I just wanted to higlight that calling a SELECT in a LOOP is bad interms of performance. Since we only have on Database server per system and its a costly resource.&lt;/P&gt;&lt;P&gt;Application servers these days can handle more data and have more memory so I didnt empohsize on Size of the internal tables.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Any way I do agree SIZE does have its say on the perofrmance but what you do inside effects even more.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Sesh&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 02 Aug 2007 15:04:33 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/nested-loops/m-p/2634609#M606067</guid>
      <dc:creator>seshatalpasai_madala</dc:creator>
      <dc:date>2007-08-02T15:04:33Z</dc:date>
    </item>
    <item>
      <title>Re: Nested loops</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/nested-loops/m-p/2634610#M606068</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;OK Sesh. Here's an example of what I meant:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
REPORT ztest MESSAGE-ID 00.

TABLES: bkpf.

PARAMETERS: p_bukrs LIKE bkpf-bukrs,
            p_gjahr LIKE bkpf-gjahr.

DATA: BEGIN OF itab1 OCCURS 0,
        f1,
      END   OF itab1.

DATA: BEGIN OF itab2 OCCURS 0,
        f1,
      END   OF itab2.

DATA: start   TYPE i,
      end     TYPE i,
      dif     TYPE i,
      wa_bkpf TYPE bkpf.

* Get a single document
SELECT * FROM bkpf
  INTO wa_bkpf
  WHERE bukrs = p_bukrs
    AND gjahr = p_gjahr.
  EXIT.
ENDSELECT.

* Fill the internal tables
DO 10000 TIMES.
  APPEND INITIAL LINE TO: itab1, itab2.
ENDDO.

* SELECT within a LOOP
GET RUN TIME FIELD start.
LOOP AT itab1.
  SELECT SINGLE * FROM bkpf
    WHERE bukrs = wa_bkpf-bukrs
      AND belnr = wa_bkpf-belnr
      AND gjahr = wa_bkpf-gjahr.
ENDLOOP.
GET RUN TIME FIELD end.
dif = end - start.
WRITE: /001 'Time for SELECT in a loop:', dif, 'microseconds'.

* Nested LOOP
GET RUN TIME FIELD start.
LOOP AT itab1.
  LOOP AT itab2.
  ENDLOOP.
ENDLOOP.
GET RUN TIME FIELD end.
dif = end - start.
WRITE: /001 'Time for nested loops    :', dif, 'microseconds'.
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;When I ran this, the nested loops took about thirty times longer than the select within the loop. When I reduced the number of iterations from 10,000 to 1,000, it took about twice as long.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So I believe the size of the tables is the determining factor, not what's going on inside them.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Rob&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 02 Aug 2007 15:38:52 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/nested-loops/m-p/2634610#M606068</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2007-08-02T15:38:52Z</dc:date>
    </item>
    <item>
      <title>Re: Nested loops</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/nested-loops/m-p/2634611#M606069</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;OK, I haven't really investigated these issues myself, but I think your example is a bit poorly constructed. I am not saying that nested loops are a good thing, but if comparisons are going to be made they should be realistic and "fair".&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;1) Your nested loops performs 10.000**2 = 100 million "operations", while the selection only performs 10 thousand operations. How can you then compare the run time of the two? To have comparable operations you should read one (arbitrary?) line from ITAB2 in each iteration.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;2) You select the same BKPF dokument every time. That is not realistic, and it most likely means it will be in the buffer after the first select - so in reality you have one selection and N buffer accesses.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The reason I am commenting on this is that I don't want people to be left with the impression that it is better to select the documents to operate on inside a loop than to select all documents into two internal tables and process them in nested loops. Why do I not want that? Because I can't believe it is true. The overhead of multiple accesses to the database should ensure that the double loop is faster, and this difference would increase with the network lag (can be a factor in remote locations for central installations).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Message was edited by Kjetil Kilhavn:&lt;/P&gt;&lt;P&gt;Note: I am not taking memory constraints into consideration, as it was only nested loops that was the topic of the question - not overall performance. For large numbers of internal table entries it may actually be better for overall system performance to select inside each iteration to reduce memory usage.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Message was edited (again!) by Kjetil Kilhavn:&lt;/P&gt;&lt;P&gt;As has already been mentioned; the question is not as simple as "is nested loops a bad thing". It depends on what you do inside the loops, and it depends even more on what alternatives you have available. Sometimes nested loops are the best option, other times the worst.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hey, no-one said programming was easy, and the difficulty is how we justify our insane hour-rates anyway!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 02 Aug 2007 23:00:52 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/nested-loops/m-p/2634611#M606069</guid>
      <dc:creator>KjetilKilhavn</dc:creator>
      <dc:date>2007-08-02T23:00:52Z</dc:date>
    </item>
    <item>
      <title>Re: Nested loops</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/nested-loops/m-p/2634612#M606070</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have to say that I don't like most of the answers here, because many things are confused.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Generally nested loops are 2 connected loops over internal tables, I would not talk about selects in the beginning.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;loop at itab1 &lt;/P&gt;&lt;P&gt;   loop at itab2  or  read itab2 &lt;/P&gt;&lt;P&gt;   ... some other cheap operations&lt;/P&gt;&lt;P&gt;endloop&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;=&amp;gt; only if number of entries in itab1 = N1 and N2 (itab2) are large, lets say&lt;/P&gt;&lt;P&gt;     N1 * N2 &amp;lt; 10.000, performance becomes important.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Assumption: You never need to loop over itab2 completely for each line of itab1. In business transactions there is always a condition on itab2, as only one or&lt;/P&gt;&lt;P&gt;few corresponding entries of itab2 are needed for each line of itab1. &lt;/P&gt;&lt;P&gt;(Complete nested loops are only necessary in optimization problems)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;=&amp;gt; Complete nested loops would need a runtime proportional to  N1 * N2 &lt;/P&gt;&lt;P&gt;and you have no chance to change it&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;=&amp;gt; Our problems can be processed in runtimes proportional to N1 * log (N2) !&lt;/P&gt;&lt;P&gt;if you program carefully!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;so for&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;   loop at itab1 into wa1&lt;/P&gt;&lt;P&gt;       loop at itab2 ...  key = wa1-key.&lt;/P&gt;&lt;P&gt;       endloop.&lt;/P&gt;&lt;P&gt;   endloop.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;+ Dont use loop at itab2 where     because this is porportional to N2&lt;/P&gt;&lt;P&gt;+ Use a sorted table for itab2&lt;/P&gt;&lt;P&gt;+ Or&lt;/P&gt;&lt;P&gt;          Read itab2 ... binary search.&lt;/P&gt;&lt;P&gt;           tabix = sy-tabix&lt;/P&gt;&lt;P&gt;             loop at itab2 from tabix.&lt;/P&gt;&lt;P&gt;               if ( key ne wa1-key )&lt;/P&gt;&lt;P&gt;                 exit.&lt;/P&gt;&lt;P&gt;               endif.&lt;/P&gt;&lt;P&gt;                ....&lt;/P&gt;&lt;P&gt;               endloop.&lt;/P&gt;&lt;P&gt;                   &lt;/P&gt;&lt;P&gt;Maybe the inner operation needs exactly one line, then use fast reads&lt;/P&gt;&lt;P&gt;+ read on a sorted table&lt;/P&gt;&lt;P&gt;+ read from a standard table with binary search&lt;/P&gt;&lt;P&gt;+ read from hashed table&lt;/P&gt;&lt;P&gt;but not use the simple read from a standard table.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;=&amp;gt; Nested loops can produce problems, most of the problems in ABAP coding are related to that, but nearly all of this problems can be avoided, if you program carefully.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Siegfried&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Message was edited by: &lt;/P&gt;&lt;P&gt;        Siegfried Boes&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 03 Aug 2007 07:54:58 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/nested-loops/m-p/2634612#M606070</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2007-08-03T07:54:58Z</dc:date>
    </item>
    <item>
      <title>Re: Nested loops</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/nested-loops/m-p/2634613#M606071</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Kjetil:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;1) You prove the point I am trying to make. The size of the internal tables is the determining factor, not what goes on inside them. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;2) This isn't meant to be a realistic example, just one that is simple and demonstrates what I'm trying to say. But I do tske your point. Buffering will probably be aqn issue here.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I guess my point is - no matter how poorly a SELECT statement is constructed, a nested loop can be constructed that is even worse.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I don't think fairness counts in programming &lt;SPAN __jive_emoticon_name="wink"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Rob&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 03 Aug 2007 13:17:51 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/nested-loops/m-p/2634613#M606071</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2007-08-03T13:17:51Z</dc:date>
    </item>
    <item>
      <title>Re: Nested loops</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/nested-loops/m-p/2634614#M606072</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Size matters, we can agree on that. But that is not only relevant for internal tables. A poorly formulated selection on BSEG is much worse than a poorly formulated selection on T001. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;However, the example to illustrate your point doesn't really relate to the question. The question is: does 2 levels of nested loops cause a performance problem? I'll still claim the answer is: it depends.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Fairness doesn't count in programming I suppose (point taken &lt;SPAN __jive_emoticon_name="happy"&gt;&lt;/SPAN&gt;, but if you want to compare two things and declare a winner, the comparison should be fair as in comparing similar things. In my opinion you didn't. Well, of course I can compare Claudia Schiffer and George Bush and say I prefer a German over a religious fanatic any day, but what significance does that have to who is the best archer?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Anyway, there are basic rules for performance tuning, but performance tuning can not be done automatically by setting up rules. Hopefully we can agree on that, and that is what I meant by saying that sometimes nested loops are the best solution, and other times the worst. You have to look at the specific case, both the code and the environment.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;As was pointed out by Siegfried in his post, you usually do not execute N1 * N2 operations in a nested loop (although that can of course happen), usually you execute only N1 * n2 where n2 is some number smaller than N2. Sometimes you only execute N1 operations (identify only one line from N2), but I'd say those are a good candidate for getting rid of the nested loop - although that's not always possible.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Well, I think this thread turned into a pretty good discussion on the topic at least. That can't be a Bad Thing(tm).&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 03 Aug 2007 21:52:54 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/nested-loops/m-p/2634614#M606072</guid>
      <dc:creator>KjetilKilhavn</dc:creator>
      <dc:date>2007-08-03T21:52:54Z</dc:date>
    </item>
    <item>
      <title>Re: Nested loops</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/nested-loops/m-p/2634615#M606073</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Siegfried (and anyone else), my curiosity got me over this one.&lt;/P&gt;&lt;P&gt;&amp;gt; + Or&lt;/P&gt;&lt;P&gt;&amp;gt;           Read itab2 ... binary search.&lt;/P&gt;&lt;P&gt;&amp;gt;  tabix = sy-tabix&lt;/P&gt;&lt;P&gt;&amp;gt;             loop at itab2 from tabix.&lt;/P&gt;&lt;P&gt;&amp;gt;    if ( key ne wa1-key )&lt;/P&gt;&lt;P&gt;&amp;gt;               exit.&lt;/P&gt;&lt;P&gt;&amp;gt;  endif.&lt;/P&gt;&lt;P&gt;&amp;gt;                ....&lt;/P&gt;&lt;P&gt;&amp;gt; endloop.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Whenever I have a date-dependent buffer (internal table) and need to find the data valid at a certain date I simply use a&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;LOOP AT buffer 
     WHERE some criteria          AND
           bedga &amp;lt;= relevant_date AND
           endda &amp;gt;= relevant_date.
ENDLOOP.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The internal tables are always sorted - not necessarily TYPE SORTED TABLE, but entries are inserted in sorted order or the internal table is sorted after filling it. My loop will identify the (single) line that is relevant. Are you saying that I would have a faster lookup by using a READ ... BINARY SEARCH followed by a LOOP AT buffer FROM sy-tabix? Usually I do define sorted tables with unique keys. I have assumed that SAP would then quickly find the relevant entry by using a binary search to find the first entry from which the looping should start.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Not that this is where I have had performance problems, but I would be interested in knowing if anyone has studied this to find out if one approach is faster than the other when you need to loop at a subset of entries in a sorted table.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;[....]&lt;/P&gt;&lt;P&gt;And this one - that statement got me wondering if it really can be true!&lt;/P&gt;&lt;P&gt;&amp;gt; Nested loops in 3 levels are never necessary!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Can't recall with certainty, but I believe I have used (at least) 3 levels of nested loops in a JVA-report designed for reporting to authorities in Brazil. First loop at the purchase order level, then the invoice level, and finally the payments level - all this in order to find out how much money had been spend on national vs. foreign purchases - reported by payment date but depending on purchase order data for separation between national and foreign. Perhaps I need to call my previous employer and tell them I made a mistake &lt;SPAN __jive_emoticon_name="happy"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I know I could have restructured the code to use an internal table with duplicate data, but I don't think there is much performance to gain.&lt;/P&gt;&lt;UL&gt;&lt;LI level="1" type="ul"&gt;&lt;P&gt;the number of selections would be unchanged&lt;/P&gt;&lt;/LI&gt;&lt;LI level="1" type="ul"&gt;&lt;P&gt;the number of buffer lines iterated over would be the same&lt;/P&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 03 Aug 2007 22:10:00 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/nested-loops/m-p/2634615#M606073</guid>
      <dc:creator>KjetilKilhavn</dc:creator>
      <dc:date>2007-08-03T22:10:00Z</dc:date>
    </item>
    <item>
      <title>Re: Nested loops</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/nested-loops/m-p/2634616#M606074</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I dropped the comment on 3 levels, because it is possible to write performant 3 level nested loops.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;loop at ita1.&lt;/P&gt;&lt;P&gt;  loop at sort2 with key k1 = wa1-k1.&lt;/P&gt;&lt;P&gt;    loop at sort3 with key k1 = wa1-k1 k2 = wa2-k2.&lt;/P&gt;&lt;P&gt;  endloop&lt;/P&gt;&lt;P&gt; endloop&lt;/P&gt;&lt;P&gt;endloop.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;More important is the other point, a loop at where on a standard table, scans linearly over the whole table, which is slow. Using the binary search to get the index, the loop from index and the exit to leave the loop is much faster. As long as you don't use it, inside of another loop it will just a bug. But if used inside a loop,&lt;/P&gt;&lt;P&gt;it will become your major performance problem. I have detailed measurements. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Siegfried&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 06 Aug 2007 07:03:51 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/nested-loops/m-p/2634616#M606074</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2007-08-06T07:03:51Z</dc:date>
    </item>
    <item>
      <title>Re: Nested loops</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/nested-loops/m-p/2634617#M606075</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;Dont use nested loops.&lt;/P&gt;&lt;P&gt;Use read statements with in the loop.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Neslin,&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 07 Aug 2007 09:36:37 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/nested-loops/m-p/2634617#M606075</guid>
      <dc:creator>Neslinn</dc:creator>
      <dc:date>2007-08-07T09:36:37Z</dc:date>
    </item>
    <item>
      <title>Re: Nested loops</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/nested-loops/m-p/2634618#M606076</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&amp;gt; Dont use nested loops.&lt;/P&gt;&lt;P&gt;&amp;gt; Use read statements with in the loop.&lt;/P&gt;&lt;P&gt;this seems to be a quite common misunderstanding.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You can not use a read when you need a loop! And there is no problem with a nested loop if done correctly.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Siegfried&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 07 Aug 2007 10:11:43 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/nested-loops/m-p/2634618#M606076</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2007-08-07T10:11:43Z</dc:date>
    </item>
    <item>
      <title>Re: Nested loops</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/nested-loops/m-p/2634619#M606077</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Siegfried and Kjetil - at the risk of beating a dead horse:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;First of all, I have been assuming that we are dealing with standard tables only - not sorted. I prefer to use standard tables because they give me more flexibility. I can sort and resort them as needed.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Secondly, for standard tables, except in the most trivial cases, a binary search will be faster. For small tables, it may be not worth the programming effort.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thirdly, SELECTs in loops should be avoided, but I've never seen an example of a program with poor performance where this was the cause. Poorly constructed SELECTs and nested LOOPs (on standard tables) are generally the cause.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Please see:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;lt;a href="/people/rob.burbank/blog/2006/11/16/performance--what-will-kill-you-and-what-will-leave-you-with-only-a-flesh-wound"&amp;gt;Performance - what will kill you and what will leave you with only a flesh wound&amp;lt;/a&amp;gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Fourthly, you don't need a loop to read all entries of a table. In&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;lt;a href="/people/rob.burbank/blog/2006/02/07/performance-of-nested-loops"&amp;gt;Performance of Nested Loops&amp;lt;/a&amp;gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I show one way of accessing all records using a binary search and indexed read on an internal table.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Rob&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 07 Aug 2007 14:07:40 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/nested-loops/m-p/2634619#M606077</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2007-08-07T14:07:40Z</dc:date>
    </item>
    <item>
      <title>Re: Nested loops</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/nested-loops/m-p/2634620#M606078</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;hmm, &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I would recommend to use sorted and hashed tables, whenever possible. I know that there is slight advantage of the binary search, but that will never cause a problem.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Problems will be caused to extensive usage of sorts on internal tables, I have seen them inside of loops! The table used in the outer loop must often not be sorted!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Your example with read binary search while read from index is just a more complicated expression of the common solution&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;read  .... with key = x binary search &lt;/P&gt;&lt;P&gt;tabix = sy-index&lt;/P&gt;&lt;P&gt;loop at ... from tabix.&lt;/P&gt;&lt;P&gt;   if ( key ne x ).&lt;/P&gt;&lt;P&gt;     exit.&lt;/P&gt;&lt;P&gt;   endif&lt;/P&gt;&lt;P&gt;endif.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;i.e. there is no reason why nested loops should not be used. The workaround above is o.k., with a sorted table it works automatically.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;These are completely acceptable solutions. Only in highly critical problems solutions using parallel indexes are necessary.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If I find the time, then I will publish details as a weblog soon.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Siegfried&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 07 Aug 2007 14:28:42 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/nested-loops/m-p/2634620#M606078</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2007-08-07T14:28:42Z</dc:date>
    </item>
  </channel>
</rss>

