Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

performance issue in nested loops

Former Member
0 Kudos
321

Moved to correct forum by moderator

hi

below is my code . in this code i used the 3 nested loops which takes the very long time to execute .

say response time > 1000.

pls some body altered this code in efficient way or provide the technique .

i knew it is resolved by read statement , it reads only a single row but in my case several entries are in

it_ekbe1 and it_ekpo.


LOOP AT IT_EKKO1 INTO WT_EKKO1.
    LOOP AT IT_EKBE1 INTO WT_EKBE1.        
      LOOP AT IT_EKPO INTO WT_EKPO.
        IF WT_EKKO1-EBELN EQ WT_EKBE1-EBELN AND
           WT_EKBE1-EBELN EQ WT_EKPO-EBELN AND
           WT_EKBE1-EBELP EQ WT_EKPO-EBELP .
*           WT_EKBE1-WERKS EQ WT_EKPO-WERKS.
          IF WT_EKPO-MWSKZ NE SPACE .
            PERFORM TAX_CALCULATE_FUNCTION USING WT_EKKO1-BUKRS      "company code
                                                 WT_EKBE1-BUDAT     "posting date
                                                 WT_EKKO1-WAERS      "currency key
                                                 WT_EKPO-EBELN
                                                 WT_EKPO-EBELP      "CONDITION ITEM NUMBER
                                                 WT_EKPO-MWSKZ      "SALES TAX CODE
                                                 WT_EKPO-TXJCD      "TAX JURISIDICTION
                                                 WT_EKBE1-DMBTR      "AMOUNT IN DOCUMENT
                                                 WT_EKKO1-LIFNR      "VENDOR CODE
                                                 WT_EKKO1-LANDS      "COUNTRY KEY
                                                 WT_EKKO1-EKORG      "PURCHASE ORGANIZATION
                                                 WT_EKKO1-LLIEF      "SUPPLYING VENDOR
                                                 WT_EKKO1-BEDAT      "DOCUMENT DATE
                                                 WT_EKPO-MATNR      "material code
                                                 WT_EKPO-WERKS      "PLANT
                                                 WT_EKPO-BWTAR      "VALUATION TYPE
                                                 WT_EKPO-MATKL      "MATERIAL GROUP
                                                 WT_EKPO-MEINS      "BASE UNIT OF MEASURE
                                                 WT_EKBE1-MENGE      "QUANTITY
                                                 WT_EKPO-MTART      "MATERIAL TYPE
                                         CHANGING T_MWSBP.
              CASE WT_EKPO-WERKS.
              WHEN 'RAU1'.
                R1_BOUGHT = R1_BOUGHT + WT_EKBE1-DMBTR.
                R1_GROSS = R1_GROSS + WT_EKBE1-DMBTR + T_MWSBP.
                CLEAR T_MWSBP.
                WT_TAB-BOUGHT1 = R1_BOUGHT.
                WT_TAB-GROSS1 = R1_GROSS.
                WT_TAB-WERKS = WT_EKPO-WERKS.
              WHEN 'RAU2'.
                R2_BOUGHT = R2_BOUGHT + WT_EKBE1-DMBTR.
                R2_GROSS = R2_GROSS + WT_EKBE1-DMBTR + T_MWSBP.
                CLEAR T_MWSBP.
                WT_TAB-BOUGHT1 = R2_BOUGHT.
                WT_TAB-GROSS1 = R2_GROSS.
                WT_TAB-WERKS = WT_EKPO-WERKS.
              WHEN 'RAU3'.
                R3_BOUGHT = R3_BOUGHT + WT_EKBE1-DMBTR.
                R3_GROSS = R3_GROSS + WT_EKBE1-DMBTR + T_MWSBP.
                CLEAR T_MWSBP.
                WT_TAB-BOUGHT1 = R3_BOUGHT.
                WT_TAB-GROSS1 = R3_GROSS.
                WT_TAB-WERKS = WT_EKPO-WERKS.
              WHEN 'RAU4'.
                R4_BOUGHT  = R4_BOUGHT + WT_EKBE1-DMBTR.
                R4_GROSS = R4_GROSS + WT_EKBE1-DMBTR + T_MWSBP.
                CLEAR T_MWSBP.
                WT_TAB-BOUGHT1 = R4_BOUGHT.
                WT_TAB-GROSS1 = R4_GROSS.
                WT_TAB-WERKS = WT_EKPO-WERKS.
              WHEN 'AP01'.
                AP1_BOUGHT = AP1_BOUGHT + WT_EKBE1-DMBTR.
                AP1_GROSS = AP1_GROSS + WT_EKBE1-DMBTR + T_MWSBP.
                CLEAR T_MWSBP.
                WT_TAB-BOUGHT1 = AP1_BOUGHT.
                WT_TAB-GROSS1 = AP1_GROSS.
                WT_TAB-WERKS = WT_EKPO-WERKS.
            ENDCASE.
 MODIFY IT_TAB FROM WT_TAB TRANSPORTING BOUGHT1
                                                   GROSS1
                                                   WERKS WHERE WERKS = WT_EKPO-WERKS.
            IF SY-SUBRC NE 0.
              APPEND WT_TAB TO IT_TAB.
              CLEAR WT_TAB.
            ENDIF.
ENDLOOP.
    ENDLOOP.
  ENDLOOP.

regards

surender.

Edited by: Matt on Jan 21, 2009 5:41 PM

1 ACCEPTED SOLUTION

Former Member
0 Kudos
268

Hi Surender,

You can try this..

LOOP AT IT_EKKO1 INTO WT_EKKO1.

LOOP AT IT_EKBE1 INTO WT_EKBE1 where ebeln = wt_ekko1-ebeln.

LOOP AT IT_EKPO INTO WT_EKPO where ebeln = wa_ekko1-ebeln

and werks = wa_ekko1-werks

and ebelp = wa_ekko1-ebelp

and MWSKZ NE SPACE.

or u can use READ statement.

Hope it will help.

regrds,

Lokesh

25 REPLIES 25

Former Member
0 Kudos
269

Hi Surender,

You can try this..

LOOP AT IT_EKKO1 INTO WT_EKKO1.

LOOP AT IT_EKBE1 INTO WT_EKBE1 where ebeln = wt_ekko1-ebeln.

LOOP AT IT_EKPO INTO WT_EKPO where ebeln = wa_ekko1-ebeln

and werks = wa_ekko1-werks

and ebelp = wa_ekko1-ebelp

and MWSKZ NE SPACE.

or u can use READ statement.

Hope it will help.

regrds,

Lokesh

0 Kudos
268

hi

first thx to lokesh,mukesh, and peter.

you r all gave the same response.

i tried out with the where clause.

but see i am triggered the posting date as 01.03.2008 to 01.09.2008 for all 5 plants. but its still running for 15 minutes

regards

surender

0 Kudos
268

Hi surrender,

can you post your code for TAX_CALCULATE_FUNCTION? I think this is what slows down the program.

regards,

Peter

Edited by: Peter Ruiz on Jan 21, 2009 3:09 PM

0 Kudos
268

Hi Surender,

If you want to increase the performance of Nested loops then you should use Parallel cursors. it will incease the performance drastically. But there is one condition that all the internal tables should be Sorted.

You can read the documentation on Parallel cursor from the following link:

[https://www.sdn.sap.com/irj/scn/weblogs?blog=/pub/wlg/3119] [original link is broken] [original link is broken] [original link is broken];

Ashvender

0 Kudos
268

hi peter


FORM TAX_CALCULATE_FUNCTION USING WT_EKKO-BUKRS      "company code
                                  WT_EKBE-BUDAT     "posting date
                                  WT_EKKO-WAERS      "currency key
                                  WT_EKPO-EBELN
                                  WT_EKPO-EBELP      "CONDITION ITEM NUMBER
                                  WT_EKPO-MWSKZ      "SALES TAX CODE
                                  WT_EKPO-TXJCD      "TAX JURISIDICTION
                                  WT_EKBE-DMBTR      "AMOUNT IN DOCUMENT
                                  WT_EKKO-LIFNR      "VENDOR CODE
                                  WT_EKKO-LANDS      "COUNTRY KEY
                                  WT_EKKO-EKORG      "PURCHASE ORGANIZATION
                                  WT_EKKO-LLIEF      "SUPPLYING VENDOR
                                  WT_EKKO-BEDAT      "DOCUMENT DATE
                                  WT_EKPO-MATNR      "MATERIAL
                                  WT_EKPO-WERKS      "PLANT
                                  WT_EKPO-BWTAR      "VALUATION TYPE
                                  WT_EKPO-MATKL      "MATERIAL GROUP
                                  WT_EKPO-MEINS      "BASE UNIT OF MEASURE
                                  WT_EKBE-MENGE      "QUANTITY
                                  WT_EKPO-MTART      "MATERIAL TYPE
                           CHANGING T_MWBSP.

  IT_TAXCOM-BUKRS = WT_EKKO-BUKRS.
  IT_TAXCOM-BUDAT = WT_EKBE-BUDAT.
  IT_TAXCOM-WAERS = WT_EKKO-WAERS.
  IT_TAXCOM-EBELN = WT_EKPO-EBELN.
  IT_TAXCOM-EBELP = WT_EKPO-EBELP.
  IT_TAXCOM-KPOSN = WT_EKPO-EBELP.
  IT_TAXCOM-MWSKZ = WT_EKPO-MWSKZ.
  IT_TAXCOM-TXJCD = WT_EKPO-TXJCD.
  IT_TAXCOM-SHKZG = 'H'.                "CREDIT/DEBIT INDICATOR
  IT_TAXCOM-XMWST = 'X'.                "APPLYING TAX AUTOMATICALLY
  IT_TAXCOM-WRBTR = WT_EKBE-DMBTR.
  IT_TAXCOM-LIFNR = WT_EKKO-LIFNR.
  IT_TAXCOM-LAND1 = WT_EKKO-LANDS.
  IT_TAXCOM-EKORG = WT_EKKO-EKORG.
  IT_TAXCOM-HWAER = 'INR'.
  IT_TAXCOM-LLIEF = WT_EKKO-LLIEF.
  IT_TAXCOM-BLDAT = WT_EKKO-BEDAT.
  IT_TAXCOM-MATNR = WT_EKPO-MATNR.
  IT_TAXCOM-WERKS = WT_EKPO-WERKS.
  IT_TAXCOM-BWTAR = WT_EKPO-BWTAR.
  IT_TAXCOM-MATKL = WT_EKPO-MATKL.
  IT_TAXCOM-MEINS = WT_EKPO-MEINS.
  IT_TAXCOM-MGLME = WT_EKBE-MENGE.
  IT_TAXCOM-MTART = WT_EKPO-MTART.
*  IT_TAXCOM-MWART = 'V'.

  CALL FUNCTION 'CALCULATE_TAX_ITEM'
    EXPORTING
*     ANZAHLUNG                 = ' '
*     DIALOG                    = ' '
*     DISPLAY_ONLY              = ' '
*     INKLUSIVE                 = ' '
*     I_ANWTYP                  = ' '
*     I_DMBTR                   = '0'
*     I_MWSTS                   = '0'
      I_TAXCOM                  = IT_TAXCOM
*     PRUEFEN                   = ' '
*     RESET                     = ' '
   IMPORTING
*     E_NAVFW                   =
     E_TAXCOM                  =  IT_TAXCOM
*     E_XSTVR                   =
*     NAV_ANTEIL                =
*   TABLES
*     T_XKOMV                   =
*   EXCEPTIONS
*     MWSKZ_NOT_DEFINED         = 1
*     MWSKZ_NOT_FOUND           = 2
*     MWSKZ_NOT_VALID           = 3
*     STEUERBETRAG_FALSCH       = 4
*     COUNTRY_NOT_FOUND         = 5
*     OTHERS                    = 6
            .
  IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

  IF SY-SUBRC EQ 0.
    T_MWSBP = IT_TAXCOM-WMWST. "TAX AMOUNT IN DOCUMENT
  ENDIF.
ENDFORM.

regards

surender

0 Kudos
268

Hi Surrender,

The slow processing of your program comes from the FM CALCULATE_TAX_ITEM since this FM selects data from the database.

Can you please check your ABAP Program via TCode SE30 and see which area uses more time when processing.

A work around for this is to try trimming down your tables so that you won't need to call the FM CALCULATE_TAX_ITEM on every loop pass.

regards,

Peter

0 Kudos
268

hi peter

i executed the report for an one plant and its pd from 01.03.2008 to 01.08.2008 the measurement calculation is pasted below.

abap 297,646,010 = 79.1%

database 78,279,147 = 20.8%

system 146,916 = 0.0%

total 376,072,073 = 100,0%

without tax_calculate fm how can i calculate the tax values for an individual items .

regards

surender

0 Kudos
268

Hi surrender,

try to read and understand the code for the FM and implement it in your program

also, try to avoid select statements inside a LOOP.. ENDLOOP block.

regards,

Peter

Former Member
0 Kudos
268

Hi,

Add the where clause for the LOOPS,

LOOP AT IT_EKKO1 INTO WT_EKKO1.

LOOP AT IT_EKBE1 INTO WT_EKBE1 where ebeln = WT_EKKO1-EBELN.

LOOP AT IT_EKPO INTO WT_EKPO where ebeln = WT_EKPO-EBELN and

EBELP = WT_EKKO1-EBELP .

This will considerably reduce the execution time.

Regards,

Mukesh.

Former Member
0 Kudos
268

Hi Surender,

Use the following method. Replace loop statement with READ TABLE.

Sort all the tables first.

LOOP AT IT_EKKO1 INTO WT_EKKO1.

READ TABLE IT_EKBE1 INTO WT_EKBE1 WITH KEY...... BINARY SEARCH

READ TABLE IT_EKPO INTO WT_EKPO WITH KEY........BINARY SEARCH.

ENDLOOP.

Hope this will help.

Regards,

Nitin.

peter_ruiz2
Active Contributor
0 Kudos
268

Hi,

This may be of help.

LOOP AT IT_EKKO1 INTO WT_EKKO1.

LOOP AT IT_EKBE1 INTO WT_EKBE1 WHERE EBELN EQ WT_EKKO1-EBELN.

LOOP AT IT_EKPO INTO WT_EKPO *WHERE EBELN EQ WT_EKBE1-EBELN

AND EBELP EQ WT_EKBE1-EBELP

AND WERKS EQ WT_EKBE1-WERKS

AND MWSKZ NE space*.

...........

ENDLOOP.

ENDLOOP.

ENDLOOP.

regards,

Peter

Former Member
0 Kudos
268

Hi

LOOP AT IT_EKPO INTO WT_EKPO.

READ TABLE IT_EKBE1 into wa_ekbe1

with key

WT_EKBE1-EBELN EQ WT_EKPO-EBELN AND

WT_EKBE1-EBELP EQ WT_EKPO-EBELP .

WT_EKBE1-WERKS EQ WT_EKPO-WERKS.

read atble it_kko1 into wa_ekko1

with key

WT_EKKO1-EBELN EQ WT_EKBE1-EBELN .

*code...

endloop.

Former Member
0 Kudos
268

Hi Surendhar,

From your posting i beleive, table "IT_EKKO1" will have unique records based on which you would like to loop into other two tables, IT_EKBE1 and IT_EKPO. You can follow the below given logic to acheive the same.

Loop at IT_EKBE1 into WT_EKBE1.
Read Table IT_EKKO1 into WT_EKKO1 with key EBELN = WT_EKBE1-EBELN Binary Search.
IF sy-subrc eq 0.
" Do the calculation part.
Endif.
Endloop.

Similarly,

Loop at IT_EKPO into WT_EKPO.
Read Table IT_EKBE1 into WT_EKBE1 with key EBELN = WT_EKPO-EBELN
                                                                       EBELP = WT_EKPO-EBELP
                                                                       Binary Search.
If sy-subrc eq 0.
" Perform the calculation part.
Endif.
Endloop.

With this logic, you might have to change the sub-routine parameters as well for tax calculation...

Check if this type of logic would serve the purpose.

Best Regards,

Ram.

Former Member
0 Kudos
268

hi,

I ahve tried out using parallel cursor for similar situation and it helped.Here is smaill documentation on it.it might help u.

Parallel cursor is the technique to increase the performance of the program, when there are nested loops.

For example if we use nested select in our program instead of For all entries addition, then definitely performance going down. In the same way the if we use nested loops in the program it will also leads to down the performance.

For example:

if the code contains this type of logic:

loop at itab into wa.

loop at itab1 into wa1.

endloop.

endloop.

In the above logic, for one record of the itab, again the table itab1 loops many times. If itab contains many records and also at the same time if itab1 contains double the records, then this would result into performance issue. You can modify it as:

loop at itab into wa.

read table itab1 into wa1 with key field1 = wa-field1.

v_tabix = sy-tabix.

if sy-subrc eq 0.

loop at itab1 into wa1 from v_tabix. "It will loop from that index

endloop.

endif.

endloop.

an example program also am attaching but this contains only one loop.u can inplement the same as per ur case.

START-OF-SELECTION

SELECT *

INTO TABLE I_KEPH FROM KEPH

WHERE KADKY <= SY-DATUM

AND TVERS = '01'

AND KALKA IN ('01','Z1','Z2')

AND BWVAR IN ('Z01','Z02','Z03','Z04','Z07')

AND KKZST = ' '

AND KKZMA = ' '

AND KKZMM = ' '

AND KEART = 'H'

AND PATNR = 0.

  • Table must be sorted to ensure all required records are together

SORT I_KEPH BY KALNR KALKA BWVAR KADKY.

  • Perform actual processing

Perform get_cost_values.

----


FORM GET_COST_VALUES.

  • Determine start position and then process all records for given key

  • from that starting point

  • i_keph is sorted on kalnr kalka bwvar kadky.

READ TABLE I_KEPH WITH KEY KALNR = W_KEKO-KALNR

KALKA = W_KEKO-KALKA

BWVAR = W_KEKO-BWVAR

KADKY = W_KEKO-KADKY BINARY SEARCH.

IF SY-SUBRC = 0.

  • Loop at itab from first record found (sy-tabix) until record

  • no-longer matches your criteria.

LOOP AT I_KEPH FROM SY-TABIX.

IF I_KEPH-KALNR = W_KEKO-KALNR AND I_KEPH-KALKA = W_KEKO-KALKA

AND I_KEPH-BWVAR = W_KEKO-BWVAR AND I_KEPH-KADKY = W_KEKO-KADKY.

  • Key match

D_MAT_COST = D_MAT_COST + I_KEPH-KST001.

D_LAB_COST = D_LAB_COST + I_KEPH-KST004.

D_OVER_HEAD = D_OVER_HEAD + I_KEPH-KST010.

D_EXT_PURCH = D_EXT_PURCH + I_KEPH-KST014.

D_MISC_COST = D_MISC_COST + I_KEPH-KST002 + I_KEPH-KST003

+ I_KEPH-KST005 + I_KEPH-KST006 + I_KEPH-KST007

+ I_KEPH-KST008 + I_KEPH-KST009 + I_KEPH-KST011

+ I_KEPH-KST012 + I_KEPH-KST013 + I_KEPH-KST015

+ I_KEPH-KST016 + I_KEPH-KST017 + I_KEPH-KST018

+ I_KEPH-KST019 + I_KEPH-KST020 + I_KEPH-KST021

+ I_KEPH-KST022 + I_KEPH-KST023 + I_KEPH-KST024

+ I_KEPH-KST025 + I_KEPH-KST026 + I_KEPH-KST027

+ I_KEPH-KST028 + I_KEPH-KST029 + I_KEPH-KST030

+ I_KEPH-KST031 + I_KEPH-KST032 + I_KEPH-KST033

+ I_KEPH-KST034 + I_KEPH-KST035 + I_KEPH-KST036

+ I_KEPH-KST037 + I_KEPH-KST038 + I_KEPH-KST039

+ I_KEPH-KST040.

ELSE.

  • Key greater - can't be less

EXIT. " Exit loop

ENDIF.

ENDLOOP.

ENDIF.

D_MAT_COST = D_MAT_COST / W_KEKO-LOSGR.

D_LAB_COST = D_LAB_COST / W_KEKO-LOSGR.

D_OVER_HEAD = D_OVER_HEAD / W_KEKO-LOSGR.

D_EXT_PURCH = D_EXT_PURCH / W_KEKO-LOSGR.

D_MISC_COST = D_MISC_COST / W_KEKO-LOSGR.

ENDFORM. " GET_COST_VALUES

aris_hidalgo
Contributor
0 Kudos
268

Hi,

you can try making your internal tables as type SORTED and use WHERE in you LOOP.

e.g. loop at WHERE...

Hope it helps...

Former Member
0 Kudos
268

Hi Surender,

You can go for Read statements inside the first loop avoiding nested loops.

This can be done this way...

LOOP AT IT_EKKO1 INTO WT_EKKO1.

READ TABLE IT_EKBE1 INTO WT_EKBE1 WITH KEY EBELN = WT_EKKO1-EBELN .

IF SY-SUBRC = 0.

READ TABLE IT_EKPO INTO WT_EKPO WITH KEY EBELN = WT_EKBE1-EBELN EBELP = WT_EKBE1-EBELP .

IF SY-SUBRC = 0.

IF WT_EKPO-MWSKZ NE SPACE .

PERFORM TAX_CALCULATE_FUNCTION USING WT_EKKO1-BUKRS "company code

WT_EKBE1-BUDAT "posting date

WT_EKKO1-WAERS "currency key

WT_EKPO-EBELN

WT_EKPO-EBELP "CONDITION ITEM NUMBER

WT_EKPO-MWSKZ "SALES TAX CODE

WT_EKPO-TXJCD "TAX JURISIDICTION

WT_EKBE1-DMBTR "AMOUNT IN DOCUMENT

WT_EKKO1-LIFNR "VENDOR CODE

WT_EKKO1-LANDS "COUNTRY KEY

WT_EKKO1-EKORG "PURCHASE ORGANIZATION

WT_EKKO1-LLIEF "SUPPLYING VENDOR

WT_EKKO1-BEDAT "DOCUMENT DATE

WT_EKPO-MATNR "material code

WT_EKPO-WERKS "PLANT

WT_EKPO-BWTAR "VALUATION TYPE

WT_EKPO-MATKL "MATERIAL GROUP

WT_EKPO-MEINS "BASE UNIT OF MEASURE

WT_EKBE1-MENGE "QUANTITY

WT_EKPO-MTART "MATERIAL TYPE

CHANGING T_MWSBP.

CASE WT_EKPO-WERKS.

WHEN 'RAU1'.

R1_BOUGHT = R1_BOUGHT + WT_EKBE1-DMBTR.

R1_GROSS = R1_GROSS + WT_EKBE1-DMBTR + T_MWSBP.

CLEAR T_MWSBP.

WT_TAB-BOUGHT1 = R1_BOUGHT.

WT_TAB-GROSS1 = R1_GROSS.

WT_TAB-WERKS = WT_EKPO-WERKS.

WHEN 'RAU2'.

R2_BOUGHT = R2_BOUGHT + WT_EKBE1-DMBTR.

R2_GROSS = R2_GROSS + WT_EKBE1-DMBTR + T_MWSBP.

CLEAR T_MWSBP.

WT_TAB-BOUGHT1 = R2_BOUGHT.

WT_TAB-GROSS1 = R2_GROSS.

WT_TAB-WERKS = WT_EKPO-WERKS.

WHEN 'RAU3'.

R3_BOUGHT = R3_BOUGHT + WT_EKBE1-DMBTR.

R3_GROSS = R3_GROSS + WT_EKBE1-DMBTR + T_MWSBP.

CLEAR T_MWSBP.

WT_TAB-BOUGHT1 = R3_BOUGHT.

WT_TAB-GROSS1 = R3_GROSS.

WT_TAB-WERKS = WT_EKPO-WERKS.

WHEN 'RAU4'.

R4_BOUGHT = R4_BOUGHT + WT_EKBE1-DMBTR.

R4_GROSS = R4_GROSS + WT_EKBE1-DMBTR + T_MWSBP.

CLEAR T_MWSBP.

WT_TAB-BOUGHT1 = R4_BOUGHT.

WT_TAB-GROSS1 = R4_GROSS.

WT_TAB-WERKS = WT_EKPO-WERKS.

WHEN 'AP01'.

AP1_BOUGHT = AP1_BOUGHT + WT_EKBE1-DMBTR.

AP1_GROSS = AP1_GROSS + WT_EKBE1-DMBTR + T_MWSBP.

CLEAR T_MWSBP.

WT_TAB-BOUGHT1 = AP1_BOUGHT.

WT_TAB-GROSS1 = AP1_GROSS.

WT_TAB-WERKS = WT_EKPO-WERKS.

ENDCASE.

MODIFY IT_TAB FROM WT_TAB TRANSPORTING BOUGHT1

GROSS1

WERKS WHERE WERKS = WT_EKPO-WERKS.

IF SY-SUBRC NE 0.

APPEND WT_TAB TO IT_TAB.

CLEAR WT_TAB.

ENDIF.

ENDIF.

ENDIF.

ENDLOOP.

Regards

Madhu.

Former Member
0 Kudos
268

Hi Surender,

Try like this:

LOOP AT IT_EKKO1 INTO WT_EKKO1.

read table IT_EKBE1 with key EBELN EQ WT_EKBE1-EBELN.

if sy-subrc = 0

LOOP AT IT_EKBE1 INTO WT_EKBE1 from sy-index.

if WT_EKKO1-EBELN NE WT_EKBE1-EBELN

exit.

endif.

read table IT_EKPO with key EBELN EQ WT_EKBE1-EBELN

EBELP EQ WT_EKBE1-EBELP .

if sy-subrc = 0.

LOOP AT IT_EKPO INTO WT_EKPO.

if WT_EKBE1-EBELP ne WT_EKPO-EBELP or

WT_EKBE1-WERKS ne WT_EKPO-WERKS.

exit.

endif.

endloop.

endif.

endloop.

endif.

endloop.

Former Member
0 Kudos
268

hey you need to use joins to query the tables used. Inner joins will always give you better performance than loops. Any report that uses loops will affect performance in production client. Try to avoid loops or write a native sql query.

GrahamRobbo
Active Contributor
0 Kudos
268

Hi Suri,

you could certainly improve performance by using pointers to the ITAB lines rather than moving them to a working area in each pass of the loop.

So instead of...


data: itab type table of mytype,
      wa type mtype.
loop at itab into wa.
  IF wa-EBELN EQ wa-EBELN
...
endloop.

something like...


data: itab type table of mytype,
      lref type ref to mtype.
loop at itab reference into lref.
  IF lref->EBELN EQ lref->EBELN
...
endloop.

..would be much faster.

Many people use field symbols in the same way but my personal preference is for references.

Cheers

Graham Robbo

0 Kudos
268

>

> Many people use field symbols in the same way but my personal preference is for references.

I used one and the other, can't decide which one is better. Just curious why your preference is on reference, except that it rhymes

Thomas

0 Kudos
268

Hi Thomas,

I use references because they closely match my experience in other languages so they make more sense to me.

I don't believe that there is any performance difference between them - but have never checked.

Cheers

Graham Robbo

kesavadas_thekkillath
Active Contributor
0 Kudos
268

hi friend,

try to make a join for ekko and ekpo while selecting data so that u can neglect a loop.

try this


sort IT_EKKO1 by ebeln.
sort IT_EKbe1 by ebeln.
sort IT_EKpo by ebeln.
sort IT_TAB by werks.

LOOP AT IT_EKKO1 INTO WT_EKKO1.

    LOOP AT IT_EKBE1 INTO WT_EKBE1 where EBELN = WT_EKKO1-EBELN.      
  
      LOOP AT IT_EKPO INTO WT_EKPO where ebeln = WT_EKBE1-EBELN
				   and ebelp   =  WT_EKBE1-EBELP
				   and werks   = WT_EKBE1-WERKS
				   and WT_EKPO-MWSKZ NE SPACE.

            PERFORM TAX_CALCULATE_FUNCTION USING WT_EKKO1-BUKRS      "company code
                                                 WT_EKBE1-BUDAT     "posting date
                                                 WT_EKKO1-WAERS      "currency key
                                                 WT_EKPO-EBELN
                                                 WT_EKPO-EBELP      "CONDITION ITEM NUMBER
                                                 WT_EKPO-MWSKZ      "SALES TAX CODE
                                                 WT_EKPO-TXJCD      "TAX JURISIDICTION
                                                 WT_EKBE1-DMBTR      "AMOUNT IN DOCUMENT
                                                 WT_EKKO1-LIFNR      "VENDOR CODE
                                                 WT_EKKO1-LANDS      "COUNTRY KEY
                                                 WT_EKKO1-EKORG      "PURCHASE ORGANIZATION
                                                 WT_EKKO1-LLIEF      "SUPPLYING VENDOR
                                                 WT_EKKO1-BEDAT      "DOCUMENT DATE
                                                 WT_EKPO-MATNR      "material code
                                                 WT_EKPO-WERKS      "PLANT
                                                 WT_EKPO-BWTAR      "VALUATION TYPE
                                                 WT_EKPO-MATKL      "MATERIAL GROUP
                                                 WT_EKPO-MEINS      "BASE UNIT OF MEASURE
                                                 WT_EKBE1-MENGE      "QUANTITY
                                                 WT_EKPO-MTART      "MATERIAL TYPE
                                         CHANGING T_MWSBP.
              CASE WT_EKPO-WERKS.
              WHEN 'RAU1'.
                R1_BOUGHT = R1_BOUGHT + WT_EKBE1-DMBTR.
                R1_GROSS = R1_GROSS + WT_EKBE1-DMBTR + T_MWSBP.
                CLEAR T_MWSBP.
                WT_TAB-BOUGHT1 = R1_BOUGHT.
                WT_TAB-GROSS1 = R1_GROSS.
                WT_TAB-WERKS = WT_EKPO-WERKS.
              WHEN 'RAU2'.
                R2_BOUGHT = R2_BOUGHT + WT_EKBE1-DMBTR.
                R2_GROSS = R2_GROSS + WT_EKBE1-DMBTR + T_MWSBP.
                CLEAR T_MWSBP.
                WT_TAB-BOUGHT1 = R2_BOUGHT.
                WT_TAB-GROSS1 = R2_GROSS.
                WT_TAB-WERKS = WT_EKPO-WERKS.
              WHEN 'RAU3'.
                R3_BOUGHT = R3_BOUGHT + WT_EKBE1-DMBTR.
                R3_GROSS = R3_GROSS + WT_EKBE1-DMBTR + T_MWSBP.
                CLEAR T_MWSBP.
                WT_TAB-BOUGHT1 = R3_BOUGHT.
                WT_TAB-GROSS1 = R3_GROSS.
                WT_TAB-WERKS = WT_EKPO-WERKS.
              WHEN 'RAU4'.
                R4_BOUGHT  = R4_BOUGHT + WT_EKBE1-DMBTR.
                R4_GROSS = R4_GROSS + WT_EKBE1-DMBTR + T_MWSBP.
                CLEAR T_MWSBP.
                WT_TAB-BOUGHT1 = R4_BOUGHT.
                WT_TAB-GROSS1 = R4_GROSS.
                WT_TAB-WERKS = WT_EKPO-WERKS.
              WHEN 'AP01'.
                AP1_BOUGHT = AP1_BOUGHT + WT_EKBE1-DMBTR.
                AP1_GROSS = AP1_GROSS + WT_EKBE1-DMBTR + T_MWSBP.
                CLEAR T_MWSBP.
                WT_TAB-BOUGHT1 = AP1_BOUGHT.
                WT_TAB-GROSS1 = AP1_GROSS.
                WT_TAB-WERKS = WT_EKPO-WERKS.
            ENDCASE.
	Read table IT_TAB into WT_TAB transporting no fields with key werks = WT_EKPO-WERKS.
	if sy-subrc = 0.
 	MODIFY IT_TAB FROM WT_TAB TRANSPORTING BOUGHT1
                                               GROSS1
                                               WERKS WHERE WERKS = WT_EKPO-WERKS.
        else.
              APPEND WT_TAB TO IT_TAB.
              CLEAR WT_TAB.
        ENDIF.
ENDLOOP.
    ENDLOOP.
  ENDLOOP.

0 Kudos
268

LOOP AT...WHERE (and READ without BINARY SEARCH) will not help performance.

Rob

Former Member
0 Kudos
268

the solution is 65 % solved. so thanks to all friends. i have to try and upgrade the code for optimum response time in my next development.

Edited by: surender on Jan 28, 2009 1:38 PM

Edited by: surender on Jan 28, 2009 1:39 PM

former_member194613
Active Contributor
0 Kudos
268

> the solution is 65 % solved.

I have serious doubts that this is true. You don't know what the optimal solution would be, so you can only say that you solution is a bit faster.

Also the last recommendation, before Robs comment, is not really faster, it is still cubic coding!

You must use SORTED tables!