<?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: SQL Select performance in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select-performance/m-p/1278529#M152133</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;PRE&gt;&lt;CODE&gt;Do a check for existance of some records in itab_1. 
CHECK NOT itab_1[] IS INITIAL.

Sort the table itab_1 by fields used in the where clause.
SORT itab_1 BY reg_no.

Instead of using INTO CORRESPONDING, try using INTO if
your itab_2 has just the fields that you are selecting.

Use 'VAKPA' table instead of VBAK as this is an index
table. But try to add more key fields of this table in 
your where clause.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hope this helps,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Srinivas&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 22 Mar 2006 17:18:44 GMT</pubDate>
    <dc:creator>Former Member</dc:creator>
    <dc:date>2006-03-22T17:18:44Z</dc:date>
    <item>
      <title>SQL Select performance</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select-performance/m-p/1278523#M152127</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi everyone&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have written a program that includes the following SQL select statement:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;  SELECT vbeln erdat erzet auart kunnr&lt;/P&gt;&lt;P&gt;  FROM vbak&lt;/P&gt;&lt;P&gt;  INTO CORRESPONDING FIELDS OF TABLE itab_2&lt;/P&gt;&lt;P&gt;  FOR ALL ENTRIES IN itab_1&lt;/P&gt;&lt;P&gt;  WHERE kunnr = itab1-reg_no&lt;/P&gt;&lt;P&gt;    AND spart = '20'.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The program takes quite a while to run and some basic run-time analysis suggests that this statement is causing a bottleneck that accounts for about 99% of the run time. Is there anything I can do to make this bit of code more efficient?  I wondered about creating a new index for VBAK using kunnr and spart but am reluctant to go down that route due to the possilbe adverse consequences.  Any help/advise would be greatly appreciated.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Kind regards&lt;/P&gt;&lt;P&gt;Andy&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 22 Mar 2006 16:20:43 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select-performance/m-p/1278523#M152127</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-03-22T16:20:43Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Select performance</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select-performance/m-p/1278524#M152128</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Andrew,&lt;/P&gt;&lt;P&gt;  Definitely there will be effect if you are not considering the key fields in a select where condition.The next best thing is create an index as you mentioned &amp;amp; use it.I dont think it will have that much of a problem.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 22 Mar 2006 16:25:33 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select-performance/m-p/1278524#M152128</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-03-22T16:25:33Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Select performance</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select-performance/m-p/1278525#M152129</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;if you use "into corresponding fields of table",it takes a lot if time and increases database usage.&lt;/P&gt;&lt;P&gt;declare the fields in itab_2 also in the same order and instead of "into corresponding fields of table",use "into table itab_2". &lt;/P&gt;&lt;P&gt;in case there are other fields in itab_2 besides vbeln erdat erzet auart kunnr, then first declare another internal table ,say itab_3 with only vbeln erdat erzet auart kunnr fields in the same order and do the following:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;SELECT vbeln erdat erzet auart kunnr&lt;/P&gt;&lt;P&gt;FROM vbak&lt;/P&gt;&lt;P&gt;INTO TABLE itab_3&lt;/P&gt;&lt;P&gt;FOR ALL ENTRIES IN itab_1&lt;/P&gt;&lt;P&gt;WHERE kunnr = itab1-reg_no&lt;/P&gt;&lt;P&gt;AND spart = '20'.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;itab_2-vbeln = itab_3-vbeln.&lt;/P&gt;&lt;P&gt;itab_2-erdat = itab_3-erdat.&lt;/P&gt;&lt;P&gt;itab_2-erzet = itab_3-erzet.&lt;/P&gt;&lt;P&gt;itab_2-auart = itab_3-auart.&lt;/P&gt;&lt;P&gt;itab_2-kunnr = itab_3-kunnr.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;append itab_2.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt; You can also do one more enhancement. In the where clause, mention spart first and then mention kunnr since spart comes before according to the data dictionary order.&lt;/P&gt;&lt;P&gt;So, the where clause will be:&lt;/P&gt;&lt;P&gt;FOR ALL ENTRIES IN itab_1&lt;/P&gt;&lt;P&gt;WHERE spart = '20'&lt;/P&gt;&lt;P&gt;AND kunnr = itab1-reg_no.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hope this will be useful.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Divya&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 22 Mar 2006 16:35:09 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select-performance/m-p/1278525#M152129</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-03-22T16:35:09Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Select performance</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select-performance/m-p/1278526#M152130</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Andrew, &lt;/P&gt;&lt;P&gt; do not use kunnr and spart which are not key fields in the where clause..&lt;/P&gt;&lt;P&gt;when using FOR ALL ENTRIES ..&lt;/P&gt;&lt;P&gt;do the following check&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;if itab_1[] is not initial.

SELECT vbeln erdat erzet auart kunnr 
  FROM vbak
  INTO TABLE itab_2
  FOR ALL ENTRIES IN itab_1
  where &amp;lt;key field&amp;gt;.

LOOP AT itab_1.
  DELETE itab_2 where  kunnr NE itab1-reg_no
                  AND spart NE '20'.
ENDLOOP.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;this shud improve the performance..&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;regards&lt;/P&gt;&lt;P&gt;satesh&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 22 Mar 2006 16:45:19 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select-performance/m-p/1278526#M152130</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-03-22T16:45:19Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Select performance</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select-performance/m-p/1278527#M152131</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Divya&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks for this - however, these changes seem to increase the run-time rather than decrease it - very bizarre...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Kind regards&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Andrew&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 22 Mar 2006 17:00:40 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select-performance/m-p/1278527#M152131</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-03-22T17:00:40Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Select performance</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select-performance/m-p/1278528#M152132</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks Satesh&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I will have a proper look at this tomorrow (its time to go home here!) - The &amp;lt;key field&amp;gt; is presumably the key field of table VBAK in the dictionary i.e. VBELN.  However, I don't have data for the VBELN field in order to restrict the record set.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Kind regards&lt;/P&gt;&lt;P&gt;Andrew&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 22 Mar 2006 17:07:37 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select-performance/m-p/1278528#M152132</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-03-22T17:07:37Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Select performance</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select-performance/m-p/1278529#M152133</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;PRE&gt;&lt;CODE&gt;Do a check for existance of some records in itab_1. 
CHECK NOT itab_1[] IS INITIAL.

Sort the table itab_1 by fields used in the where clause.
SORT itab_1 BY reg_no.

Instead of using INTO CORRESPONDING, try using INTO if
your itab_2 has just the fields that you are selecting.

Use 'VAKPA' table instead of VBAK as this is an index
table. But try to add more key fields of this table in 
your where clause.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hope this helps,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Srinivas&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 22 Mar 2006 17:18:44 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select-performance/m-p/1278529#M152133</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-03-22T17:18:44Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Select performance</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select-performance/m-p/1278530#M152134</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Andrew,&lt;/P&gt;&lt;P&gt;   you can consider few options in optimizing this querry using &amp;lt;i&amp;gt;RANGES&amp;lt;/i&amp;gt;.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;here is the sample codes.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
RANGES : lr_regno FOR vbak-kunnr.         
  lr_regno-sign = 'I'.                             
  lr_regno-option = 'EQ'.
 *** similar ranges for the other two fields            
 
    LOOP AT itab_1 INTO wa. 
      lr_regno-low = wa-reg_no.   
      APPEND lr_regno.      
** entries for the other fields                 
    ENDLOOP.                                  
 
    SELECT vbeln erdat erzet auart kunnr
      from vbak
 appending table itab          
            WHERE kunnr IN lr_regno
              and spart = '20'.
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;   &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;you might find this code helpful specially when the no. of entries in itab1 are not too many.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;do let me know if you have any further queries.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;there is a similar post some time back where a problem of similar kind was discussed. Have a look at the following &lt;A class="jive_macro jive_macro_thread" href="https://community.sap.com/" __jive_macro_name="thread" modifiedtitle="true" __default_attr="123960"&gt;&lt;/A&gt;&lt;/P&gt;&lt;P&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;Kinshuk Saxena&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 22 Mar 2006 17:44:06 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select-performance/m-p/1278530#M152134</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-03-22T17:44:06Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Select performance</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select-performance/m-p/1278531#M152135</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;When I get a problem like this, I find it a good idea to go to something similar that does what I want. In this case, if you go to transaction VA03 and do F4 on the order, you'll find that there is a search help for orders by customer. If you have a performance trace on when you do the search, you can see that it looks at M_VMVAE. This is a view for the table that Srinivas mentioned VAKPA. Armed with this, you can change your program:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
DATA: BEGIN OF itab1a OCCURS 0,
        reg_no LIKE vbak-kunnr,
      END   OF itab1a.

DATA: BEGIN OF itab3 OCCURS 0,
        vbeln LIKE vbak-vbeln,
      END   OF itab3.

CHECK NOT itab1[] IS INITIAL.
LOOP AT itab1.
  itab1a-reg_no = itab1-reg_no.
  APPEND itab1a.
ENDLOOP.
SORT itab1a.
DELETE ADJACENT DUPLICATES FROM itab1a.

SELECT vbeln
  FROM m_vmvae
  INTO TABLE itab3
  FOR ALL ENTRIES IN itab1a
  WHERE kunde = itab1a-reg_no.

CHECK NOT itab3[] IS INITIAL.
SELECT vbeln erdat erzet auart kunnr
  FROM vbak
  INTO TABLE itab_2
  FOR ALL ENTRIES IN itab3
  WHERE vbeln = itab3-vbeln
  AND   spart = '20'.
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This should be much quicker.&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, 22 Mar 2006 23:01:32 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select-performance/m-p/1278531#M152135</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-03-22T23:01:32Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Select performance</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select-performance/m-p/1278532#M152136</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;HI Andrew,&lt;/P&gt;&lt;P&gt; you can also try creating indexes for the selection fields ..&lt;/P&gt;&lt;P&gt;Or in this try giving appropriate key fields which fall in your selection criteria..&lt;/P&gt;&lt;P&gt;it shud be a little faster than previous selection&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;itab_1[] is not initial.
  SELECT 
  vbeln erdat erzet auart kunnr 
   FROM vbak 
   APPENDING TABLE itab_2
   FOR ALL ENTRIES IN itab_1
   where &amp;lt;key field&amp;gt;.
   LOOP AT itab_1. 
   DELETE itab_2 where 
   kunnr NE itab1-reg_no AND spart NE '20'.
   ENDLOOP.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;regards&lt;/P&gt;&lt;P&gt;satesh&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 23 Mar 2006 01:16:02 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select-performance/m-p/1278532#M152136</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-03-23T01:16:02Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Select performance</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select-performance/m-p/1278533#M152137</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Wow, "much quicker" is an understatement! This approach has reduced the run time by a factor of about 100.  Thanks Rob and thanks to everyone else for your replies.&lt;/P&gt;&lt;P&gt;Kind regards&lt;/P&gt;&lt;P&gt;Andrew&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 23 Mar 2006 11:28:27 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select-performance/m-p/1278533#M152137</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-03-23T11:28:27Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Select performance</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select-performance/m-p/1278534#M152138</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Glad to help, Andrew. In performance tuning, the most important things to do are:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Use an index effectively when working with large database tables.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Avoid nested loops when working with large internal tables.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Other methods can improve performance as well, but I think these are the two to remember.&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, 23 Mar 2006 14:46:56 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select-performance/m-p/1278534#M152138</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-03-23T14:46:56Z</dc:date>
    </item>
  </channel>
</rss>

