Quantcast
Channel: ABAP Development
Viewing all articles
Browse latest Browse all 948

Parallel Cursor : Performance for Nested Loops

$
0
0

In a lot of scenarios we come across where nesting loops is the only option we have to achieve what we want.

 

They can be hazardous if not tuned properly and can result in time out dumps and heavy processing time.

 

The solution : PARALLEL Cursor.

 

Lets assume a scenario where we are having 3 internal tables one with following structure.

 

KNA1

     Kunnr

     Name1 SORTED TABLE by KUNNR

 

VBRK

    Vbeln

     Kunag [Sold to will be equal to kunnr].

 

VBRP

     vbeln

     posnr

     matnr

     netwr [SORTED TABLE by VBELN]

 

Now it becomes tricky as we want to link KNA1 to VBRK and VBRK to VBRP.

 

Note : This is just to explain a three level nested loop if exact case is considered a read on KNA1 will be better.

 

Some thumb rules for Parallel Cursor

1. SORTING should be on the field which will part of where clause if you are not using.

     Like in this case KNA1 will be on KUNNR

     VBRK will be on KUNAG

     and VBRP will be on VBELN.

2. Recommended : use of variables to store tabix of the loop process.

 

Now the solution :

 

DATA : lv_vbrk_tabix  TYPE sy-tabix,

       lv_vbrp_tabix  TYPE sy-tabix.

 

SORT it_vbrk BY kunag.

 

lv_vbrk_tabix = 1.

 

LOOP AT it_kna1 INTO wa_kna1.

 

     WRITE:/5 'Name : ' , wa_kna1-name1 , ' | ' , wa_kna1-kunnr.

     ULINE.


     LOOP AT it_vbrk INTO wa_vbrk FROM lv_vbrk_tabix.

 

          IF wa_kna1-kunnr GT wa_vbrk-kunag.

            lv_vbrk_tabix = sy-tabix.

            EXIT. 

          ENDIF.

 

          CHECK wa_kna1-kunnr = wa_vbrk-kunag.

 

          WRITE:/10 wa_vbrk-vbeln , wa_vbrk-fkdat.

 

          READ TABLE it_vbrp WITH TABLE KEY vbeln = wa_vbrk-vbeln

                             TRANSPORTING NO FIELDS.

 

* AS IT_VBRP is a sorted table on VBELN default BINARY SEATCH will be used.         

 

          IF sy-subrc IS INITIAL.

            lv_vbrp_tabix = sy-tabix.

          ELSE.

            CONTINUE.

          ENDIF.

 

          LOOP AT it_vbrp INTO wa_vbrp FROM lv_vbrp_tabix.

 

            IF wa_vbrk-vbeln NE wa_vbrp-vbeln.

              EXIT. 

            ENDIF.

 

            WRITE:/15 wa_vbrp-matnr , 50 wa_vbrp-netwr.

 

          ENDLOOP.

 

     ENDLOOP.

 

ENDLOOP.

 

These are the 2 possible ways of parallel cursor.

 

Do comment if any issues or there is a better way...

 

BINARY SEARCH Removed and SORTED TABLE added Thanks for the input "Mathew Sir"


Viewing all articles
Browse latest Browse all 948

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>