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

fine tune the program for performance

Former Member
0 Likes
1,835

Moved to correct forum by moderator.

Hello gurus please give me suggestions to performance the code in a good way.

METHOD if_ex_mb_migo_badi~post_document . 
**********************************************************************
*The delivery note number provided by vendor should be unique  in migo
Importing it_mseg and is_mkpf 
**********************************************************************
  DATA : itab_mkpf TYPE TABLE OF mkpf.
  DATA : work_mseg TYPE mseg.
  DATA : itab1t_mseg TYPE TABLE OF mseg. 
  DATA : itab1_mseg TYPE TABLE OF mseg.

  LOOP AT it_mseg INTO work_mseg.

*  Fetching the delivery note if already exists
      SELECT mblnr mjahr xblnr FROM mkpf
                INTO CORRESPONDING FIELDS OF TABLE itab_mkpf
                                WHERE xblnr = is_mkpf-xblnr.

      IF sy-subrc EQ 0.

        IF work_mseg-bwart EQ '103' . 
          
          SELECT mblnr lifnr FROM mseg INTO  TABLE itab1_mseg
                             FOR ALL ENTRIES IN itab_mkpf
                                WHERE mblnr = itab_mkpf-mblnr
                                  AND mjahr = itab_mkpf-mjahr
                                  AND lifnr = work_mseg-lifnr.
          IF sy-subrc EQ 0.
            MESSAGE 'Delivery note already there for this Vendor' TYPE 'E'.
          ENDIF.

        ELSEIF work_mseg-bwart EQ '105'.

          SELECT mblnr mjahr lifnr  ebeln ebelp FROM mseg
                     INTO CORRESPONDING FIELDS OF TABLE itab1t_mseg
                                       WHERE ebeln = work_mseg-ebeln
                                         AND ebelp = work_mseg-ebelp.

          SELECT mblnr mjahr xblnr FROM mkpf
                     INTO CORRESPONDING FIELDS OF TABLE itab_mkpf
                                    FOR ALL ENTRIES IN itab1t_mseg
                                   WHERE mblnr = itab1t_mseg-mblnr
                                   AND   mjahr = itab1t_mseg-mjahr
                                   AND   xblnr = is_mkpf-xblnr.

          IF sy-subrc = 0.

            IF sy-subrc = 0. 
              SELECT mblnr lifnr  ebeln FROM mseg
                    INTO CORRESPONDING FIELDS OF TABLE  itab1_mseg
                                     FOR ALL ENTRIES IN itab_mkpf
                                    WHERE mblnr = itab_mkpf-mblnr
                                      AND mjahr = itab_mkpf-mjahr
                                      AND bwart <> '103'
                                      AND lifnr = work_mseg-lifnr.

              IF sy-subrc EQ 0.
                MESSAGE 'Delivery note already there for this Vendor' TYPE 'E'.
              ENDIF.
            ENDIF. 
          ELSE.
            MESSAGE 'Maintain the same delivery note number as maintained for 103 mvt type'              TYPE 'E'.
          ENDIF.
        ENDIF.
      ENDIF.

  ENDLOOP.
ENDMETHOD.

Edited by: Matt on Dec 24, 2008 7:33 PM - added tags

Moved to correct forum by moderator.

Hello gurus please give me suggestions to performance the code in a good way.

METHOD if_ex_mb_migo_badi~post_document . 
**********************************************************************
*The delivery note number provided by vendor should be unique  in migo
Importing it_mseg and is_mkpf 
**********************************************************************
  DATA : itab_mkpf TYPE TABLE OF mkpf.
  DATA : work_mseg TYPE mseg.
  DATA : itab1t_mseg TYPE TABLE OF mseg. 
  DATA : itab1_mseg TYPE TABLE OF mseg.

  LOOP AT it_mseg INTO work_mseg.

*  Fetching the delivery note if already exists
      SELECT mblnr mjahr xblnr FROM mkpf
                INTO CORRESPONDING FIELDS OF TABLE itab_mkpf
                                WHERE xblnr = is_mkpf-xblnr.

      IF sy-subrc EQ 0.

        IF work_mseg-bwart EQ '103' . 
          
          SELECT mblnr lifnr FROM mseg INTO  TABLE itab1_mseg
                             FOR ALL ENTRIES IN itab_mkpf
                                WHERE mblnr = itab_mkpf-mblnr
                                  AND mjahr = itab_mkpf-mjahr
                                  AND lifnr = work_mseg-lifnr.
          IF sy-subrc EQ 0.
            MESSAGE 'Delivery note already there for this Vendor' TYPE 'E'.
          ENDIF.

        ELSEIF work_mseg-bwart EQ '105'.

          SELECT mblnr mjahr lifnr  ebeln ebelp FROM mseg
                     INTO CORRESPONDING FIELDS OF TABLE itab1t_mseg
                                       WHERE ebeln = work_mseg-ebeln
                                         AND ebelp = work_mseg-ebelp.

          SELECT mblnr mjahr xblnr FROM mkpf
                     INTO CORRESPONDING FIELDS OF TABLE itab_mkpf
                                    FOR ALL ENTRIES IN itab1t_mseg
                                   WHERE mblnr = itab1t_mseg-mblnr
                                   AND   mjahr = itab1t_mseg-mjahr
                                   AND   xblnr = is_mkpf-xblnr.

          IF sy-subrc = 0.

            IF sy-subrc = 0. 
              SELECT mblnr lifnr  ebeln FROM mseg
                    INTO CORRESPONDING FIELDS OF TABLE  itab1_mseg
                                     FOR ALL ENTRIES IN itab_mkpf
                                    WHERE mblnr = itab_mkpf-mblnr
                                      AND mjahr = itab_mkpf-mjahr
                                      AND bwart <> '103'
                                      AND lifnr = work_mseg-lifnr.

              IF sy-subrc EQ 0.
                MESSAGE 'Delivery note already there for this Vendor' TYPE 'E'.
              ENDIF.
            ENDIF. 
          ELSE.
            MESSAGE 'Maintain the same delivery note number as maintained for 103 mvt type'              TYPE 'E'.
          ENDIF.
        ENDIF.
      ENDIF.

  ENDLOOP.
ENDMETHOD.

Edited by: Matt on Dec 24, 2008 7:33 PM - added tags

17 REPLIES 17
Read only

Former Member
0 Likes
1,787

Remove your selects from loop.. endloop ...

Read only

Former Member
0 Likes
1,787

Few basic things...

- Dont use selects inside the loop, if possible try and populate your table outside the loop and use the READ statement with Binary search in the loop.

- Instead of INTO CORRESPONDING FIELDS key in the actual field names.

- Also Use key fields in the where condition if available

Rgds

Narendra

Read only

Former Member
0 Likes
1,787

Hi,

don't use the select statements inside the loop...endloop.

select all the data from database and use read statement or loop on the internal tables.

then u can reduce the perfomance issue.

Read only

0 Likes
1,787

sorry in my code i msissed one statement.

METHOD if_ex_mb_migo_badi~post_document . 
**********************************************************************
*The delivery note number provided by vendor should be unique  in migo
Importing it_mseg and is_mkpf 
Work = wa
Itab1 = itt
Itab1t = ittt
Itab = ist
**********************************************************************
  DATA : itab_mkpf TYPE TABLE OF mkpf.
  DATA : work_mseg TYPE mseg.
  DATA : itab1t_mseg TYPE TABLE OF mseg. 
  DATA : itab1_mseg TYPE TABLE OF mseg.

  LOOP AT it_mseg INTO work_mseg.
if work_mseg-bukrs = '2122'."here i am checking for only one company code ,if it satisfies then only the rest of the code will execute.so I think using loop here wont degrade the performance

*  Fetching the delivery note if already exists
      SELECT mblnr mjahr xblnr FROM mkpf
                INTO CORRESPONDING FIELDS OF TABLE itab_mkpf
                                WHERE xblnr = is_mkpf-xblnr.

      IF sy-subrc EQ 0.

        IF work_mseg-bwart EQ '103' . 
          
          SELECT mblnr lifnr FROM mseg INTO  TABLE itab1_mseg
                             FOR ALL ENTRIES IN itab_mkpf
                                WHERE mblnr = itab_mkpf-mblnr
                                  AND mjahr = itab_mkpf-mjahr
                                  AND lifnr = work_mseg-lifnr.
          IF sy-subrc EQ 0.
            MESSAGE 'Delivery note already there for this Vendor' TYPE 'E'.
          ENDIF.

        ELSEIF work_mseg-bwart EQ '105'.

          SELECT mblnr mjahr lifnr  ebeln ebelp FROM mseg
                     INTO CORRESPONDING FIELDS OF TABLE itab1t_mseg
                                       WHERE ebeln = work_mseg-ebeln
                                         AND ebelp = work_mseg-ebelp.

          SELECT mblnr mjahr xblnr FROM mkpf
                     INTO CORRESPONDING FIELDS OF TABLE itab_mkpf
                                    FOR ALL ENTRIES IN itab1t_mseg
                                   WHERE mblnr = itab1t_mseg-mblnr
                                   AND   mjahr = itab1t_mseg-mjahr
                                   AND   xblnr = is_mkpf-xblnr.

          IF sy-subrc = 0.

            IF sy-subrc = 0. 
              SELECT mblnr lifnr  ebeln FROM mseg
                    INTO CORRESPONDING FIELDS OF TABLE  itab1_mseg
                                     FOR ALL ENTRIES IN itab_mkpf
                                    WHERE mblnr = itab_mkpf-mblnr
                                      AND mjahr = itab_mkpf-mjahr
                                      AND bwart <> '103'
                                      AND lifnr = work_mseg-lifnr.

              IF sy-subrc EQ 0.
                MESSAGE 'Delivery note already there for this Vendor' TYPE 'E'.
              ENDIF.
            ENDIF. 
          ELSE.
            MESSAGE 'Maintain the same delivery note number as maintained for 103 mvt type'              TYPE 'E'.
          ENDIF.
        ENDIF.
      ENDIF.
Endif.
  ENDLOOP.
ENDMETHOD.

Edited by: Matt on Dec 24, 2008 7:34 PM

Read only

0 Likes
1,787

hi,

all the code what u wrote inside the badi MB_MIGO_BADI is not required....

all the required fields are available in the parameter it_mseg and is_mkpf..

using this 2 parameter's, u can validate and acheive your requirement..

because u wrote soo many codes inside the BADI, this is not neccessary because u can use the parameter directly..

Read only

0 Likes
1,787

gurus could anyone please guide me further where exactly i can make the improvements.

Thank you for all the replies

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,787

Hello Madan,

I see that in the code you have 2 selects from MKPF using XBLNR. I had a look @ MKPF & there is no default INDEX on XBLNR.

My suggestion is to check if you have an index on XBLNR, if not create one.

Hope this helps.

BR,

Suhas

Read only

matt
Active Contributor
0 Likes
1,787

>

> sorry in my code i msissed one statement.

You also missed tags around your code. I've put them in for you. Doesn't it look nicer!

Read only

0 Likes
1,787

thank you Matt , now my code looks good so that anyone can understand and help me to change the code.

Gurus please guide where further cahnges are required.

Read only

0 Likes
1,787

>

> thank you Matt , now my code looks good so that anyone can understand and help me to change the code.

>

> Gurus please guide where further cahnges are required.

Have you adjust your code based on people advises in here?

Regards,

Abraham

Read only

0 Likes
1,787

any other replys please

Read only

0 Likes
1,787

I think following three points you need to take care of:

1. Try to remove move-corresponding

2. Select query shouldnt be used in any loop...endloop.

3. If you nned to process for bukrs = '2122' then insted of looping every records use

 LOOP AT it_mseg  where bukrs = '2122'.

Read only

Former Member
0 Likes
1,787

Remove INTO CORRESPONDING FIELDS

while selecting for all entries take all the key fields in select statment whether u need it or not.

Avoid hitting one table multiple times rather select in one go and use read to filter the data.

Hope this helps.

Regards

Bikas

Read only

Former Member
0 Likes
1,787

Hi,

If you write DB queries inside the loop it will affect the performance.

What you can do is create ranges for fields like

work_mseg-ebeln, work_mseg-ebelp etc then use those ranges in select statement.

Also instead of into corresponding give the field name in the same order as your internal table,

in case ur int tab is same as db tab structure dont use corresponding.

Ex.

RANGES: r_lifnr FOR mkpf-lifnr.

LOOP AT it_mseg INTO work_mseg.
"your conditions
    r_lifnr-sign = 'I'.
    r_lifnr-option = 'EQ'.
    r_lifnr-low = work_mseg-lifnr.
    APPEND lr_cust.
"You can fill other ranges here 
  ENDLOOP.


SELECT mblnr lifnr FROM mseg INTO TABLE itab1_mseg
FOR ALL ENTRIES IN itab_mkpf
WHERE mblnr = itab_mkpf-mblnr
AND mjahr = itab_mkpf-mjahr
AND lifnr IN r_lifnr. "<=use your range here

Regards,

Manoj Kumar P

Read only

Former Member
0 Likes
1,787

Hi Madan,

1) hitting the BSEG with in the loop leads to bad performance. instead select the data from the BSEG and process the records based on your requirement.

2)Don't use the "INTO CORRESPONDING FIELDS " instead take the actual fields.

3) as BSEG is the cluster table u need to use the all the primary key values in the where conditon. other wise data will not select properly.

for the BSEG primary key fields are BUKRS,BELNR,GJAHR,BUZEI.

hope these tips are useful

Read only

Former Member
0 Likes
1,787

Your whole logic is quite strange, please revise completely!

Use a sorted table for it_mseg! otherwise you will always loop the full table:

Then divide into logic for the branches, and be aware that the first select is independent

of the loop.


      SELECT mblnr mjahr xblnr FROM mkpf
                INTO CORRESPONDING FIELDS OF TABLE itab_mkpf
                                WHERE xblnr = is_mkpf-xblnr.

  LOOP AT it_mseg INTO work_mseg where work_mseg-bwart EQ '103' . 
    IF NOT itab_mkbf IS INOITIAL.
          SELECT mblnr lifnr FROM mseg INTO  TABLE itab1_mseg
                       FOR ALL ENTRIES IN itab_mkpf
                       WHERE mblnr = itab_mkpf-mblnr
                        AND mjahr = itab_mkpf-mjahr
                       AND lifnr = work_mseg-lifnr.
          IF sy-subrc EQ 0.
            MESSAGE 'Delivery note already there for this Vendor' TYPE 'E'.
          ENDIF.


    ENDIF.
  ENDLOOP


  LOOP AT it_mseg INTO work_mseg where work_mseg-bwart EQ '105' . 

   see below
....

    ENDIF.
  ENDLOOP

Check whether you can change this into a join!


          SELECT mblnr mjahr lifnr  ebeln ebelp FROM mseg
                     INTO CORRESPONDING FIELDS OF TABLE itab1t_mseg
                                       WHERE ebeln = work_mseg-ebeln
                                         AND ebelp = work_mseg-ebelp.
 
          SELECT mblnr mjahr xblnr FROM mkpf
                     INTO CORRESPONDING FIELDS OF TABLE itab_mkpf
                                    FOR ALL ENTRIES IN itab1t_mseg
                                   WHERE mblnr = itab1t_mseg-mblnr
                                   AND   mjahr = itab1t_mseg-mjahr
                                   AND   xblnr = is_mkpf-xblnr.
 
          
            IF sy-subrc = 0. 
              SELECT mblnr lifnr  ebeln FROM mseg
                    INTO CORRESPONDING FIELDS OF TABLE  itab1_mseg
                                     FOR ALL ENTRIES IN itab_mkpf
                                    WHERE mblnr = itab_mkpf-mblnr
                                      AND mjahr = itab_mkpf-mjahr
                                      AND bwart  '103'
                                      AND lifnr = work_mseg-lifnr.

Siegfried

Read only

Former Member
0 Likes
1,787

This message was moderated.