<?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 Performance optimization : query taking 7mints in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/performance-optimization-query-taking-7mints/m-p/5273491#M1217312</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi All ,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Requirement : I need to improve the performance of  custom program ( Program taking more than 7 mints +dump).I checked in runtime analysis and below mention query taking more time .&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Please let me know the approach to minimize the  query time  .&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;TYPES: BEGIN OF lty_dberchz1,
           belnr    TYPE dberchz1-belnr,
           belzeile TYPE dberchz1-belzeile,
           belzart  TYPE dberchz1-belzart,
           buchrel  TYPE dberchz1-buchrel,
           tariftyp TYPE dberchz1-tariftyp,
           tarifnr  TYPE dberchz1-tarifnr,
           v_zahl1  TYPE dberchz1-v_zahl1,
           n_zahl1  TYPE dberchz1-n_zahl1,
           v_zahl3  TYPE dberchz1-v_zahl3,
           n_zahl3  TYPE dberchz1-n_zahl3,
           nettobtr TYPE dberchz3-nettobtr,
           twaers   TYPE dberchz3-twaers,
         END   OF lty_dberchz1.

  DATA: lt_dberchz1 TYPE SORTED TABLE OF lty_dberchz1
        WITH NON-UNIQUE KEY belnr belzeile
        INITIAL SIZE 0 WITH HEADER LINE.


DATA: lt_dberchz1a LIKE TABLE OF lt_dberchz1 WITH HEADER LINE.

*** ***********************************Taking more time*************************************************
*Individual line items
    SELECT dberchz1~belnr dberchz1~belzeile
           belzart buchrel tariftyp tarifnr
           v_zahl1 n_zahl1 v_zahl3 n_zahl3
           nettobtr twaers
      INTO TABLE lt_dberchz1
      FROM dberchz1 JOIN dberchz3
      ON dberchz1~belnr = dberchz3~belnr
      AND dberchz1~belzeile = dberchz3~belzeile
      WHERE buchrel  EQ 'X'.

    DELETE lt_dberchz1 WHERE belzart NOT IN r_belzart.      

    LOOP AT lt_dberchz1.
      READ TABLE lt_dberdlb BINARY SEARCH
      WITH KEY billdoc = lt_dberchz1-belnr.
      IF sy-subrc NE 0.
        DELETE lt_dberchz1.
      ENDIF.
    ENDLOOP.

    lt_dberchz1a[] = lt_dberchz1[].
    DELETE lt_dberchz1 WHERE belzart EQ 'ZUTAX1'
                          OR belzart EQ 'ZUTAX2'
                          OR belzart EQ 'ZUTAX3'.
    DELETE lt_dberchz1a WHERE belzart NE 'ZUTAX1'
                          AND belzart NE 'ZUTAX2'
                          AND belzart NE 'ZUTAX3'.

***************************second query************************************
*  SELECT opbel budat vkont partner sto_opbel
    INTO CORRESPONDING FIELDS OF TABLE lt_erdk
    FROM erdk
    WHERE budat IN r_budat
      AND druckdat   NE '00000000'
      AND stokz      EQ space
      AND intopbel   EQ space
      AND total_amnt GT 40000.
**************************taking more time*********************************
  SORT lt_erdk BY opbel.

  IF lt_erdk[] IS NOT INITIAL.
    SELECT DISTINCT printdoc billdoc vertrag
      INTO CORRESPONDING FIELDS OF TABLE lt_dberdlb
      FROM dberdlb
* begin of code change by vishal
      FOR ALL ENTRIES IN lt_erdk
      WHERE printdoc = lt_erdk-opbel.

    IF lt_dberdlb[] IS NOT INITIAL.
      SELECT belnr belzart ab bis aus01
             v_zahl1 n_zahl1 v_zahl3 n_zahl3
        INTO CORRESPONDING FIELDS OF TABLE lt_dberchz1
        FROM dberchz1
        FOR ALL ENTRIES IN lt_dberdlb
        WHERE belnr   EQ lt_dberdlb-billdoc
          AND belzart IN ('ZUTAX1', 'ZUTAX2', 'ZUTAX3').
    ENDIF. "lt_dberdlb
   endif.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;Rahul&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Edited by: Matt on Mar 17, 2009 4:17 PM - Added  tags and moved to correct forum&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 17 Mar 2009 14:22:00 GMT</pubDate>
    <dc:creator>Former Member</dc:creator>
    <dc:date>2009-03-17T14:22:00Z</dc:date>
    <item>
      <title>Performance optimization : query taking 7mints</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/performance-optimization-query-taking-7mints/m-p/5273491#M1217312</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi All ,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Requirement : I need to improve the performance of  custom program ( Program taking more than 7 mints +dump).I checked in runtime analysis and below mention query taking more time .&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Please let me know the approach to minimize the  query time  .&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;TYPES: BEGIN OF lty_dberchz1,
           belnr    TYPE dberchz1-belnr,
           belzeile TYPE dberchz1-belzeile,
           belzart  TYPE dberchz1-belzart,
           buchrel  TYPE dberchz1-buchrel,
           tariftyp TYPE dberchz1-tariftyp,
           tarifnr  TYPE dberchz1-tarifnr,
           v_zahl1  TYPE dberchz1-v_zahl1,
           n_zahl1  TYPE dberchz1-n_zahl1,
           v_zahl3  TYPE dberchz1-v_zahl3,
           n_zahl3  TYPE dberchz1-n_zahl3,
           nettobtr TYPE dberchz3-nettobtr,
           twaers   TYPE dberchz3-twaers,
         END   OF lty_dberchz1.

  DATA: lt_dberchz1 TYPE SORTED TABLE OF lty_dberchz1
        WITH NON-UNIQUE KEY belnr belzeile
        INITIAL SIZE 0 WITH HEADER LINE.


DATA: lt_dberchz1a LIKE TABLE OF lt_dberchz1 WITH HEADER LINE.

*** ***********************************Taking more time*************************************************
*Individual line items
    SELECT dberchz1~belnr dberchz1~belzeile
           belzart buchrel tariftyp tarifnr
           v_zahl1 n_zahl1 v_zahl3 n_zahl3
           nettobtr twaers
      INTO TABLE lt_dberchz1
      FROM dberchz1 JOIN dberchz3
      ON dberchz1~belnr = dberchz3~belnr
      AND dberchz1~belzeile = dberchz3~belzeile
      WHERE buchrel  EQ 'X'.

    DELETE lt_dberchz1 WHERE belzart NOT IN r_belzart.      

    LOOP AT lt_dberchz1.
      READ TABLE lt_dberdlb BINARY SEARCH
      WITH KEY billdoc = lt_dberchz1-belnr.
      IF sy-subrc NE 0.
        DELETE lt_dberchz1.
      ENDIF.
    ENDLOOP.

    lt_dberchz1a[] = lt_dberchz1[].
    DELETE lt_dberchz1 WHERE belzart EQ 'ZUTAX1'
                          OR belzart EQ 'ZUTAX2'
                          OR belzart EQ 'ZUTAX3'.
    DELETE lt_dberchz1a WHERE belzart NE 'ZUTAX1'
                          AND belzart NE 'ZUTAX2'
                          AND belzart NE 'ZUTAX3'.

***************************second query************************************
*  SELECT opbel budat vkont partner sto_opbel
    INTO CORRESPONDING FIELDS OF TABLE lt_erdk
    FROM erdk
    WHERE budat IN r_budat
      AND druckdat   NE '00000000'
      AND stokz      EQ space
      AND intopbel   EQ space
      AND total_amnt GT 40000.
**************************taking more time*********************************
  SORT lt_erdk BY opbel.

  IF lt_erdk[] IS NOT INITIAL.
    SELECT DISTINCT printdoc billdoc vertrag
      INTO CORRESPONDING FIELDS OF TABLE lt_dberdlb
      FROM dberdlb
* begin of code change by vishal
      FOR ALL ENTRIES IN lt_erdk
      WHERE printdoc = lt_erdk-opbel.

    IF lt_dberdlb[] IS NOT INITIAL.
      SELECT belnr belzart ab bis aus01
             v_zahl1 n_zahl1 v_zahl3 n_zahl3
        INTO CORRESPONDING FIELDS OF TABLE lt_dberchz1
        FROM dberchz1
        FOR ALL ENTRIES IN lt_dberdlb
        WHERE belnr   EQ lt_dberdlb-billdoc
          AND belzart IN ('ZUTAX1', 'ZUTAX2', 'ZUTAX3').
    ENDIF. "lt_dberdlb
   endif.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;Rahul&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Edited by: Matt on Mar 17, 2009 4:17 PM - Added  tags and moved to correct forum&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 17 Mar 2009 14:22:00 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/performance-optimization-query-taking-7mints/m-p/5273491#M1217312</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2009-03-17T14:22:00Z</dc:date>
    </item>
    <item>
      <title>Re: Performance optimization : query taking 7mints</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/performance-optimization-query-taking-7mints/m-p/5273492#M1217313</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;your post is unclear. &lt;/P&gt;&lt;P&gt;1. write how many rows do you have in your tables dberchz1 and dberchz3.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;2. do you have indexes on a table? probably not, but this is important after you will write number of rows, because for small tables indexes are not necessary - you should have an index on fields &lt;STRONG&gt;buchrel&lt;/STRONG&gt; and &lt;STRONG&gt;belzart&lt;/STRONG&gt; and instead of this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;DELETE lt_dberchz1 WHERE belzart NOT IN r_belzart.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;just move the condition to the select statement after creating an index (removing NOT before).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;3. what is this table lt_dberdlb ?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;put the coding into the {/code} tags because now your post is incomplete.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 17 Mar 2009 14:57:11 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/performance-optimization-query-taking-7mints/m-p/5273492#M1217313</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2009-03-17T14:57:11Z</dc:date>
    </item>
    <item>
      <title>Re: Performance optimization : query taking 7mints</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/performance-optimization-query-taking-7mints/m-p/5273493#M1217314</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello Rahul,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;*Individual line items&lt;/P&gt;&lt;P&gt;SELECT dberchz1&lt;SUB&gt;belnr dberchz1&lt;/SUB&gt;belzeile&lt;/P&gt;&lt;P&gt;belzart buchrel tariftyp tarifnr&lt;/P&gt;&lt;P&gt;v_zahl1 n_zahl1 v_zahl3 n_zahl3&lt;/P&gt;&lt;P&gt;nettobtr twaers&lt;/P&gt;&lt;P&gt;INTO TABLE lt_dberchz1&lt;/P&gt;&lt;P&gt;FROM dberchz1 JOIN dberchz3&lt;/P&gt;&lt;P&gt;ON dberchz1&lt;SUB&gt;belnr = dberchz3&lt;/SUB&gt;belnr&lt;/P&gt;&lt;P&gt;AND dberchz1&lt;SUB&gt;belzeile = dberchz3&lt;/SUB&gt;belzeile&lt;/P&gt;&lt;P&gt;WHERE buchrel EQ 'X'.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;DELETE lt_dberchz1 WHERE belzart NOT IN r_belzart. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;LOOP AT lt_dberchz1.&lt;/P&gt;&lt;P&gt;READ TABLE lt_dberdlb BINARY SEARCH&lt;/P&gt;&lt;P&gt;WITH KEY billdoc = lt_dberchz1-belnr.&lt;/P&gt;&lt;P&gt;IF sy-subrc NE 0.&lt;/P&gt;&lt;P&gt;DELETE lt_dberchz1.&lt;/P&gt;&lt;P&gt;ENDIF.&lt;/P&gt;&lt;P&gt;ENDLOOP.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You are trying to capture data in lt_dberchz1 from the JOIN of dberchz1 &amp;amp; dberchz3 &amp;amp; then deleting the records from lt_dberchz1 which are not there in the table lt_dberdlb.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Is my understanding correct. If so you can change your select query to incorporate FOR ALL ENTRIES like this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;*Individual line items
SELECT dberchz1~belnr dberchz1~belzeile
belzart buchrel tariftyp tarifnr
v_zahl1 n_zahl1 v_zahl3 n_zahl3
nettobtr twaers
INTO TABLE lt_dberchz1
FROM dberchz1 JOIN dberchz3
ON dberchz1~belnr = dberchz3~belnr
AND dberchz1~belzeile = dberchz3~belzeile
FOR ALL ENTRIES IN lt_dberdlb
WHERE 
belnr = lt_dberdlb-billdoc
AND buchrel EQ 'X'.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This will lessen the data fetching time &amp;amp; you can avoid the LOOP...ENDLOOP block as well.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;BR,&lt;/P&gt;&lt;P&gt;Suhas&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 17 Mar 2009 15:13:38 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/performance-optimization-query-taking-7mints/m-p/5273493#M1217314</guid>
      <dc:creator>SuhaSaha</dc:creator>
      <dc:date>2009-03-17T15:13:38Z</dc:date>
    </item>
    <item>
      <title>Re: Performance optimization : query taking 7mints</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/performance-optimization-query-taking-7mints/m-p/5273494#M1217315</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Run the SQL Trace and tell us where the time is spent,&lt;/P&gt;&lt;P&gt;see here how to use it:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
 SELECT dberchz1~belnr dberchz1~belzeile
           belzart buchrel tariftyp tarifnr
           v_zahl1 n_zahl1 v_zahl3 n_zahl3
           nettobtr twaers
      INTO TABLE lt_dberchz1
      FROM dberchz1 JOIN dberchz3
      ON dberchz1~belnr = dberchz3~belnr
      AND dberchz1~belzeile = dberchz3~belzeile
      WHERE buchrel  EQ 'X'.
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I assume that is this select, but without data is quite useless&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;How large are the two tables  dberchz1 JOIN dberchz3&lt;/P&gt;&lt;P&gt;What are the key fields?&lt;/P&gt;&lt;P&gt;Is there an index on buchrel&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Please use aliases  dberchz1 as a&lt;/P&gt;&lt;P&gt;                             INNER JOIN dberchz3 as b&lt;/P&gt;&lt;P&gt;to which table does buchrel belong?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I don't know you tables, but buchrel  EQ 'X' seems not selective, so a lot of data&lt;/P&gt;&lt;P&gt;might be selected.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
lt_dberchz1 TYPE SORTED TABLE OF lty_dberchz1
        WITH NON-UNIQUE KEY belnr belzeile
        INITIAL SIZE 0 WITH HEADER LINE.
 
    DELETE lt_dberchz1 WHERE belzart NOT IN r_belzart.      
 
    LOOP AT lt_dberchz1.
      READ TABLE lt_dberdlb BINARY SEARCH
      WITH KEY billdoc = lt_dberchz1-belnr.
      IF sy-subrc NE 0.
        DELETE lt_dberchz1.
      ENDIF.
    ENDLOOP.
 
    lt_dberchz1a[] = lt_dberchz1[].
    DELETE lt_dberchz1 WHERE belzart EQ 'ZUTAX1'
                          OR belzart EQ 'ZUTAX2'
                          OR belzart EQ 'ZUTAX3'.
    DELETE lt_dberchz1a WHERE belzart NE 'ZUTAX1'
                          AND belzart NE 'ZUTAX2'
                          AND belzart NE 'ZUTAX3'.

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This is really poor coding, there is sorted table ... nice a compelelty different key is&lt;/P&gt;&lt;P&gt;needed and used .... useless.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Then there is a loop which is anywy a full processing no sort necessary.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Where is the read if you use binary search on TABLE lt_dberdlb ?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Then the tables are again process completely ...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
    DELETE lt_dberchz1a WHERE belzart NE 'ZUTAX1'
                          AND belzart NE 'ZUTAX2'
                          AND belzart NE 'ZUTAX3'.
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;What is that ???? Are you sure that anything can survive this delete???&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, 17 Mar 2009 16:51:30 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/performance-optimization-query-taking-7mints/m-p/5273494#M1217315</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2009-03-17T16:51:30Z</dc:date>
    </item>
  </channel>
</rss>

