<?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 select in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/nested-select/m-p/1282783#M153170</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The avoidance of nested selects, while desirable, is far from the most important thing to consider when tuning. Consider the following example:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
REPORT ztest LINE-SIZE 80 MESSAGE-ID 00.

TABLES: bkpf, bseg.

DATA: BEGIN OF bkpf_int OCCURS 0.
        INCLUDE STRUCTURE bkpf.
DATA: END   OF bkpf_int.

DATA: BEGIN OF bseg_int OCCURS 0.
        INCLUDE STRUCTURE bseg.
DATA: END   OF bseg_int.

DATA: start TYPE i,
      end   TYPE i,
      dif   TYPE i.

SELECT-OPTIONS: s_bukrs FOR bkpf-bukrs,
                s_belnr FOR bkpf-belnr,
                s_gjahr FOR bkpf-gjahr.

SELECT * FROM bkpf
  WHERE bukrs IN s_bukrs
    AND belnr IN s_belnr
    AND gjahr IN s_gjahr.
  SELECT * FROM bseg
    WHERE bukrs = bkpf-bukrs
      AND belnr = bkpf-belnr
      AND gjahr = bkpf-gjahr.
  ENDSELECT.
ENDSELECT.

SELECT * FROM bkpf
INTO TABLE bkpf_int
  WHERE bukrs IN s_bukrs
    AND belnr IN s_belnr
    AND gjahr IN s_gjahr.

CHECK NOT bkpf_int[] IS INITIAL.
SELECT * FROM bseg
  FOR ALL ENTRIES IN bkpf_int
  WHERE bukrs = bkpf_int-bukrs
    AND belnr = bkpf_int-belnr
    AND gjahr = bkpf_int-gjahr.
ENDSELECT.

GET RUN TIME FIELD start.
SELECT * FROM bkpf
  WHERE bukrs IN s_bukrs
    AND belnr IN s_belnr
    AND gjahr IN s_gjahr.
  SELECT * FROM bseg
    WHERE bukrs = bkpf-bukrs
      AND belnr = bkpf-belnr
      AND gjahr = bkpf-gjahr.
  ENDSELECT.
ENDSELECT.
GET RUN TIME FIELD end.
dif = end - start.
WRITE: /001 'Time for nested select', dif, 'microseconds'.

REFRESH bkpf_int.
GET RUN TIME FIELD start.
SELECT * FROM bkpf
INTO TABLE bkpf_int
  WHERE bukrs IN s_bukrs
    AND belnr IN s_belnr
    AND gjahr IN s_gjahr.
SELECT * FROM bseg
  FOR ALL ENTRIES IN bkpf_int
  WHERE bukrs = bkpf_int-bukrs
    AND belnr = bkpf_int-belnr
    AND gjahr = bkpf_int-gjahr.
ENDSELECT.
GET RUN TIME FIELD end.
dif = end - start.
WRITE: /001 'Time for all entries select', dif, 'microseconds'.
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;When I ran the above code, the 'for all entries' select saved about 25% over the nested select.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you have a report that has performance problems, nobody is gong to thank you for saving 1/4 of the execution time.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In my experience, there are only two techniques that drastically improve performance:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;1) Make sure you use an index effectively in select statements.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;2) Avoid nested loops over large tables.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Either of these two techniques can cut run times by a factor of 20 or 30; much better than 1/4.&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, 14 Apr 2006 16:53:53 GMT</pubDate>
    <dc:creator>Former Member</dc:creator>
    <dc:date>2006-04-14T16:53:53Z</dc:date>
    <item>
      <title>Nested select</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/nested-select/m-p/1282777#M153164</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;One more thing&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;jus read this&lt;/P&gt;&lt;P&gt;'Nested select should be avoided and used only as a last resort when no other data retrieval method is possible'&lt;/P&gt;&lt;P&gt;can anybody plz give me the example where we can be forced to use nested select statements??&lt;/P&gt;&lt;P&gt;Thanks once again&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 14 Apr 2006 06:10:01 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/nested-select/m-p/1282777#M153164</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-04-14T06:10:01Z</dc:date>
    </item>
    <item>
      <title>Re: Nested select</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/nested-select/m-p/1282778#M153165</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Rashmi,&lt;/P&gt;&lt;P&gt;   ABAP programming has eveolved to such an extent that there will be no practical example to demonstrate the inevitable usage of nested selects.&lt;/P&gt;&lt;P&gt;Any code using a nested select can be converted into a better code(In terms of efficiency and performance).&lt;/P&gt;&lt;P&gt;We can tell you how to avoid a nested select if you want to.&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Ravi&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 14 Apr 2006 06:12:58 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/nested-select/m-p/1282778#M153165</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-04-14T06:12:58Z</dc:date>
    </item>
    <item>
      <title>Re: Nested select</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/nested-select/m-p/1282779#M153166</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Rashmi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Ravi is absoultely right, In the current situation coding can be done in such a way that NESTED SELECTS are avoided all the time.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Ravi&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 14 Apr 2006 06:16:04 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/nested-select/m-p/1282779#M153166</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-04-14T06:16:04Z</dc:date>
    </item>
    <item>
      <title>Re: Nested select</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/nested-select/m-p/1282780#M153167</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;IN Other words, instead of using nested selects&lt;/P&gt;&lt;P&gt;you can use select into table option,&lt;/P&gt;&lt;P&gt;and later use nested loop statements which do not hit the database layer and much faster than nested selects.&lt;/P&gt;&lt;P&gt;Eg:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;loop at itab.&lt;/P&gt;&lt;P&gt;select *&lt;/P&gt;&lt;P&gt;  from mara&lt;/P&gt;&lt;P&gt;  into mara&lt;/P&gt;&lt;P&gt; where matnr = itab-matnr.&lt;/P&gt;&lt;P&gt;select *&lt;/P&gt;&lt;P&gt;  from makt&lt;/P&gt;&lt;P&gt;  into makt&lt;/P&gt;&lt;P&gt; where matnr = mara-matnr.&lt;/P&gt;&lt;P&gt;endselect.&lt;/P&gt;&lt;P&gt;endselect.&lt;/P&gt;&lt;P&gt;endloop.&lt;/P&gt;&lt;P&gt;can be changed to&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;if not itab[] is initial.&lt;/P&gt;&lt;P&gt;select *&lt;/P&gt;&lt;P&gt;  from mara&lt;/P&gt;&lt;P&gt;  into table it_mara&lt;/P&gt;&lt;P&gt;  for all entries in itab&lt;/P&gt;&lt;P&gt; where matnr = itab-matnr.&lt;/P&gt;&lt;P&gt;if not it_mara[] is initial.&lt;/P&gt;&lt;P&gt;select *&lt;/P&gt;&lt;P&gt;  from makt&lt;/P&gt;&lt;P&gt;  into table it_makt&lt;/P&gt;&lt;P&gt;for all entries in it_mara&lt;/P&gt;&lt;P&gt; where matnr = it_mara-matnr.&lt;/P&gt;&lt;P&gt;endif.&lt;/P&gt;&lt;P&gt;endif.&lt;/P&gt;&lt;P&gt;REgards,&lt;/P&gt;&lt;P&gt;Ravi&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 14 Apr 2006 06:18:30 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/nested-select/m-p/1282780#M153167</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-04-14T06:18:30Z</dc:date>
    </item>
    <item>
      <title>Re: Nested select</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/nested-select/m-p/1282781#M153168</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks guyz&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 14 Apr 2006 06:18:53 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/nested-select/m-p/1282781#M153168</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-04-14T06:18:53Z</dc:date>
    </item>
    <item>
      <title>Re: Nested select</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/nested-select/m-p/1282782#M153169</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Call Transaction SE30 and click on Tips&amp;amp;Tricks, there are examples and expkanationas and also the possibility to compare the runtime of codings.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Matthias&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 14 Apr 2006 08:31:42 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/nested-select/m-p/1282782#M153169</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-04-14T08:31:42Z</dc:date>
    </item>
    <item>
      <title>Re: Nested select</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/nested-select/m-p/1282783#M153170</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The avoidance of nested selects, while desirable, is far from the most important thing to consider when tuning. Consider the following example:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
REPORT ztest LINE-SIZE 80 MESSAGE-ID 00.

TABLES: bkpf, bseg.

DATA: BEGIN OF bkpf_int OCCURS 0.
        INCLUDE STRUCTURE bkpf.
DATA: END   OF bkpf_int.

DATA: BEGIN OF bseg_int OCCURS 0.
        INCLUDE STRUCTURE bseg.
DATA: END   OF bseg_int.

DATA: start TYPE i,
      end   TYPE i,
      dif   TYPE i.

SELECT-OPTIONS: s_bukrs FOR bkpf-bukrs,
                s_belnr FOR bkpf-belnr,
                s_gjahr FOR bkpf-gjahr.

SELECT * FROM bkpf
  WHERE bukrs IN s_bukrs
    AND belnr IN s_belnr
    AND gjahr IN s_gjahr.
  SELECT * FROM bseg
    WHERE bukrs = bkpf-bukrs
      AND belnr = bkpf-belnr
      AND gjahr = bkpf-gjahr.
  ENDSELECT.
ENDSELECT.

SELECT * FROM bkpf
INTO TABLE bkpf_int
  WHERE bukrs IN s_bukrs
    AND belnr IN s_belnr
    AND gjahr IN s_gjahr.

CHECK NOT bkpf_int[] IS INITIAL.
SELECT * FROM bseg
  FOR ALL ENTRIES IN bkpf_int
  WHERE bukrs = bkpf_int-bukrs
    AND belnr = bkpf_int-belnr
    AND gjahr = bkpf_int-gjahr.
ENDSELECT.

GET RUN TIME FIELD start.
SELECT * FROM bkpf
  WHERE bukrs IN s_bukrs
    AND belnr IN s_belnr
    AND gjahr IN s_gjahr.
  SELECT * FROM bseg
    WHERE bukrs = bkpf-bukrs
      AND belnr = bkpf-belnr
      AND gjahr = bkpf-gjahr.
  ENDSELECT.
ENDSELECT.
GET RUN TIME FIELD end.
dif = end - start.
WRITE: /001 'Time for nested select', dif, 'microseconds'.

REFRESH bkpf_int.
GET RUN TIME FIELD start.
SELECT * FROM bkpf
INTO TABLE bkpf_int
  WHERE bukrs IN s_bukrs
    AND belnr IN s_belnr
    AND gjahr IN s_gjahr.
SELECT * FROM bseg
  FOR ALL ENTRIES IN bkpf_int
  WHERE bukrs = bkpf_int-bukrs
    AND belnr = bkpf_int-belnr
    AND gjahr = bkpf_int-gjahr.
ENDSELECT.
GET RUN TIME FIELD end.
dif = end - start.
WRITE: /001 'Time for all entries select', dif, 'microseconds'.
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;When I ran the above code, the 'for all entries' select saved about 25% over the nested select.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you have a report that has performance problems, nobody is gong to thank you for saving 1/4 of the execution time.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In my experience, there are only two techniques that drastically improve performance:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;1) Make sure you use an index effectively in select statements.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;2) Avoid nested loops over large tables.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Either of these two techniques can cut run times by a factor of 20 or 30; much better than 1/4.&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, 14 Apr 2006 16:53:53 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/nested-select/m-p/1282783#M153170</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-04-14T16:53:53Z</dc:date>
    </item>
    <item>
      <title>Re: Nested select</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/nested-select/m-p/1282784#M153171</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks Rob&lt;/P&gt;&lt;P&gt;I got it&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 15 Apr 2006 06:29:46 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/nested-select/m-p/1282784#M153171</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-04-15T06:29:46Z</dc:date>
    </item>
  </channel>
</rss>

