<?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: JOIN VBAK-VBAP - performance poor in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/join-vbak-vbap-performance-poor/m-p/1439224#M209097</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Andrea&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;A lot of the fields in your Where clause are part of the primary key in VAPMA, which is a sales index table.  If you have a look at it in the ABAP dictionary I would be inclined to try using three select statements.  The first would select just VBELN and POSNR from VAPMA based on as many of the conditions that apply to that table &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;SELECT vbeln posnr&lt;/P&gt;&lt;P&gt;FROM vapma&lt;/P&gt;&lt;P&gt;INTO CORRESPONDING FIELDS OF TABLE i_vapma&lt;/P&gt;&lt;P&gt;WHERE matnr IN r_matnrnf1&lt;/P&gt;&lt;P&gt;AND vkorg = c_vkorg&lt;/P&gt;&lt;P&gt;etc etc&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Make sure that the order the fields appear in your WHERE clause are the same as in VAPMA.  Also avoid using criteria that do not appear in the primary key.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Then write two more select statements collectin data from VBAK and VBAP:&lt;/P&gt;&lt;P&gt;CHECK NOT i_vapma IS INITIAL.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;SELECT vbeln vdatu bname&lt;/P&gt;&lt;P&gt;FROM vbak&lt;/P&gt;&lt;P&gt;INTO CORRESPONDING FIELDS OF TABLE i_vbak&lt;/P&gt;&lt;P&gt;FOR ALL ENTRIES IN i_vapma&lt;/P&gt;&lt;P&gt;WHERE vbeln = i_vapma-vbeln.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;SELECT vbeln posnr werks matnr kwmeng&lt;/P&gt;&lt;P&gt;FROM vbap&lt;/P&gt;&lt;P&gt;INTO CORRESPONDING FIELDS OF TABLE i_vbap&lt;/P&gt;&lt;P&gt;FOR ALL ENTRIES IN i_vapma&lt;/P&gt;&lt;P&gt;WHERE vbeln = i_vapma-vbeln&lt;/P&gt;&lt;P&gt;AND posnr = i_vapma-vbap.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You can then loop through i_vbak and read records from vbap into a combined table:&lt;/P&gt;&lt;P&gt;LOOP AT i_vbak INTO wa_vbak.&lt;/P&gt;&lt;P&gt;  IF wa_vbak-vdatu IN r_vdatu AND bname = space.&lt;/P&gt;&lt;P&gt;    READ TABLE i_vbap WITH KEY vbeln = i_vbak-vbeln&lt;/P&gt;&lt;P&gt;                               posnr - i_vbak-posnr&lt;/P&gt;&lt;P&gt;                      INTO wa_vbap.&lt;/P&gt;&lt;P&gt;    IF i_vbap-werks IN r_dfl.&lt;/P&gt;&lt;P&gt;      ...&lt;/P&gt;&lt;P&gt;    ENDIF.&lt;/P&gt;&lt;P&gt;  ENDIF.&lt;/P&gt;&lt;P&gt;ENDLOOP.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;WHere ... = code to copy all of the various data from wa_vbak and wa_vbap to an itab containing the relevant combined fields.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I've not tested this so be a bit careful but I think the logic is sound in that you are using an index to find the primary key fields for VBAK and VBAP (i.e. VBELN and POSNR).  Then you can select from VBAK and VBAP using their primary keys which will be &amp;lt;b&amp;gt;much&amp;lt;/b&amp;gt; quicker than what you were trying.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Generally, you should avoid Joins as they can be very slow, it is &amp;lt;b&amp;gt;normally&amp;lt;/b&amp;gt; better to do multiple select...for all entries and then combine the data.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I hope this is of some help.&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>Fri, 23 Jun 2006 13:56:42 GMT</pubDate>
    <dc:creator>Former Member</dc:creator>
    <dc:date>2006-06-23T13:56:42Z</dc:date>
    <item>
      <title>JOIN VBAK-VBAP - performance poor</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/join-vbak-vbap-performance-poor/m-p/1439213#M209086</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi, i have some problems with this select. Perfomance are very poor&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;SELECT p&lt;SUB&gt;vbeln p&lt;/SUB&gt;posnr t&lt;SUB&gt;vdatu p&lt;/SUB&gt;werks p~matnr&lt;/P&gt;&lt;P&gt;         p~kwmeng&lt;/P&gt;&lt;P&gt;         INTO CORRESPONDING FIELDS OF TABLE i_vbap&lt;/P&gt;&lt;P&gt;          FROM vbak AS t INNER JOIN vbap AS p&lt;/P&gt;&lt;P&gt;          ON    t&lt;SUB&gt;vbeln   = p&lt;/SUB&gt;vbeln&lt;/P&gt;&lt;P&gt;          WHERE t~vkorg   = c_vkorg&lt;/P&gt;&lt;P&gt;          AND   t~vtweg   = c_vtweg&lt;/P&gt;&lt;P&gt;          AND   t~spart   = c_spart&lt;/P&gt;&lt;P&gt;          AND   ( t~auart = 'tip' OR&lt;/P&gt;&lt;P&gt;                t~auart  = 'tap')&lt;/P&gt;&lt;P&gt;          AND   t~vdatu  IN r_vdatu &lt;/P&gt;&lt;P&gt;          AND   p~matnr  IN r_matnrnf1&lt;/P&gt;&lt;P&gt;          AND   p~werks  IN r_dfl&lt;/P&gt;&lt;P&gt;          AND   p~spart  NE 'F1'&lt;/P&gt;&lt;P&gt;          AND   t~vkgrp  IN r_vkgrp&lt;/P&gt;&lt;P&gt;          AND   t~bname = space.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In r_vdatu Orders are about 60000 and positions are 80 per order.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Is it possibile to improve perfomance?. Do you know an another way to do this select?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you for attention.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Bye&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 23 Jun 2006 09:56:21 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/join-vbak-vbap-performance-poor/m-p/1439213#M209086</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-06-23T09:56:21Z</dc:date>
    </item>
    <item>
      <title>Re: JOIN VBAK-VBAP - performance poor</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/join-vbak-vbap-performance-poor/m-p/1439214#M209087</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Declare the i_vbap with the order of fields same as the order in which they are selected and you can remove the into correspoding field statement which is very costly.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;SELECT p~vbeln &lt;/P&gt;&lt;P&gt;       p~posnr &lt;/P&gt;&lt;P&gt;       p~werks &lt;/P&gt;&lt;P&gt;       p~matnr&lt;/P&gt;&lt;P&gt;       p~kwmeng&lt;/P&gt;&lt;P&gt;       t~vdatu &lt;/P&gt;&lt;P&gt;  INTO TABLE i_vbap&lt;/P&gt;&lt;P&gt;  FROM vbak AS t INNER JOIN vbap AS p&lt;/P&gt;&lt;P&gt;  ON t&lt;SUB&gt;vbeln = p&lt;/SUB&gt;vbeln&lt;/P&gt;&lt;P&gt;WHERE t~vkorg = c_vkorg&lt;/P&gt;&lt;P&gt;AND t~vtweg = c_vtweg&lt;/P&gt;&lt;P&gt;AND t~spart = c_spart&lt;/P&gt;&lt;P&gt;AND ( t~auart = 'tip' OR&lt;/P&gt;&lt;P&gt;t~auart = 'tap')&lt;/P&gt;&lt;P&gt;AND t~vdatu IN r_vdatu &lt;/P&gt;&lt;P&gt;AND p~matnr IN r_matnrnf1&lt;/P&gt;&lt;P&gt;AND p~werks IN r_dfl&lt;/P&gt;&lt;P&gt;AND p~spart NE 'F1'&lt;/P&gt;&lt;P&gt;AND t~vkgrp IN r_vkgrp&lt;/P&gt;&lt;P&gt;AND t~bname = space.&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, 23 Jun 2006 10:00:42 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/join-vbak-vbap-performance-poor/m-p/1439214#M209087</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-06-23T10:00:42Z</dc:date>
    </item>
    <item>
      <title>Re: JOIN VBAK-VBAP - performance poor</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/join-vbak-vbap-performance-poor/m-p/1439215#M209088</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello Andrea,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Yuo can probably split up the two select statements like this - &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;SELECT vbeln 
  FROM vbak 
  INTO table lt_vbak
 WHERE vkorg = c_vkorg
   AND vtweg = c_vtweg
   AND spart = c_spart
   AND ( auart = 'tip' OR auart = 'tap')
   AND vdatu IN r_vdatu 
   AND bname = space.

SELECT vbeln posnr werks matnr kwmeng
  FROM vbap
  INTO table i_vbap
  FOR ALL ENTRIES IN lt_vbak
WHERE vbeln = lt_vba-vbeln
  AND matnr IN r_matnrnf1
  AND werks IN r_dfl
  AND spart NE 'F1'.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Since both the tables VBAK and VBAP are quite huge, a Join on these tables may not be desirable from the performance point of view.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Anand Mandalika.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 23 Jun 2006 10:04:43 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/join-vbak-vbap-performance-poor/m-p/1439215#M209088</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-06-23T10:04:43Z</dc:date>
    </item>
    <item>
      <title>Re: JOIN VBAK-VBAP - performance poor</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/join-vbak-vbap-performance-poor/m-p/1439216#M209089</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;along with the above suggestion..&lt;/P&gt;&lt;P&gt;Remove some unnecessary where conditions..,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;after select use delete itab where &amp;lt;condition&amp;gt;.&lt;/P&gt;&lt;P&gt;some thing like this..&lt;/P&gt;&lt;P&gt;&amp;lt;b&amp;gt;delete itab where vbeln not in s_vbeln.&amp;lt;/b&amp;gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;regards&lt;/P&gt;&lt;P&gt;vijay&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 23 Jun 2006 10:05:58 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/join-vbak-vbap-performance-poor/m-p/1439216#M209089</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-06-23T10:05:58Z</dc:date>
    </item>
    <item>
      <title>Re: JOIN VBAK-VBAP - performance poor</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/join-vbak-vbap-performance-poor/m-p/1439217#M209090</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;You would use a view which has the fields of both the tables which you require and then query it for specific records&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;e.g views WB2_V_VBAK_VBAP2&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Sameena&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Message was edited by: sameena attarwala&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 23 Jun 2006 10:07:12 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/join-vbak-vbap-performance-poor/m-p/1439217#M209090</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-06-23T10:07:12Z</dc:date>
    </item>
    <item>
      <title>Re: JOIN VBAK-VBAP - performance poor</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/join-vbak-vbap-performance-poor/m-p/1439218#M209091</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 are using for all entries then don't forget to check the initial condition.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;SELECT vbeln 
  FROM vbak 
  INTO table lt_vbak
 WHERE vkorg = c_vkorg
   AND vtweg = c_vtweg
   AND spart = c_spart
   AND ( auart = 'tip' OR auart = 'tap')
   AND vdatu IN r_vdatu 
   AND bname = space.
&amp;lt;b&amp;gt;if not lt_vbak[] is initial.&amp;lt;/b&amp;gt;
SELECT vbeln posnr werks matnr kwmeng
  FROM vbap
  INTO table i_vbap
  FOR ALL ENTRIES IN lt_vbak
WHERE vbeln = lt_vba-vbeln
  AND matnr IN r_matnrnf1
  AND werks IN r_dfl
  AND spart NE 'F1'.
&amp;lt;b&amp;gt;endif.&amp;lt;/b&amp;gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;vijay&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 23 Jun 2006 10:08:15 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/join-vbak-vbap-performance-poor/m-p/1439218#M209091</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-06-23T10:08:15Z</dc:date>
    </item>
    <item>
      <title>Re: JOIN VBAK-VBAP - performance poor</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/join-vbak-vbap-performance-poor/m-p/1439219#M209092</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;one more suggestion for performance improvement.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;in the WHERE Condition of SELECT the fields you are giving should be in the same order in the tables. then only you will get results faster.&lt;/P&gt;&lt;P&gt;in your case,&lt;/P&gt;&lt;P&gt; VBAK TABLE,&lt;/P&gt;&lt;P&gt;    VBELN,&lt;/P&gt;&lt;P&gt;    AUART,&lt;/P&gt;&lt;P&gt;    VKORG,&lt;/P&gt;&lt;P&gt; ETC.. IS There in the table.but in your SELECT you given VKORG,VTWEG before AUART. which you have to change.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;regards&lt;/P&gt;&lt;P&gt;srikanth&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 23 Jun 2006 10:14:06 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/join-vbak-vbap-performance-poor/m-p/1439219#M209092</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-06-23T10:14:06Z</dc:date>
    </item>
    <item>
      <title>Re: JOIN VBAK-VBAP - performance poor</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/join-vbak-vbap-performance-poor/m-p/1439220#M209093</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;to improve performance, you should keep in mind following points.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;1. the order of the fields of internal table should be the same as in select statement.&lt;/P&gt;&lt;P&gt;2. avoid using joins.instead use two select statements.&lt;/P&gt;&lt;P&gt;3. check the condition if the first internal table has some values then only select data into the other internal tables(that data depends on the data selected in first internal table)&lt;/P&gt;&lt;P&gt;4. in the where condition, the fields should be in the same order in the tables&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hope this helps you.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Richa&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 23 Jun 2006 10:20:29 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/join-vbak-vbap-performance-poor/m-p/1439220#M209093</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-06-23T10:20:29Z</dc:date>
    </item>
    <item>
      <title>Re: JOIN VBAK-VBAP - performance poor</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/join-vbak-vbap-performance-poor/m-p/1439221#M209094</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;You can use indexes on the tables in the given sequences.&lt;/P&gt;&lt;P&gt;If you dont have the key values, pass the empty ranges for keys.&lt;/P&gt;&lt;P&gt;Try not to give negative conditions in where clause.&lt;/P&gt;&lt;P&gt;Insted, delete the table after select statement.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Alternatively,&lt;/P&gt;&lt;P&gt;Try using following views,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;WB2_V_VBAK_VBAP&lt;/P&gt;&lt;P&gt;WB2_V_VBAK_VBAP2&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Shashank&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 23 Jun 2006 11:01:46 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/join-vbak-vbap-performance-poor/m-p/1439221#M209094</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-06-23T11:01:46Z</dc:date>
    </item>
    <item>
      <title>Re: JOIN VBAK-VBAP - performance poor</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/join-vbak-vbap-performance-poor/m-p/1439222#M209095</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;see the sample code:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;may be it will be useful.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;REPORT  ZDEMO.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;TYPES:  BEGIN OF TY_VBAK,&lt;/P&gt;&lt;P&gt;            VBELN LIKE VBAK-VBELN,&lt;/P&gt;&lt;P&gt;            VDATU LIKE VBAK-VDATU,&lt;/P&gt;&lt;P&gt;        END OF TY_VBAK.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;TYPES: BEGIN OF TY_VBAP,&lt;/P&gt;&lt;P&gt;          VBELN LIKE VBAP-VBELN,&lt;/P&gt;&lt;P&gt;          POSNR LIKE VBAP-POSNR,&lt;/P&gt;&lt;P&gt;          MATNR LIKE VBAP-MATNR,&lt;/P&gt;&lt;P&gt;          WERKS LIKE VBAK-WERKS,&lt;/P&gt;&lt;P&gt;          KWMENG LIKE VBAP-KWMENG,&lt;/P&gt;&lt;P&gt;       END OF TY_VBAP.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;DATA: IT_VBAK TYPE STANDARD TABLE OF TY_VBAK,&lt;/P&gt;&lt;P&gt;      WA_VBAK LIKE LINE OF IT_VBAK,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;      IT_VBAP TYPE STANDARD TABLE OF TY_VBAP,&lt;/P&gt;&lt;P&gt;      WA-VBAP LIKE LINE OF IT_VBAP.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;SELECT VBELN&lt;/P&gt;&lt;P&gt;       VDATU&lt;/P&gt;&lt;P&gt;       INTO TABLE IT_VBAK&lt;/P&gt;&lt;P&gt;       FROM VBAK&lt;/P&gt;&lt;P&gt;       WHERE VKORG = C_VKORG&lt;/P&gt;&lt;P&gt;       AND   VTWEG = C_VTWEG&lt;/P&gt;&lt;P&gt;       AND   SPART = C_SPART&lt;/P&gt;&lt;P&gt;       AND   ( AUART = 'TIP' OR AUART = 'TAP')&lt;/P&gt;&lt;P&gt;       AND   VDATU IN R_VDATU&lt;/P&gt;&lt;P&gt;       AND   BNAME = SPACE.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;IF SY-SUBRC = 0.&lt;/P&gt;&lt;P&gt;  SELECT VBELN&lt;/P&gt;&lt;P&gt;         POSNR&lt;/P&gt;&lt;P&gt;         MATNR&lt;/P&gt;&lt;P&gt;         WERKS&lt;/P&gt;&lt;P&gt;         KWMENG&lt;/P&gt;&lt;P&gt;         FROM VBAP INTO TABLE IT_VBAP&lt;/P&gt;&lt;P&gt;         FOR ALL ENTRIES IN IT_VBAK&lt;/P&gt;&lt;P&gt;         AND MATNR IN R_MATNRNF1&lt;/P&gt;&lt;P&gt;         AND WERKS IN R_DFL&lt;/P&gt;&lt;P&gt;         AND SPART NE 'F1'.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;  IF SY-SUBRC &amp;lt;&amp;gt; 0.&lt;/P&gt;&lt;P&gt;  ENDIF.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;ENDIF.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;- Selvapandian Arunachalam&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 23 Jun 2006 11:08:29 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/join-vbak-vbap-performance-poor/m-p/1439222#M209095</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-06-23T11:08:29Z</dc:date>
    </item>
    <item>
      <title>Re: JOIN VBAK-VBAP - performance poor</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/join-vbak-vbap-performance-poor/m-p/1439223#M209096</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I don't think there's much you can do here without re-working the logic. The program selects from two rather large tables without using either primary or secondary keys.&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, 23 Jun 2006 13:26:24 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/join-vbak-vbap-performance-poor/m-p/1439223#M209096</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-06-23T13:26:24Z</dc:date>
    </item>
    <item>
      <title>Re: JOIN VBAK-VBAP - performance poor</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/join-vbak-vbap-performance-poor/m-p/1439224#M209097</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Andrea&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;A lot of the fields in your Where clause are part of the primary key in VAPMA, which is a sales index table.  If you have a look at it in the ABAP dictionary I would be inclined to try using three select statements.  The first would select just VBELN and POSNR from VAPMA based on as many of the conditions that apply to that table &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;SELECT vbeln posnr&lt;/P&gt;&lt;P&gt;FROM vapma&lt;/P&gt;&lt;P&gt;INTO CORRESPONDING FIELDS OF TABLE i_vapma&lt;/P&gt;&lt;P&gt;WHERE matnr IN r_matnrnf1&lt;/P&gt;&lt;P&gt;AND vkorg = c_vkorg&lt;/P&gt;&lt;P&gt;etc etc&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Make sure that the order the fields appear in your WHERE clause are the same as in VAPMA.  Also avoid using criteria that do not appear in the primary key.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Then write two more select statements collectin data from VBAK and VBAP:&lt;/P&gt;&lt;P&gt;CHECK NOT i_vapma IS INITIAL.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;SELECT vbeln vdatu bname&lt;/P&gt;&lt;P&gt;FROM vbak&lt;/P&gt;&lt;P&gt;INTO CORRESPONDING FIELDS OF TABLE i_vbak&lt;/P&gt;&lt;P&gt;FOR ALL ENTRIES IN i_vapma&lt;/P&gt;&lt;P&gt;WHERE vbeln = i_vapma-vbeln.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;SELECT vbeln posnr werks matnr kwmeng&lt;/P&gt;&lt;P&gt;FROM vbap&lt;/P&gt;&lt;P&gt;INTO CORRESPONDING FIELDS OF TABLE i_vbap&lt;/P&gt;&lt;P&gt;FOR ALL ENTRIES IN i_vapma&lt;/P&gt;&lt;P&gt;WHERE vbeln = i_vapma-vbeln&lt;/P&gt;&lt;P&gt;AND posnr = i_vapma-vbap.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You can then loop through i_vbak and read records from vbap into a combined table:&lt;/P&gt;&lt;P&gt;LOOP AT i_vbak INTO wa_vbak.&lt;/P&gt;&lt;P&gt;  IF wa_vbak-vdatu IN r_vdatu AND bname = space.&lt;/P&gt;&lt;P&gt;    READ TABLE i_vbap WITH KEY vbeln = i_vbak-vbeln&lt;/P&gt;&lt;P&gt;                               posnr - i_vbak-posnr&lt;/P&gt;&lt;P&gt;                      INTO wa_vbap.&lt;/P&gt;&lt;P&gt;    IF i_vbap-werks IN r_dfl.&lt;/P&gt;&lt;P&gt;      ...&lt;/P&gt;&lt;P&gt;    ENDIF.&lt;/P&gt;&lt;P&gt;  ENDIF.&lt;/P&gt;&lt;P&gt;ENDLOOP.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;WHere ... = code to copy all of the various data from wa_vbak and wa_vbap to an itab containing the relevant combined fields.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I've not tested this so be a bit careful but I think the logic is sound in that you are using an index to find the primary key fields for VBAK and VBAP (i.e. VBELN and POSNR).  Then you can select from VBAK and VBAP using their primary keys which will be &amp;lt;b&amp;gt;much&amp;lt;/b&amp;gt; quicker than what you were trying.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Generally, you should avoid Joins as they can be very slow, it is &amp;lt;b&amp;gt;normally&amp;lt;/b&amp;gt; better to do multiple select...for all entries and then combine the data.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I hope this is of some help.&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>Fri, 23 Jun 2006 13:56:42 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/join-vbak-vbap-performance-poor/m-p/1439224#M209097</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-06-23T13:56:42Z</dc:date>
    </item>
    <item>
      <title>Re: JOIN VBAK-VBAP - performance poor</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/join-vbak-vbap-performance-poor/m-p/1439225#M209098</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Good point Andrew. I'd forgotten about that table. (I don't work with SD very much.) &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;However, I'm not sure about a join being inefficient. For example, look at SAP view COVP which is essentially a join on two very large tables. I re-wrote the original code using VAPMA in the join. If the original poster (or any one else) wants to try both methods, it would be interesting to compare the results. My code follows (I'm not sure about the WHERE on SPART):&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;SELECT p~vbeln p~posnr t~vdatu p~werks p~matnr
       p~kwmeng
  INTO CORRESPONDING FIELDS OF TABLE i_vbap
  FROM vbak AS t
    INNER JOIN vbap AS p
      ON t~vbeln = p~vbeln
    INNER JOIN vapma AS m
      ON t~vbeln = m~vbeln
  WHERE m~matnr IN r_matnr
  AND   m~vkorg = c_vkorg
  AND   m~vtweg = c_vtweg
  AND   m~spart = c_spart
  AND   m~spart NE 'F1'
  AND ( m~auart = 'tip' OR
        m~auart = 'tap')
  AND   t~vkgrp IN r_vkgrp
  AND   p~werks IN r_dfl
  AND   t~vdatu IN r_vdatu
  AND   t~bname = space.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Rob&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 23 Jun 2006 14:54:40 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/join-vbak-vbap-performance-poor/m-p/1439225#M209098</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-06-23T14:54:40Z</dc:date>
    </item>
    <item>
      <title>Re: JOIN VBAK-VBAP - performance poor</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/join-vbak-vbap-performance-poor/m-p/1439226#M209099</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks Rob - you mean there's a world outside SD??? I must get out more!!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You're right it would be interesting to see how this works - maybe one day I'll get round to trying it.  I'm not convinced it will be more efficient because I understood the Joins to work by finding all of the matches between the joined tables and then applying the Where conditions.  I suspect this means the relative efficiency is down to the number of records involved in the corresponding tables.  However, when compared with my solution yours looks much more elegant and maintainable so if the efficiency gains are marginal it's probably a better option.&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>Fri, 23 Jun 2006 15:10:29 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/join-vbak-vbap-performance-poor/m-p/1439226#M209099</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-06-23T15:10:29Z</dc:date>
    </item>
    <item>
      <title>Re: JOIN VBAK-VBAP - performance poor</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/join-vbak-vbap-performance-poor/m-p/1439227#M209100</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hey,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;SD has index tables from which data retrieval is much faster as compared to VBAK and VBAP.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;These tables are &lt;/P&gt;&lt;P&gt;VAKPA- Sales Index: Orders by Partner Function&lt;/P&gt;&lt;P&gt;VAPMA- Sales Index: Order Items by Material   &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Try using these tables in your application.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;-Kiran&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 23 Jun 2006 15:32:38 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/join-vbak-vbap-performance-poor/m-p/1439227#M209100</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-06-23T15:32:38Z</dc:date>
    </item>
    <item>
      <title>Re: JOIN VBAK-VBAP - performance poor</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/join-vbak-vbap-performance-poor/m-p/1439228#M209101</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Andrew - yes, there's a world ouside of SD - FI. (I've heard rumours of things called beaches and baseball games as well).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Any way, a similar question came up about a month ago and I wrote a program that compared a join on a number of tables against both for all entries and nested selects. The nested select lost, but the join beat both. Here's the original &lt;A class="jive_macro jive_macro_message" href="https://community.sap.com/" __jive_macro_name="message" modifiedtitle="true" __default_attr="1689367"&gt;&lt;/A&gt; Your results may vary.&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, 23 Jun 2006 15:39:42 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/join-vbak-vbap-performance-poor/m-p/1439228#M209101</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-06-23T15:39:42Z</dc:date>
    </item>
    <item>
      <title>Re: JOIN VBAK-VBAP - performance poor</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/join-vbak-vbap-performance-poor/m-p/1439229#M209102</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi AS , &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;try to use any Indexes which are created on these Tables ? if not create new S.Index.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;2.try to minimize Where Claue.&lt;/P&gt;&lt;P&gt;3.run st05 and find out DB cost and which index it is using .&lt;/P&gt;&lt;P&gt;4.maintain Proper order based on the Position of the Field in the Relavent Table.&lt;/P&gt;&lt;P&gt;5.Try use Date Range in the where Clause.&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
SELECT p~vbeln p~posnr t~vdatu p~werks p~matnr
p~kwmeng
INTO CORRESPONDING FIELDS OF TABLE i_vbap
FROM vbak AS t INNER JOIN vbap AS p
ON t~vbeln = p~vbeln
WHERE t~auart in(  'tip','tap')
AND t~vkorg = c_vkorg
AND t~vtweg = c_vtweg
AND t~spart = c_spart
AND t~vdatu IN r_vdatu 
AND p~matnr IN r_matnrnf1
AND p~werks IN r_dfl
AND p~spart NE 'F1'
AND t~vkgrp IN r_vkgrp
AND t~bname = space.
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;Prabhu&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 24 Jun 2006 06:40:01 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/join-vbak-vbap-performance-poor/m-p/1439229#M209102</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-06-24T06:40:01Z</dc:date>
    </item>
    <item>
      <title>Re: JOIN VBAK-VBAP - performance poor</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/join-vbak-vbap-performance-poor/m-p/1439230#M209103</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;the 'into corresponding fields' is not costly much; you will not be able to determine the costs as more than 0.5 % of the overall performance.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Depending on your database system, it may be useful first to select the header data from vbak and then get the item data using for all entries clause with header table and specifying the item restrictions.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Although people might tell you that for all entries is costly, they will never be able to prove that because just the opposite is fact.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Clemens&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 24 Jun 2006 18:44:43 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/join-vbak-vbap-performance-poor/m-p/1439230#M209103</guid>
      <dc:creator>Clemenss</dc:creator>
      <dc:date>2006-06-24T18:44:43Z</dc:date>
    </item>
    <item>
      <title>Re: JOIN VBAK-VBAP - performance poor</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/join-vbak-vbap-performance-poor/m-p/1439231#M209104</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Rob - what is this 'beach' of which you speak?!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks for this. I've had a look at the thread and was very interested to see that the join comes out on top.  I'll certainly being giving your program a go on our system when I get a chance and see what happens.  Call me a geek but I find all this performance stuff fascinating!!!&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>Mon, 26 Jun 2006 08:19:07 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/join-vbak-vbap-performance-poor/m-p/1439231#M209104</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-06-26T08:19:07Z</dc:date>
    </item>
  </channel>
</rss>

