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

please help me optimize this code..

Former Member
0 Likes
552

i have already checked the indexes of the tables and used the primary keys.


 r_datum-sign = gco_sign_i.
  r_datum-low  = docdate_from.

  IF NOT docdate_to IS INITIAL.
    r_datum-option = gco_option_bt.
    r_datum-high   = docdate_to.

  ELSE.
    r_datum-option = gco_option_eq.
  ENDIF.

  APPEND r_datum.
  CLEAR r_datum.

  r_vbeln-sign = gco_sign_i.
  r_vbeln-low  = docnum_from.

  IF NOT docnum_to IS INITIAL.
    r_vbeln-option = gco_option_bt.
    r_vbeln-high   = docnum_to.

  ELSE.
    r_vbeln-option = gco_option_eq.
  ENDIF.

  APPEND r_vbeln.
  CLEAR r_vbeln.

  process_web_rset salesgroup
                   r_vkgrp
                   gco_sign_i
                   gco_option_eq.

* Get Total (Confirmed) Qty
  PERFORM get_valid_itemcateg TABLES gr_pstyv.

* Header
  SELECT   vbak~vbeln
           vbkd~inco1
           vbak~erdat
           vbak~gueen
           vbak~ernam
           vbkd~bstkd
           vbak~kunnr
  APPENDING CORRESPONDING FIELDS OF TABLE lt_hdr
  FROM  vbak INNER JOIN vbkd
  ON  vbak~vbeln EQ vbkd~vbeln
  WHERE vbak~vbeln IN r_vbeln
                AND  vbak~erdat IN r_datum
                AND  vbak~vkgrp IN r_vkgrp
                AND  vbak~kunnr EQ customer
                AND  vbkd~posnr EQ lv_posnr
.

  SORT lt_hdr BY vbeln.

  LOOP AT lt_hdr.
* Customer Name
    SELECT SINGLE  kna1~name1
             INTO  lv_cusname
             FROM  kna1
             WHERE kna1~kunnr EQ lt_hdr-kunnr.

    MOVE lt_hdr-vbeln TO listing-sa.
    MOVE lt_hdr-ernam TO listing-ordered_thru.
    MOVE lt_hdr-bstkd TO listing-po.
    MOVE lt_hdr-kunnr TO listing-customer.
    MOVE lv_cusname TO listing-customer_name.

    MOVE lt_hdr-erdat TO listing-date_created.
    MOVE lt_hdr-gueen TO listing-valid_until.


 CONCATENATE lt_hdr-erdat+6(2) lt_hdr-erdat+4(2) lt_hdr-erdat+0(4) INTO
                   listing-date_created SEPARATED BY '.'.

 CONCATENATE  lt_hdr-gueen+6(2) lt_hdr-gueen+4(2) lt_hdr-gueen+0(4) INTO
                            listing-valid_until SEPARATED BY '.'.

* Get Mode of Sale
    IF lt_hdr-inco1 EQ 'FOB'.
      listing-incoterms = co_inco1.

    ELSEIF lt_hdr-inco1 EQ 'CIF'.
      listing-incoterms = co_inco2.
    ENDIF.

* Get Status
    SELECT SINGLE  bezei
             FROM  tvbst JOIN vbuk
               ON  tvbst~statu EQ vbuk~gbstk
             INTO  listing-status
            WHERE  tbnam      EQ co_tbnam
              AND  spras      EQ co_spras
              AND  fdnam      EQ co_fdnam
              AND  vbuk~vbeln EQ listing-sa.

* Get Ship to party
    SELECT SINGLE  name1
             FROM  vbpa INNER JOIN kna1
               ON  vbpa~kunnr = kna1~kunnr
             INTO  listing-ship_to
            WHERE  vbeln EQ listing-sa
              AND  posnr EQ lv_posnr
              AND  parvw EQ 'WE'.

* Get Plant
    SELECT SINGLE  vbap~werks
                   t001w~name1
             FROM  vbap INNER JOIN t001w
               ON  vbap~werks EQ t001w~werks
             INTO (listing-plant,
                   listing-plant_text)
            WHERE  vbap~vbeln EQ listing-sa
            .

* Items
    SELECT SINGLE
          a~matnr
          a~pstyv
          b~bmeng
          b~vrkme
  INTO (lv_matnr,
  lv_pstyv,
  lv_bmeng,
  lv_vrkme)
    FROM  vbap AS a INNER JOIN vbep AS b
      ON  a~vbeln = b~vbeln
     AND  a~posnr = b~posnr
   WHERE  a~vbeln = lt_hdr-vbeln
   AND a~posnr EQ co_posnr
   AND a~pstyv IN gr_pstyv
   .




* Use zshowitem to identify items for display on SO
    lv_convvl = 1.

*   Convert Quantity
    PERFORM check_conversion USING: lv_bmeng
                                    listing-total_uom
                                    lv_pstyv
                                    lv_vrkme
                               lv_matnr.  "<<CR002-DEVK944861 ins

    IF listing-total_uom IS INITIAL.
      MOVE lv_vrkme TO listing-total_uom.
    ENDIF.


    IF listing-total_uom IS INITIAL.
      MOVE lv_vrkme TO listing-total_uom.
    ENDIF.

    vl_sum1 = vl_sum1 + lv_bmeng.

*    ENDLOOP.
    listing-total_qty = vl_sum1.


    REFRESH lv_itab.
    CLEAR: vl_sum1,
           vl_sum2.



* Scheduling Agreement Item and Served Qty
    SELECT SINGLE
            b~vbeln AS vbeln2
            b~rfmng
            b~meins
  INTO (lv_vbeln2,
        lv_rfmng,
        lv_meins)
      FROM  vbap AS a INNER JOIN vbfa AS b
      ON a~vbeln EQ b~vbelv
      AND a~posnr EQ b~posnv
     WHERE a~vbeln = lt_hdr-vbeln
     AND a~pstyv IN gr_pstyv
     AND b~vbtyp_n EQ co_vbtyp_n.
    IF sy-subrc EQ 0.

*   Goods Issue Status per Item
      SELECT SINGLE  wbstk
               FROM  vbuk
               INTO  vl_drstatus
              WHERE  vbeln = lv_vbeln2.

*   Convert Quantity
      PERFORM check_conversion USING: lv_rfmng
                                      listing-served_uom
                                      lv_pstyv
                                      lv_meins
                               lv_matnr. " <<CR002-DEVK944861 ins

*   Pass Value
      IF listing-served_uom IS INITIAL.
        MOVE lv_meins TO listing-served_uom.
      ENDIF.

      vl_served_qty = vl_served_qty + lv_rfmng.


      CLEAR: lt_scheditm,
             vl_drstatus,
             vl_sum3,
             vl_meins.

    ENDIF.



    listing-served_qty = vl_served_qty.
    CLEAR vl_served_qty.

* Append List
    APPEND listing.
    CLEAR: lt_hdr,
           lv_itab,
           lt_scheditm,
           listing,
           vl_datum1,
           vl_datum2,
           vl_sum1,
           vl_sum2,
           vl_sum3.

  ENDLOOP.


  IF sy-subrc <> 0.
    CALL FUNCTION 'BALW_BAPIRETURN_GET2'
         EXPORTING
              type   = 'W'
              cl     = 'ZECOM'
              number = 000
         IMPORTING
              return = return.

  ELSE.
    CALL FUNCTION 'BALW_BAPIRETURN_GET2'
         EXPORTING
              type   = 'S'
              cl     = 'ZECOM'
              number = 001
         IMPORTING
              return = return.
  ENDIF.


ENDFUNCTION.

1 ACCEPTED SOLUTION
Read only

alison_lloyd
Active Participant
0 Likes
504

As mentioned by others avoid into corresponding fields.

If you have to join vbak/vbkd then at least try to use

Sales Organization

Distribution Channel

Division

FOr customer name do n ot select for each document, select for all entries in lt_hdr_aux before the loop where lt_hdr_aux[] = lt_hdr[] with adjacent duplicates by customer deleted and then in the loop use read instead.

Don't use inner joins on vbap to other tables instead select vbap information for all documents selected and then read.

etc etc

Use st05 to review which table accesses are giving you a problem and concentrate first on those taking into account

- not to retrieve data multiple times e.g. is customer is cust1 for deoc1 and for doc 10 then only retrieve once as detailed above.

- optimise the actual select statement - no into corresponding, using keys etc

3 REPLIES 3
Read only

Former Member
0 Likes
504

hi

1. Avoid APPENDING clause in the SELECT statement. use temporary internal table.

2. Avoid INTO CORRESPONDING

3. Avoid Join on VBAK and VBKD.

4. Avoid select single inside loop, if full keys are not passed in the select.

5.Avoid joins in the Loop-Endloop.

there are so many SELECTs in Loop-Endloop, and even joins. try to move them outside loop.

regards,

madhu

Read only

Former Member
0 Likes
504

HI,

And even..

1 Try to use field symbols/workarea when looping the internal tables..

field symbols act like pointers.. they will directly point to the value in the table..

2. Avoid select statement inside the loop..

select the necessary data from DBtable into Internal tables and the make the manupulations using internal tables..

reward if helpful,

regards,

nazeer

Read only

alison_lloyd
Active Participant
0 Likes
505

As mentioned by others avoid into corresponding fields.

If you have to join vbak/vbkd then at least try to use

Sales Organization

Distribution Channel

Division

FOr customer name do n ot select for each document, select for all entries in lt_hdr_aux before the loop where lt_hdr_aux[] = lt_hdr[] with adjacent duplicates by customer deleted and then in the loop use read instead.

Don't use inner joins on vbap to other tables instead select vbap information for all documents selected and then read.

etc etc

Use st05 to review which table accesses are giving you a problem and concentrate first on those taking into account

- not to retrieve data multiple times e.g. is customer is cust1 for deoc1 and for doc 10 then only retrieve once as detailed above.

- optimise the actual select statement - no into corresponding, using keys etc