Saturday, August 31, 2019

Parallel Cursor Technique

In the following example I have shown the parallel cursor technique and how it is very effective for performance when system is facing huge data (big data) in their database.

TYPESBEGIN OF ts_out,
         matnr TYPE mard-matnr,
         werks TYPE mard-werks,
         lgort TYPE mard-lgort,
         mtart TYPE mara-mtart,
       END OF ts_out.
DATAwa_out TYPE ts_out,
      it_out TYPE TABLE OF ts_out.

PARAMETERS p_mtart TYPE mara-mtart.

SELECT matnrmtart
  FROM mara INTO TABLE @DATA(lt_mara)
  WHERE mtart @p_mtart.

IF sy-subrc 0.
  SELECT matnrwerkslgort
    FROM mard INTO TABLE @DATA(lt_mard)
    FOR ALL ENTRIES IN @lt_mara
    WHERE matnr @lt_mara-matnr.
ENDIF.

SORTlt_mara BY matnr,
      lt_mard BY matnr.

GET RUN TIME FIELD DATA(r1).
LOOP AT lt_mara ASSIGNING FIELD-SYMBOL(<ls_mara>).
  LOOP AT lt_mard ASSIGNING FIELD-SYMBOL(<ls_mard>)
    WHERE matnr <ls_mara>-matnr.
    wa_out-matnr <ls_mard>-matnr.
    wa_out-werks <ls_mard>-werks.
    wa_out-lgort <ls_mard>-lgort.
    wa_out-mtart <ls_mara>-mtart.
    APPEND wa_out TO it_out.
    CLEAR  wa_out.
  ENDLOOP.
ENDLOOP.
GET RUN TIME FIELD DATA(r2).

r2 r2 r1.
CLEAR r1.

GET RUN TIME FIELD r1.
LOOP AT lt_mara ASSIGNING <ls_mara>.
  READ TABLE lt_mard ASSIGNING <ls_mard>
  WITH KEY matnr <ls_mara>-matnr
  BINARY SEARCH.
  IF sy-subrc 0.
    DATA(lv_tabixsy-tabix.

    "--No Where Clause
    "--Use the keyword FROM: it reflects faster performance
    LOOP AT lt_mard ASSIGNING <ls_mard> FROM lv_tabix.
      IF <ls_mard>-matnr NE <ls_mara>-matnr.
        EXIT.
      ENDIF.

      wa_out-matnr <ls_mard>-matnr.
      wa_out-werks <ls_mard>-werks.
      wa_out-lgort <ls_mard>-lgort.
      wa_out-mtart <ls_mara>-mtart.
      APPEND wa_out TO it_out.
      CLEAR  wa_out.
    ENDLOOP.
  ENDIF.
ENDLOOP.
GET RUN TIME FIELD DATA(r3).

r3 r3 r1.
WRITE'Where Clause Time:'r2,
       / 'Parallel Cursor Time:'r3.


Here we have executed the program 3 times and you can see that each and every time the cursor technique works much faster than normal where clause.

No comments: