<?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: Alternate for Loop within Loop for optimization in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/alternate-for-loop-within-loop-for-optimization/m-p/3614563#M870779</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;why are you posting your question twice?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A class="jive_macro jive_macro_thread" href="https://community.sap.com/" __jive_macro_name="thread" modifiedtitle="true" __default_attr="785288"&gt;&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I stick to my answer in that first thread, toss out i_po and i_eban, rather select all you need into i_prpo right away using a join select on EKKO, EKPO and EBAN (outer join on EBAN so you get those POs without a requisition as well).&lt;/P&gt;&lt;P&gt;Cure the cause, not the symptoms.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;over &amp;amp; out&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thomas&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 17 Mar 2008 15:36:39 GMT</pubDate>
    <dc:creator>ThomasZloch</dc:creator>
    <dc:date>2008-03-17T15:36:39Z</dc:date>
    <item>
      <title>Alternate for Loop within Loop for optimization</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/alternate-for-loop-within-loop-for-optimization/m-p/3614558#M870774</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Dear friends,&lt;/P&gt;&lt;P&gt;LOOP AT i_po.&lt;/P&gt;&lt;P&gt;        LOOP AT i_eban WHERE banfn = i_po-banfn&lt;/P&gt;&lt;P&gt;                        AND  bnfpo = i_po-bnfpo.&lt;/P&gt;&lt;P&gt;          i_prpo-matnr = i_eban-matnr.&lt;/P&gt;&lt;P&gt;          i_prpo-txz01 = i_eban-txz01.&lt;/P&gt;&lt;P&gt;          i_prpo-banfn = i_eban-banfn.&lt;/P&gt;&lt;P&gt;          i_prpo-bnfpo = i_eban-bnfpo.&lt;/P&gt;&lt;P&gt;          i_prpo-badat = i_eban-badat.&lt;/P&gt;&lt;P&gt;          i_prpo-menger = i_eban-menge.&lt;/P&gt;&lt;P&gt;          i_prpo-meinsr = i_eban-meins.&lt;/P&gt;&lt;P&gt;          i_prpo-ebeln = i_po-ebeln.&lt;/P&gt;&lt;P&gt;          i_prpo-ebelp = i_po-ebelp.&lt;/P&gt;&lt;P&gt;          i_prpo-bedat = i_po-bedat.&lt;/P&gt;&lt;P&gt;          i_prpo-menge = i_po-menge.&lt;/P&gt;&lt;P&gt;          i_prpo-lifnr = i_po-lifnr.&lt;/P&gt;&lt;P&gt;          APPEND i_prpo.&lt;/P&gt;&lt;P&gt;          CLEAR i_prpo.&lt;/P&gt;&lt;P&gt;        ENDLOOP.&lt;/P&gt;&lt;P&gt;        IF sy-subrc &amp;lt;&amp;gt; 0.&lt;/P&gt;&lt;UL&gt;&lt;LI level="1" type="ul"&gt;&lt;P&gt; only PO details&lt;/P&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;          i_prpo-ebeln = i_po-ebeln.&lt;/P&gt;&lt;P&gt;          i_prpo-ebelp = i_po-ebelp.&lt;/P&gt;&lt;P&gt;          i_prpo-bedat = i_po-bedat.&lt;/P&gt;&lt;P&gt;          i_prpo-menge = i_po-menge.&lt;/P&gt;&lt;P&gt;          i_prpo-lifnr = i_po-lifnr.&lt;/P&gt;&lt;P&gt;          APPEND i_prpo.&lt;/P&gt;&lt;P&gt;          CLEAR i_prpo.&lt;/P&gt;&lt;P&gt;        ENDIF.&lt;/P&gt;&lt;P&gt;      ENDLOOP.&lt;/P&gt;&lt;P&gt;is there a way as 2 how i could remove loop within loop and still get the same o/p..&lt;/P&gt;&lt;P&gt;Please guide me.&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Essam&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 17 Mar 2008 13:33:27 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/alternate-for-loop-within-loop-for-optimization/m-p/3614558#M870774</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2008-03-17T13:33:27Z</dc:date>
    </item>
    <item>
      <title>Re: Alternate for Loop within Loop for optimization</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/alternate-for-loop-within-loop-for-optimization/m-p/3614559#M870775</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Please see:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;[The Performance of Nested Loops|/people/rob.burbank/blog/2006/02/07/performance-of-nested-loops]&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Rob&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 17 Mar 2008 14:52:54 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/alternate-for-loop-within-loop-for-optimization/m-p/3614559#M870775</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2008-03-17T14:52:54Z</dc:date>
    </item>
    <item>
      <title>Re: Alternate for Loop within Loop for optimization</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/alternate-for-loop-within-loop-for-optimization/m-p/3614560#M870776</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi pal. Here are some steps that may help you:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;- Sort both your internal tables by the main keys that will link them. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;- Choose the main table (the straighter course for the data you want), in this case I guess it may be I_PO. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;- Loop the main table. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;- Move the desired values from the main table to the destiny. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;- Read the data on the secondary table using the key fields you sorted before. For that you can use the READ TABLE command. As you sorted the tables you can use the BINARY SEARCH clause, which will make it seek faster. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;- Move the desired values from the secondary table to the destiny. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hope it can help!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 17 Mar 2008 14:56:06 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/alternate-for-loop-within-loop-for-optimization/m-p/3614560#M870776</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2008-03-17T14:56:06Z</dc:date>
    </item>
    <item>
      <title>Re: Alternate for Loop within Loop for optimization</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/alternate-for-loop-within-loop-for-optimization/m-p/3614561#M870777</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello Essam&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The blog of Rob is a &lt;STRONG&gt;MUST READ&lt;/STRONG&gt; about this topic. Highly recommended.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;  Uwe&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 17 Mar 2008 15:10:48 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/alternate-for-loop-within-loop-for-optimization/m-p/3614561#M870777</guid>
      <dc:creator>uwe_schieferstein</dc:creator>
      <dc:date>2008-03-17T15:10:48Z</dc:date>
    </item>
    <item>
      <title>Re: Alternate for Loop within Loop for optimization</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/alternate-for-loop-within-loop-for-optimization/m-p/3614562#M870778</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Your coding is a wonderful example for quadratic coding, whcih is a big performance problem.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You must sort only the inner table, and optimize the access by&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt; 
loop
 
  read binary search,
  loop from index, 
  exit if condition is not fulfilled.

   endloop
endloop
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;See last section of this blog for details:&lt;/P&gt;&lt;P&gt;/people/siegfried.boes/blog/2007/09/12/runtimes-of-reads-and-loops-on-internal-tables&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If it helps read the rest of the blog &lt;SPAN __jive_emoticon_name="happy"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Use a sorted table as the inner table, then everything works automatically.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It makes no sense to sort the outer table!&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, 17 Mar 2008 15:27:17 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/alternate-for-loop-within-loop-for-optimization/m-p/3614562#M870778</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2008-03-17T15:27:17Z</dc:date>
    </item>
    <item>
      <title>Re: Alternate for Loop within Loop for optimization</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/alternate-for-loop-within-loop-for-optimization/m-p/3614563#M870779</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;why are you posting your question twice?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A class="jive_macro jive_macro_thread" href="https://community.sap.com/" __jive_macro_name="thread" modifiedtitle="true" __default_attr="785288"&gt;&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I stick to my answer in that first thread, toss out i_po and i_eban, rather select all you need into i_prpo right away using a join select on EKKO, EKPO and EBAN (outer join on EBAN so you get those POs without a requisition as well).&lt;/P&gt;&lt;P&gt;Cure the cause, not the symptoms.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;over &amp;amp; out&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thomas&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 17 Mar 2008 15:36:39 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/alternate-for-loop-within-loop-for-optimization/m-p/3614563#M870779</guid>
      <dc:creator>ThomasZloch</dc:creator>
      <dc:date>2008-03-17T15:36:39Z</dc:date>
    </item>
    <item>
      <title>Re: Alternate for Loop within Loop for optimization</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/alternate-for-loop-within-loop-for-optimization/m-p/3614564#M870780</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;Nested Loops - This is one of the fear factors for all the ABAP developers as this consumes lot of program execution time. If the number of entries in the internal tables is huge, then the situation would be too worse. The solution for this is to use parallel cursor method whenever there is a need for Nested Loop.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Program using Normal Nested Loop:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;



REPORT  ZNORMAL_NESTEDLOOP.

TABLES:
  likp,
  lips.

Data:
  t_likp  type table of likp,
  t_lips  type TABLE OF lips.

data:
  W_RUNTIME1 TYPE I,
  W_RUNTIME2 TYPE I.

START-OF-SELECTION.
select *
  from likp
  into table t_likp.

select *
  from lips
  into table t_lips.

get RUN TIME FIELD w_runtime1.

loop at t_likp into likp.
  loop at t_lips into lips where vbeln eq likp-vbeln.
  endloop.
endloop.

get RUN TIME FIELD w_runtime2.

w_runtime2 = w_runtime2 - w_runtime1.

write w_runtime2.
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Nested Loop using Parallel Cursor:&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;

REPORT  zparallel_cursor2.

TABLES:
  likp,
  lips.

DATA:
  t_likp  TYPE TABLE OF likp,
  t_lips  TYPE TABLE OF lips.

DATA:
  w_runtime1 TYPE i,
  w_runtime2 TYPE i,
  w_index LIKE sy-index.

START-OF-SELECTION.
  SELECT *
    FROM likp
    INTO TABLE t_likp.

  SELECT *
    FROM lips
    INTO TABLE t_lips.

  GET RUN TIME FIELD w_runtime1.
  SORT t_likp BY vbeln.
  SORT t_lips BY vbeln.

  LOOP AT t_likp INTO likp.

    LOOP AT t_lips INTO lips FROM w_index.
      IF likp-vbeln NE lips-vbeln.
        w_index = sy-tabix.
        EXIT.
      ENDIF.
    ENDLOOP.
  ENDLOOP.

  GET RUN TIME FIELD w_runtime2.

  w_runtime2 = w_runtime2 - w_runtime1.

  WRITE w_runtime2.




&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
Iteration No | Normal Nested Loop  | Using Parallel Cursor  
1.               | 34,796,147               | 63,829  
2.               | 38,534,583               | 56,894  
3.               |34,103,426                | 50,510  

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Hope this helps, Do reward.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Edited by: Runal Singh on Mar 18, 2008 12:39 PM&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 18 Mar 2008 07:09:18 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/alternate-for-loop-within-loop-for-optimization/m-p/3614564#M870780</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2008-03-18T07:09:18Z</dc:date>
    </item>
    <item>
      <title>Re: Alternate for Loop within Loop for optimization</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/alternate-for-loop-within-loop-for-optimization/m-p/3614565#M870781</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&amp;gt; Nested Loops - This is one of the fear factors for all the ABAP developers as this consumes lot of program &lt;/P&gt;&lt;P&gt;&amp;gt; execution time. If the number of entries in the internal tables is huge, then the situation would be too worse.&lt;/P&gt;&lt;P&gt;&amp;gt; The solution for this is to use parallel cursor method whenever there is a need for Nested Loop.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This is incorrect!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;A processing with sorted or hashed tables as inner tables is perfectly o.k. and the recommended&lt;/P&gt;&lt;P&gt;solution.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For standard tables you need a workaround as mentioned above (Find starting point with binary search, loop from index,&lt;/P&gt;&lt;P&gt;and stop loop as soon as condition is not fulfilled).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The parallel cursor is in general much to complictated, and actually only slightly faster. People think that is much faster, but they&lt;/P&gt;&lt;P&gt;that this solution requires a sort for both tables. In all other solutions it is not necessary to0 sort the other table.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;And Thomas is of course right. If there is a possiblity to select the data in an effective join, then the nested loop should be&lt;/P&gt;&lt;P&gt;avoided. However, there are of course many nested loop tasks which can not be avoided.&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, 18 Mar 2008 09:30:35 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/alternate-for-loop-within-loop-for-optimization/m-p/3614565#M870781</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2008-03-18T09:30:35Z</dc:date>
    </item>
    <item>
      <title>Re: Alternate for Loop within Loop for optimization</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/alternate-for-loop-within-loop-for-optimization/m-p/3614566#M870782</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello Essam,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;if the data amount for the give sample causes a performance issue, then it is very likey a memory issue too. From the basic logic it seems to do a kind of inner join on 2 internal tables of &lt;STRONG&gt;n&lt;/STRONG&gt; and &lt;STRONG&gt;m&lt;/STRONG&gt; entries into table with &lt;STRONG&gt;n&lt;/STRONG&gt; * &lt;STRONG&gt;m&lt;/STRONG&gt; entries.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Depending on the logic afterwards one may consider a redesign of the data structures. For sample to have one structure with 2 tables which have some kind manual link (e.g. a column index with the table index of the other table).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;  Klaus&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 18 Mar 2008 20:13:21 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/alternate-for-loop-within-loop-for-optimization/m-p/3614566#M870782</guid>
      <dc:creator>former_member183804</dc:creator>
      <dc:date>2008-03-18T20:13:21Z</dc:date>
    </item>
  </channel>
</rss>

