Performance Tuning using Parallel cursor


When you loop around an internal table using a where clause the loop command still processes every line in the table to check if they meet the correct criteria. This is probably not a problem in most cases as its the processing within the loop that takes the time rather than the actual loop. Having said that there maybe a small number of cases where you have millions of records and you know that you only want a few of them which are all together somewhere in the middle of this table. This method allows you to go straight to the first record and stop processing immediately when you get to the last one.



**************************************************************
*              Performance Tuning using parallel cursor
*
**************************************************************


**************************************************************
* START-OF-SELECTION
  SELECT *
  INTO TABLE I_KEPH FROM KEPH
  WHERE KADKY <= SY-DATUM
    AND TVERS = '01'
    AND KALKA IN ('01','Z1','Z2')
    AND BWVAR IN ('Z01','Z02','Z03','Z04','Z07')
    AND KKZST = ' '
    AND KKZMA = ' '
    AND KKZMM = ' '
    AND KEART = 'H'
    AND PATNR = 0.

* Table must be sorted to ensure all required records are together
  SORT I_KEPH BY KALNR KALKA BWVAR KADKY.

* Perform actual processing
  Perform get_cost_values.


*----------------------------------------------------------------------*
FORM GET_COST_VALUES.
* Determine start position and then process all records for given key
* from that starting point
* i_keph is sorted on kalnr kalka bwvar kadky.
  READ TABLE I_KEPH WITH KEY KALNR = W_KEKO-KALNR
                             KALKA = W_KEKO-KALKA
                             BWVAR = W_KEKO-BWVAR
                             KADKY = W_KEKO-KADKY BINARY SEARCH.
  IF SY-SUBRC = 0.
* Loop at itab from first record found (sy-tabix) until record
* no-longer matches your criteria.
    LOOP AT I_KEPH FROM SY-TABIX.
      IF  I_KEPH-KALNR = W_KEKO-KALNR AND I_KEPH-KALKA = W_KEKO-KALKA
      AND I_KEPH-BWVAR = W_KEKO-BWVAR AND I_KEPH-KADKY = W_KEKO-KADKY.
*       Key match
        D_MAT_COST = D_MAT_COST + I_KEPH-KST001.
        D_LAB_COST = D_LAB_COST + I_KEPH-KST004.
        D_OVER_HEAD = D_OVER_HEAD + I_KEPH-KST010.
        D_EXT_PURCH = D_EXT_PURCH + I_KEPH-KST014.
        D_MISC_COST = D_MISC_COST + I_KEPH-KST002 + I_KEPH-KST003
                    + I_KEPH-KST005 + I_KEPH-KST006 + I_KEPH-KST007
                    + I_KEPH-KST008 + I_KEPH-KST009 + I_KEPH-KST011
                    + I_KEPH-KST012 + I_KEPH-KST013 + I_KEPH-KST015
                    + I_KEPH-KST016 + I_KEPH-KST017 + I_KEPH-KST018
                    + I_KEPH-KST019 + I_KEPH-KST020 + I_KEPH-KST021
                    + I_KEPH-KST022 + I_KEPH-KST023 + I_KEPH-KST024
                    + I_KEPH-KST025 + I_KEPH-KST026 + I_KEPH-KST027
                    + I_KEPH-KST028 + I_KEPH-KST029 + I_KEPH-KST030
                    + I_KEPH-KST031 + I_KEPH-KST032 + I_KEPH-KST033
                    + I_KEPH-KST034 + I_KEPH-KST035 + I_KEPH-KST036
                    + I_KEPH-KST037 + I_KEPH-KST038 + I_KEPH-KST039
                    + I_KEPH-KST040.
      ELSE.
*       Key greater - can't be less
        EXIT.                                               " Exit loop
      ENDIF.
    ENDLOOP.
  ENDIF.

  D_MAT_COST  = D_MAT_COST  / W_KEKO-LOSGR.
  D_LAB_COST  = D_LAB_COST  / W_KEKO-LOSGR.
  D_OVER_HEAD = D_OVER_HEAD / W_KEKO-LOSGR.
  D_EXT_PURCH = D_EXT_PURCH / W_KEKO-LOSGR.
  D_MISC_COST = D_MISC_COST / W_KEKO-LOSGR.
ENDFORM.                               " GET_COST_VALUES