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

Code optimization

former_member321825
Active Participant
0 Likes
1,077

Hi All,

What is the best way to optimize the below code .


     SELECT kunnr
         bukrs
  FROM knb1
  INTO TABLE Zcustomerdata
  WHERE kunnr IN Zkunnr
  AND bukrs IN Zbukrs.

LOOP AT Zcustomerdata INTO lwa_customerdata.

   CALL FUNCTION 'BAPI_AR_ACC_GETSTATEMENT'
        EXPORTING
          companycode = lwa_customerdata-companycode
          customer    = lwa_customerdata-customer
          date_from   = lv_bapifrmdt
          date_to     = sy-datum
        IMPORTING
          return      = lwa_bapireturn
        TABLES
          lineitems   = Zlineitems.

LOOP AT Zlineitems INTO lwa_lineitems WHERE ( doc_type EQ 'DZ' or doc_type EQ 'ZP' )

                                                 AND entry_date in so_date and post_key NE '07'." lv_frodat

          IF NOT lwa_lineitems-INV_REF IS INITIAL.

               IF lwa_lineitems-nxt_doctyp = 'Z'.

*--Looping on the Invoice Document Number

                       LOOP AT Zlineitems INTO lwa_lineitems2 WHERE ( doc_type IN so_blart AND doc_no  = lwa_lineitems-inv_ref) .

-----

----     " Calculations

-----

endloop

endloop

endloop

4 REPLIES 4
Read only

Former Member
0 Likes
1,040

Hi Rachel,

Insted of using loop statement use READ Statement  with binary search by sorting the primary key fields.

it is condition that you must want to use nested loop then go for parallel cursor concept.

Hope useful,

Regards,

sudheer

Read only

former_member576008
Active Participant
0 Likes
1,040

I would suggest you to go with Parallel cursor and Parallel processing technique

Read only

former_member321825
Active Participant
0 Likes
1,040

Please check whether this is what u had suggested.

  SELECT   bukrs kunnr
  FROM knb1
  INTO corresponding fields TABLE Zcustomerdata
  WHERE kunnr IN Zkunnr
  AND bukrs IN Zbukrs.

Sort Zcustomerdata by bukrs kunnr.

LOOP AT Zcustomerdata INTO lwa_customerdata.

   CALL FUNCTION 'BAPI_AR_ACC_GETSTATEMENT'
        EXPORTING
          companycode = lwa_customerdata-companycode
          customer    = lwa_customerdata-customer
          date_from   = lv_bapifrmdt
          date_to     = sy-datum
        IMPORTING
          return      = lwa_bapireturn
        TABLES
          lineitems   = Zlineitems.

Sort Zlineitems by bukrs kunnr.

read Zlineitems INTO lwa_lineitems with key kunnr = lwa_customerdata–kunnr

  bukrs = lwa_customerdata–bukrs                                

       binary search.

  if sy-subrc = 0.  

          

    Zindex = sy-tabix.

LOOP AT Zlineitems INTO lwa_lineitems from Zindex

WHERE ( doc_type EQ 'DZ' or doc_type EQ 'ZP' )

                                                 AND entry_date in so_date and post_key NE '07'."

          IF NOT lwa_lineitems-INV_REF IS INITIAL.

               IF lwa_lineitems-nxt_doctyp = 'Z'.

*--Looping on the Invoice Document Number  ( DO i need to for the below loop as well ??????)

                       LOOP AT Zlineitems INTO lwa_lineitems2 WHERE ( doc_type IN so_blart AND doc_no  = lwa_lineitems-inv_ref) .

-----

----     " Calculations

-----

endloop

endloop

endloop

Read only

RaymondGiuseppi
Active Contributor
0 Likes
1,040

You could try to optimize/parallelize the execution of BAPI using some CALL FUNCTION - STARTING NEW TASK {CALLING meth}|{PERFORMING subr} ON END OF TASK DESTINATION IN GROUP {group|DEFAULT}.

  • First get number of free/available processes to use with FM SPBT_INITIALIZE (or be ready to monitor RESOURCE_FAILURE exception)
  • Then manage a counter of currently executing call, increment when calling, decrement in the method/form executed at end (where you read result with RECEIVE statement update your final internal tables), when no more processes available either use a WAIT UNTIL counter < threshold or call the FM without the STARTING NEW TASK.

/OR/

Don’t use the BAPI,

Analyze it and read KNC1 and BSID/BSAD tables to get the same result.

Regards,

Raymond