Application Development and Automation 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: 
Read only

function module within loop

ravi_kumar221
Participant
0 Likes
3,539

HI ,

I am using fuction module within loop

which is taking lots of time please tell me any trick how to do fast processing

my code is

LOOP AT IT_HD INTO WA_HD .

     var = var +  1.

     CALL FUNCTION 'BBP_PD_SC_GETDETAIL'

       EXPORTING

         I_GUID    = WA_HD-GUID

       IMPORTING

         E_HEADER  = E_HEADER

       TABLES

         E_ITEM    = E_ITEM

         E_ACCOUNT = E_ACCOUNT

         E_PARTNER = E_PARTNER

         E_STATUS  = E_STATUS .

   delete E_STATUS WHERE INACT 'X'.

   READ TABLE  E_STATUS INTO  WA_STATUS INDEX 1.

   select single TXT30 from TJ02T into TXT30 WHERE  ISTAT =   WA_STATUS-STAT and SPRAS = 'E'.

      loop at E_ITEM INTO WA_ITEM .

     READ TABLE  E_PARTNER INTO  WA_PARTNER WITH KEY PARTNER_FCT = '00000019'.

       READ TABLE IT_ORG INTO WA_ORG WITH KEY BE_PUR_GROUP  = WA_ITEM-BE_PUR_GROUP

                                                BE_PUR_ORG = WA_ITEM-BE_PUR_ORG

                                                BE_CO_CODE  = WA_ITEM-BE_CO_CODE.

       IF SY-SUBRC = 0.

         IF E_HEADER-POSTING_DATE IN  POS_DATE.

           SELECT SINGLE  BE_OBJECT_ID FROM BBP_PDBEI INTO WA_SC-BE_OBJECT_ID WHERE GUID = WA_ITEM-GUID.

           SELECT SINGLE NAME_ORG1 FROM BUT000 INTO WA_SC-NAME_ORG1 WHERE PARTNER_GUID  WA_PARTNER-PARTNER_NO .

         ENDIF.

             READ TABLE  E_PARTNER INTO  WA_PARTNER WITH KEY PARTNER_FCT = '00000019'.

           WA_SC-ZZSOCOREQ = TXT30.

         READ TABLE E_ACCOUNT INTO WA_ACCOUNT INDEX 1 .

         CALL FUNCTION 'OWN_LOGICAL_SYSTEM_GET'

           IMPORTING

             OWN_LOGICAL_SYSTEM = SYST_ID.

         .

         SELECT  SINGLE RFC_SYS FROM ZLOGICAL_SYS INTO RFC_SYS WHERE OWN_SYS = SYST_ID.

         IF SY-SUBRC = 0.

           CALL FUNCTION 'Z_SRM_PO_INFO' DESTINATION RFC_SYS

             EXPORTING

               BE_OBJECT_ID = WA_SC-BE_OBJECT_ID

               GL_CODE      = WA_ACCOUNT-G_L_ACCT

               COST_CTR     = WA_ACCOUNT-COST_CTR

               FUND         = WA_ACCOUNT-FUNDs_ctr

             IMPORTING

               AEDAT        = WA_SC-AEDAT

               GL_CODE_DES  = WA_SC-GL_CODE_DES

               C_C_D        = WA_SC-C_C_D

               FUND_DES     = WA_SC-FUND_DES.

           .

           CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'

             EXPORTING

               INPUT         WA_ITEM-ORDERED_PROD

            IMPORTING

              OUTPUT        =    WA_ITEM-ORDERED_PROD

                     .

           WA_SC-CREATED_BY = WA_HD-CREATED_BY .

           WA_SC-POSTING_DATE  E_HEADER-POSTING_DATE .

           WA_SC-BE_PUR_GROUP = WA_ITEM-BE_PUR_GROUP .

           WA_SC-BE_PUR_ORG   =   WA_ITEM-BE_PUR_ORG .

           WA_SC-BJECT_ID E_HEADER-OBJECT_ID .

           WA_SC-DESCRIPTION E_HEADER-DESCRIPTION.

           WA_SC-ORDERED_PROD = WA_ITEM-ORDERED_PROD.

           WA_SC-DESCRIPTION1 WA_ITEM-DESCRIPTION.

           WA_SC-QUANTITY     =   WA_ITEM-QUANTITY .

           WA_SC-CTR_HDR_NUMBER =   WA_ITEM-CTR_HDR_NUMBER .

           WA_SC-CTR_ITEM_NUMBER =   WA_ITEM-CTR_ITEM_NUMBER .

           WA_SC-CURRENCY    WA_ITEM-CURRENCY .

           WA_SC-GROSS_PRICE    WA_ITEM-PRICE *  WA_ITEM-QUANTITY.

           WA_SC-ACC_CAT  WA_ACCOUNT-ACC_CAT.

           WA_SC-COST_CTR  = WA_ACCOUNT-COST_CTR.

           WA_SC-ORDER_NO WA_ACCOUNT-ORDER_NO.

           WA_SC-NETWORK  = WA_ACCOUNT-NETWORK.

           WA_SC-ACTIVITY = WA_ACCOUNT-ACTIVITY.

           WA_SC-FUND  WA_ACCOUNT-FUNDs_ctr.

           WA_SC-ZZPRODUCT = WA_ACCOUNT-ZZPRODUCT.

           WA_SC-GL_CODE = WA_ACCOUNT-G_L_ACCT .

           IF  G_L_ACCT IS NOT INITIAL AND COST_CTR IS NOT INITIAL .

             READ TABLE E_ACCOUNT INTO WA_ACCOUNT INDEX 1.

             IF WA_ACCOUNT-G_L_ACCT = G_L_ACCT  AND WA_ACCOUNT-COST_CTR COST_CTR.

               APPEND WA_SC TO IT_SC .

                  ENDIF .

           ENDIF .

           IF  G_L_ACCT IS NOT INITIAL AND COST_CTR IS INITIAL .

             READ TABLE E_ACCOUNT INTO WA_ACCOUNT INDEX 1.

             IF WA_ACCOUNT-G_L_ACCT = G_L_ACCT  .

               APPEND WA_SC TO IT_SC .

             ENDIF .

           ENDIF .

           IF  G_L_ACCT IS INITIAL AND COST_CTR IS NOT INITIAL .

             READ TABLE E_ACCOUNT INTO WA_ACCOUNT INDEX 1.

             IF WA_ACCOUNT-COST_CTR = COST_CTR.

               APPEND WA_SC TO IT_SC .

             ENDIF .

           ENDIF .

           IF  G_L_ACCT IS INITIAL AND COST_CTR IS  INITIAL .

             APPEND WA_SC TO IT_SC .

           ENDIF .

         ENDIF .

       ENDIF .

   ENDLOOP .

    ENDLOOP .

11 REPLIES 11
Read only

Arun_Prabhu_K
Active Contributor
0 Likes
2,013

Hi Ravi kumar,

As per my observations of your code,

Reasons for large response time:

1) Loop inside loop

2) Read statements.

My suggestions:

1) Instead of using the first loop (LOOP AT IT_HD INTO WA_HD)

Make use of:

describe table it_hd.

itab_cnt = sy-tfill "No .of records in it_hd

cnt = 1.

while cnt <= itab_cnt.

read table it_hd index cnt

.

endwhile.

THIS WILL ELIMINATE THE SYSTEM'S LOAD TO TRACK INDEX OF TWO INTERNAL TABLES

2) Wherever READ is used, sort the internal tables and read them using binary search.

3) Following codes can be put outside the loop as they are static (no dependency on e_item)

(a)READ TABLE  E_PARTNER INTO  WA_PARTNER WITH KEY PARTNER_FCT = '00000019'.

(b) CALL FUNCTION 'OWN_LOGICAL_SYSTEM_GET'

       IMPORTING

         OWN_LOGICAL_SYSTEM = SYST_ID etc.

4) You can make use of FIELD-SYMBOLS for much faster access of the internal table (e_item)


Read only

Former Member
0 Likes
2,013

Hi Ravi,

Main Issues and advices are

1) Issue - Read Statements without BINARY SEARCH clause.

Advice - Sort all the internal tables in the sequence of fields which you are using in 'WHERE' Clause and then use BINARY SEARCH in all Read Statement. After Read statement check the sy-subrc value and then proceed.

2) Issue - Select Statements inside the Loop. Some Selects are inside second level of Loop. Since every Select Statement opens a DB Cursor, fetches Data and Closes it, it is very performance intensive.

Advice - Get all the entries into an internal table and then Do the Select Once for all using For All Entries.

* As suggested by Arun you can go for Field Symbols instead of any work area, it will be faster but be sure to make a check

if <FS> is assigned.

*-- Process

endif.

BR.

Read only

0 Likes
2,013

Well, the OP himself has indicated the main cause. Using Function modules inside a loop.

In this case that IS the main problem. So how to solve this?

This problem is that standard functions usually retrieve far more data than required.

The best way to get around this, is to check what data you need, and see if it is feasible to select it from the database (not by the FM, but by your own coding).

If you can do this, it will be a huge improvement. But it all depends on what data you need...

Then, you can look at the othe suggestions (READ w. BINARY SEARCH, Selecting into internal tables and (if you have records) do a SELECT FOR ALL ENTRIES.

But first things first. Try to get rid of the FM's.

Read only

gurunathkumar_dadamu
Active Contributor
0 Likes
2,013

Hi Ravi,

1. sort the internal table

2. while using the read statement use binary search, so that it will fetch the data as faster

3. Instead of loop within loop use parallel cursor method.this is the best technique while loop in loop case.

Let me know if any issues.

Regards,

Guru

Read only

0 Likes
2,013

please explain me  

parallel cursor method.

Read only

0 Likes
2,013

ravi kumar wrote:

please explain me  

parallel cursor method.

Plenty of documentation - please search.

Read only

0 Likes
2,013

Hi Ravi,

See this code

TABLES:

  likp,

  lips.

Data:

  t_likp  type table of likp,

  t_lips  type TABLE OF lips.

data:

  W_RUNTIME1 TYPE I,

  W_RUNTIME2 TYPE I,

  w_index LIKE sy-index.                " Using Parallel Cursor

START-OF-SELECTION.

select *

  from likp

  into table t_likp.

select *

  from lips

  into table t_lips.

get RUN TIME FIELD w_runtime1.

SORT t_likp BY vbeln.   " Using Parallel Cursor

SORT t_lips BY vbeln.   " Using Parallel Cursor

loop at t_likp into likp.

*  loop at t_lips into lips where vbeln eq likp-vbeln.

  LOOP AT t_lips INTO lips FROM w_index.                " Using Parallel Cursor

      IF likp-vbeln NE lips-vbeln.

        w_index = sy-tabix.

        EXIT.

      ENDIF.

    endloop.

endloop.

get RUN TIME FIELD w_runtime2.

w_runtime2 = w_runtime2 - w_runtime1.

write w_runtime2.

Read only

0 Likes
2,013

Hi Ravi,

please follow the Ramesh T,he was explained earlier post.

Read only

Former Member
0 Likes
2,013

Hi Ravi,

you need to do the following to increase the performance.

  1. validate SY-SUBRC after all commands for Read tables,select single.
  2. sort the table before using read table.
  3. read table with binary search.
  4. do not call the function without exceptions.
  5. work area should be cleared after every internal table is modified.
  6. avoid hard coding like 'E' or '000000019' instead use text elements or message class or define it has a consatnt.
  7. do not' select without where class

Regards, Suhas.

Read only

kiran_k8
Active Contributor
0 Likes
2,013

BBP_PD_SC_GETDETAIL is a RFC enabled function module that isgetting calling inside a loop which could be a cause of concern and needs to be checked.

K.Kiran.

Read only

ronaldo_aparecido
Contributor
0 Likes
2,013

Hi

1-Avoid duplication in internal table using 'Delete Adjacent Duplicate'.

     SELECT SINGLE FROM NAME_ORG1 BUT000 INTO WA_SC-NAME_ORG1 WHERE PARTNER_GUID = WA_PARTNER-PARTNER_NO.

2 - The command 'select single' above must contain the condition 'where' the whole key and 'PARTNER_GUID' is key, so much influence on performance see if the functional has the field 'PARTNER' which is the key of the table.

See if others 'select singles' are key to complete as much influence on performance.