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: 

Hi how to avoid nested loops in this program to improve the performence

Former Member
0 Kudos

Hi all

How to avoide the nested loops in this programing what is the replacement for the nested loops in this program coding......

LOOP AT itb_ekpo.

READ TABLE itb_marc WITH KEY

matnr = itb_ekpo-matnr

werks = itb_ekpo-werks BINARY SEARCH.

CHECK sy-subrc = 0.

  • FAE 26446 fin remplacement

itb_pca-ebeln = itb_ekpo-ebeln.

itb_pca-ebelp = itb_ekpo-ebelp.

  • itb_pca-lifnr = itb_ekko-lifnr. "-FAE26446

itb_pca-lifnr = itb_ekpo-lifnr. "+FAE26446

itb_pca-ekgrp = itb_ekpo-ekgrp. "+FAE26446

itb_pca-dispo = itb_ekpo-dispo. "+FAE26446

itb_pca-matnr = itb_ekpo-matnr.

itb_pca-werks = itb_ekpo-werks.

  • Recherche du libellé article

READ TABLE itb_makt

WITH KEY matnr = itb_ekpo-matnr

spras = text-fra

BINARY SEARCH.

IF sy-subrc = 0.

itb_pca-maktx = itb_makt-maktx.

ELSE.

READ TABLE itb_makt

WITH KEY matnr = itb_ekpo-matnr

spras = text-ang

BINARY SEARCH.

IF sy-subrc = 0.

itb_pca-maktx = itb_makt-maktx.

ENDIF.

ENDIF.

IF NOT itb_ekpo-bpumn IS INITIAL.

itb_pca-menge = itb_ekpo-menge * itb_ekpo-bpumz /

itb_ekpo-bpumn.

ENDIF.

  • Sélect° ds la table EKES des dates de livraisons et des qtés

  • en transit

CLEAR w_temoin_ar.

CLEAR w_etens.

LOOP AT itb_ekes

FROM w_index_ekes.

IF itb_ekes-ebeln = itb_ekpo-ebeln

AND itb_ekes-ebelp = itb_ekpo-ebelp.

IF itb_ekes-ebtyp = text-arn.

itb_pca-eindt = itb_ekes-eindt.

w_temoin_ar = 'X'.

ELSE.

  • Si c'est une qté en transit alors on recupere

  • la quantité et la date.

IF itb_ekes-dabmg < itb_ekes-menge.

itb_pca-qtran = itb_pca-qtran + itb_ekes-menge -

itb_ekes-dabmg.

ENDIF.

IF itb_ekes-etens > w_etens.

w_etens = itb_ekes-etens.

itb_pca-dtran = itb_ekes-eindt.

ENDIF.

ENDIF.

ELSEIF itb_ekes-ebeln > itb_ekpo-ebeln

OR ( itb_ekes-ebeln = itb_ekpo-ebeln

AND itb_ekes-ebelp > itb_ekpo-ebelp ).

w_index_ekes = sy-tabix.

EXIT.

ENDIF.

ENDLOOP.

  • S'il n'y a pas d'AR alors récupérat° de la date livraison dans EKET.

LOOP AT itb_eket

FROM w_index_eket.

IF itb_eket-ebeln = itb_ekpo-ebeln

AND itb_eket-ebelp = itb_ekpo-ebelp.

IF w_temoin_ar IS INITIAL.

itb_pca-eindt = itb_eket-eindt.

ENDIF.

itb_pca-slfdt = itb_eket-slfdt.

  • Calcul du portefeuille fournisseur à partir de la

  • qté commandée et la qté reçue

itb_pca-attdu = itb_pca-attdu + itb_eket-menge -

itb_eket-wemng.

  • Calcul du montant du poste

itb_pca-netpr = itb_ekpo-netpr * itb_pca-attdu.

IF itb_ekpo-peinh NE 0.

itb_pca-netpr = itb_pca-netpr / itb_ekpo-peinh.

ENDIF.

  • Calcul quantité réceptionnée.

itb_pca-wemng = itb_pca-wemng + itb_eket-wemng.

  • Calcul du retard en nombre de jours calendaires

  • Le calcul du retard ne doit pas prendre en compte

  • le jour de livraison

ADD 1 TO itb_eket-eindt.

IF NOT itb_pca-attdu IS INITIAL

AND itb_eket-eindt LT sy-datum.

  • Calcul du retard en nombre de jours ouvrés

CLEAR w_retard.

CALL FUNCTION 'Z_00_BC_WORKDAYS_PER_PERIOD'

EXPORTING

date_deb = itb_eket-eindt

date_fin = sy-datum

IMPORTING

jours = w_retard.

itb_pca-rtard = itb_pca-rtard + w_retard .

ENDIF.

ELSEIF itb_eket-ebeln > itb_ekpo-ebeln

OR ( itb_eket-ebeln = itb_ekpo-ebeln

AND itb_eket-ebelp > itb_ekpo-ebelp ).

w_index_eket = sy-tabix.

EXIT.

ENDIF.

ENDLOOP.

  • Recherche de la derniere date de livraison.

LOOP AT itb_mseg

FROM w_index_mseg.

IF itb_mseg-ebeln = itb_ekpo-ebeln

AND itb_mseg-ebelp = itb_ekpo-ebelp.

READ TABLE itb_mkpf

WITH KEY mblnr = itb_mseg-mblnr

mjahr = itb_mseg-mjahr

BINARY SEARCH.

IF sy-subrc = 0.

IF itb_mkpf-bldat > itb_pca-bldat.

itb_pca-bldat = itb_mkpf-bldat.

ENDIF.

ENDIF.

ELSEIF itb_mseg-ebeln > itb_ekpo-ebeln

OR ( itb_mseg-ebeln = itb_ekpo-ebeln

AND itb_mseg-ebelp > itb_ekpo-ebelp ).

w_index_mseg = sy-tabix.

EXIT.

ENDIF.

ENDLOOP.

APPEND itb_pca.

CLEAR itb_pca.

  • FAE26446 suppression parag. suivant

  • ELSEIF itb_ekpo-ebeln > itb_ekko-ebeln.

  • w_index_ekpo = sy-tabix.

  • EXIT.

  • ENDIF.

  • ENDLOOP.

  • Fin FAE26446

ENDLOOP.

Thanks in advance for all.....

2 REPLIES 2

kiran_k8
Active Contributor
0 Kudos

Sudhakar,

Seems the Client is a French Company.To improve performance see to that you use only primary key fields or index fields in the where clause of the select queries.This will drastically improve the performance.

K.Kiran.

Former Member
0 Kudos

Hi

these are the performance tips

Instead of using nested Select loops it is often better to use subqueries.

SELECT * FROM SPFLI

INTO TABLE T_SPFLI

WHERE CITYFROM = 'FRANKFURT'

AND CITYTO = 'NEW YORK'.

SELECT * FROM SFLIGHT AS F

INTO SFLIGHT_WA

FOR ALL ENTRIES IN T_SPFLI

WHERE SEATSOCC < F~SEATSMAX

AND CARRID = T_SPFLI-CARRID

AND CONNID = T_SPFLI-CONNID

AND FLDATE BETWEEN '19990101' AND '19990331'.

ENDSELECT.

The above mentioned code can be even more optimized by using subqueries instead of for all entries.

SELECT * FROM SFLIGHT AS F INTO SFLIGHT_WA

WHERE SEATSOCC < F~SEATSMAX

AND EXISTS ( SELECT * FROM SPFLI

WHERE CARRID = F~CARRID

AND CONNID = F~CONNID

AND CITYFROM = 'FRANKFURT'

AND CITYTO = 'NEW YORK' )

AND FLDATE BETWEEN '19990101' AND '19990331'.

ENDSELECT.

<b>Internal Tables</b>

1. Table operations should be done using explicit work areas rather than via header lines.

2. Always try to use binary search instead of linear search. But don’t forget to sort your internal table before that.

3. A dynamic key access is slower than a static one, since the key specification must be evaluated at runtime.

4. A binary search using secondary index takes considerably less time.

5. LOOP ... WHERE is faster than LOOP/CHECK because LOOP ... WHERE evaluates the specified condition internally.

6. Modifying selected components using “ MODIFY itab …TRANSPORTING f1 f2.. “ accelerates the task of updating a line of an internal table.

Point # 2

READ TABLE ITAB INTO WA WITH KEY K = 'X‘ BINARY SEARCH.

IS MUCH FASTER THAN USING

READ TABLE ITAB INTO WA WITH KEY K = 'X'.

If TAB has n entries, linear search runs in O( n ) time, whereas binary search takes only O( log2( n ) ).

Point # 3

READ TABLE ITAB INTO WA WITH KEY K = 'X'. IS FASTER THAN USING

READ TABLE ITAB INTO WA WITH KEY (NAME) = 'X'.

Point # 5

LOOP AT ITAB INTO WA WHERE K = 'X'.

" ...

ENDLOOP.

The above code is much faster than using

LOOP AT ITAB INTO WA.

CHECK WA-K = 'X'.

" ...

ENDLOOP.

Point # 6

WA-DATE = SY-DATUM.

MODIFY ITAB FROM WA INDEX 1 TRANSPORTING DATE.

The above code is more optimized as compared to

WA-DATE = SY-DATUM.

MODIFY ITAB FROM WA INDEX 1.

7. Accessing the table entries directly in a "LOOP ... ASSIGNING ..." accelerates the task of updating a set of lines of an internal table considerably

8. If collect semantics is required, it is always better to use to COLLECT rather than READ BINARY and then ADD.

9. "APPEND LINES OF itab1 TO itab2" accelerates the task of appending a table to another table considerably as compared to “ LOOP-APPEND-ENDLOOP.”

10. “DELETE ADJACENT DUPLICATES“ accelerates the task of deleting duplicate entries considerably as compared to “ READ-LOOP-DELETE-ENDLOOP”.

11. "DELETE itab FROM ... TO ..." accelerates the task of deleting a sequence of lines considerably as compared to “ DO -DELETE-ENDDO”.

Point # 7

Modifying selected components only makes the program faster as compared to Modifying all lines completely.

e.g,

LOOP AT ITAB ASSIGNING <WA>.

I = SY-TABIX MOD 2.

IF I = 0.

<WA>-FLAG = 'X'.

ENDIF.

ENDLOOP.

The above code works faster as compared to

LOOP AT ITAB INTO WA.

I = SY-TABIX MOD 2.

IF I = 0.

WA-FLAG = 'X'.

MODIFY ITAB FROM WA.

ENDIF.

ENDLOOP.

Point # 8

LOOP AT ITAB1 INTO WA1.

READ TABLE ITAB2 INTO WA2 WITH KEY K = WA1-K BINARY SEARCH.

IF SY-SUBRC = 0.

ADD: WA1-VAL1 TO WA2-VAL1,

WA1-VAL2 TO WA2-VAL2.

MODIFY ITAB2 FROM WA2 INDEX SY-TABIX TRANSPORTING VAL1 VAL2.

ELSE.

INSERT WA1 INTO ITAB2 INDEX SY-TABIX.

ENDIF.

ENDLOOP.

The above code uses BINARY SEARCH for collect semantics. READ BINARY runs in O( log2(n) ) time. The above piece of code can be more optimized by

LOOP AT ITAB1 INTO WA.

COLLECT WA INTO ITAB2.

ENDLOOP.

SORT ITAB2 BY K.

COLLECT, however, uses a hash algorithm and is therefore independent

of the number of entries (i.e. O(1)) .

Point # 9

APPEND LINES OF ITAB1 TO ITAB2.

This is more optimized as compared to

LOOP AT ITAB1 INTO WA.

APPEND WA TO ITAB2.

ENDLOOP.

Point # 10

DELETE ADJACENT DUPLICATES FROM ITAB COMPARING K.

This is much more optimized as compared to

READ TABLE ITAB INDEX 1 INTO PREV_LINE.

LOOP AT ITAB FROM 2 INTO WA.

IF WA = PREV_LINE.

DELETE ITAB.

ELSE.

PREV_LINE = WA.

ENDIF.

ENDLOOP.

Point # 11

DELETE ITAB FROM 450 TO 550.

This is much more optimized as compared to

DO 101 TIMES.

DELETE ITAB INDEX 450.

ENDDO.

12. Copying internal tables by using “ITAB2[ ] = ITAB1[ ]” as compared to “LOOP-APPEND-ENDLOOP”.

13. Specify the sort key as restrictively as possible to run the program faster.

Point # 12

ITAB2[] = ITAB1[].

This is much more optimized as compared to

REFRESH ITAB2.

LOOP AT ITAB1 INTO WA.

APPEND WA TO ITAB2.

ENDLOOP.

Point # 13

“SORT ITAB BY K.” makes the program runs faster as compared to “SORT ITAB.”

Internal Tables contd…

Hashed and Sorted tables

1. For single read access hashed tables are more optimized as compared to sorted tables.

2. For partial sequential access sorted tables are more optimized as compared to hashed tables

Hashed And Sorted Tables

Point # 1

Consider the following example where HTAB is a hashed table and STAB is a sorted table

DO 250 TIMES.

N = 4 * SY-INDEX.

READ TABLE HTAB INTO WA WITH TABLE KEY K = N.

IF SY-SUBRC = 0.

" ...

ENDIF.

ENDDO.

This runs faster for single read access as compared to the following same code for sorted table

DO 250 TIMES.

N = 4 * SY-INDEX.

READ TABLE STAB INTO WA WITH TABLE KEY K = N.

IF SY-SUBRC = 0.

" ...

ENDIF.

ENDDO.

Point # 2

Similarly for Partial Sequential access the STAB runs faster as compared to HTAB

LOOP AT STAB INTO WA WHERE K = SUBKEY.

" ...

ENDLOOP.

This runs faster as compared to

LOOP AT HTAB INTO WA WHERE K = SUBKEY.

" ...

ENDLOOP.

<b>Reward if usefufll</b>