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

Performance of nested loop

former_member219850
Participant
0 Likes
1,044

I want to improve performance of below code.

Please help..

LOOP AT gt_qmel_qmih INTO gs_qmel_qmih.

       lt_jcds[] = lt_jcds_tmp[].

       DELETE lt_jcds[] WHERE objnr NE gs_qmel_qmih-objnr.

       READ TABLE lt_jcds INTO ls_jcds WITH KEY objnr = gs_qmel_qmih-objnr.

       IF sy-subrc EQ 0.

         lv_index2 = sy-tabix.

       ELSE.

         EXIT.

       ENDIF.

       LOOP AT lt_jcds INTO ls_jcds FROM lv_index2.

         IF sy-tabix EQ 1.

           gs_tj30t-qmnum = gs_qmel_qmih-qmnum.

           gs_tj30t-txt04 = ls_jcds-txt04.

         APPEND gs_tj30t TO gt_tj30t.

         ENDIF.

         CLEAR:gs_tj30t,

               ls_jcds,

               gs_qmel_qmih.

         FREE :gt_jcds.

       ENDLOOP.

     ENDLOOP.

5 REPLIES 5
Read only

RaymondGiuseppi
Active Contributor
0 Likes
700

Well, first rewrite this awful code (and don't panic)


       lt_jcds[] = lt_jcds_tmp[].

       DELETE lt_jcds[] WHERE objnr NE gs_qmel_qmih-objnr.

When there are many notifications loaded, that will kill the performance (n times copy the whole itab and then delete records not required...) better use a sorted/hashed key on objnr and use a simple LOOP WHERE. (which will be optimized to use the key)


Regards,

Raymond

Read only

0 Likes
700

Please show sample code.

It will be really helpful...

Thanks,

Darshan.

Read only

0 Likes
700

No code required, just define the table for JCDS data as a TYPE SORTED or HASHED, then use a simple LOOP ON lt_jcds WHERE condition on OBJNR. (reference for performance on LOOP/WHERE)


Regards,

Raymond

Read only

0 Likes
700

Don't ask for sample code - search and you will find. Read the sap help on tables and indexes. If you want to know about secondary indexes, I wrote a blog about it. You can find that by searching as well.

The more effort you personally put into finding answers, the better you will learn.

Read only

Former Member
0 Likes
700

Hi Darshan panchal,

Once have a look at this code.

types : r_objnr type range of objnr,

lt_jcds[] = lt_jcds_tmp[].

LOOP AT gt_qmel_qmih INTO gs_qmel_qmih.

       r_objnr-sign = 'I'.

       r_objnr-option = 'EQ'.

       r_objnr-low    = gs_qmel_qmih-objnr.

   clear r_onjnr.

endloop.

delete lt_jcds[] where objnr not in r_objnr.

LOOP AT gt_qmel_qmih INTO gs_qmel_qmih.

       READ TABLE lt_jcds INTO ls_jcds WITH KEY objnr = gs_qmel_qmih-objnr.

       IF sy-subrc EQ 0.

         lv_index2 = sy-tabix.

       ELSE.

         EXIT.

       ENDIF.

       LOOP AT lt_jcds INTO ls_jcds FROM lv_index2.

         IF sy-tabix EQ 1.

           gs_tj30t-qmnum = gs_qmel_qmih-qmnum.

           gs_tj30t-txt04 = ls_jcds-txt04.

         APPEND gs_tj30t TO gt_tj30t.

         ENDIF.

         CLEAR:gs_tj30t,

               ls_jcds,

               gs_qmel_qmih.

         FREE :gt_jcds.

       ENDLOOP.

     ENDLOOP.

And There is no need of read statement for parallel cursor.Pls try this on your own