SAP 多层LOOP嵌套优化

2023-10-13 16:49:50 浏览数 (2)

量越大,时间差距越明显!!!!!!!

代码语言:javascript复制
DATA:lv_time_s TYPE timestampl,
     lv_time_e TYPE timestampl,
     lv_text   TYPE timestampl.
DATA:lv_tabix  TYPE i.

SELECT * FROM mara INTO TABLE @DATA(it_mara) UP TO 20000 ROWS.              "量越大 时间差越明显

CHECK it_mara IS NOT INITIAL.

SELECT * FROM marc INTO TABLE @DATA(it_marc) FOR ALL ENTRIES IN @it_mara WHERE matnr = @it_mara-matnr.

GET TIME STAMP FIELD lv_time_s.

DATA ls_data LIKE marc.
DATA lt_data LIKE TABLE OF marc.
LOOP AT it_mara INTO DATA(is_mara).
  LOOP AT it_marc INTO DATA(is_marc) WHERE matnr = is_mara-matnr.
    MOVE-CORRESPONDING is_marc TO ls_data.
    APPEND ls_data TO lt_data.
    CLEAR ls_data.
  ENDLOOP.
ENDLOOP.
GET TIME STAMP FIELD lv_time_e.

lv_text = lv_time_e - lv_time_s.

DESCRIBE TABLE lt_data LINES DATA(lv_num_l).
WRITE: /(14) '普通循环时间:', lv_text, '条目数量:', lv_num_l.

CLEAR:lv_text, lv_time_e, lv_time_s,lt_data.

GET TIME STAMP FIELD lv_time_s.

SORT:it_mara BY matnr,
     it_marc BY matnr.
LOOP AT it_mara INTO is_mara.
  READ TABLE it_marc TRANSPORTING NO FIELDS WITH KEY matnr = is_mara-matnr BINARY SEARCH.
  IF sy-subrc = 0.
    lv_tabix = sy-tabix.

    LOOP AT it_marc FROM lv_tabix INTO is_marc.
      IF is_mara-matnr <> is_marc-matnr.
        EXIT.
      ENDIF.

      MOVE-CORRESPONDING is_marc TO ls_data.
      APPEND ls_data TO lt_data.
      CLEAR ls_data.
    ENDLOOP.

  ENDIF.

ENDLOOP.
GET TIME STAMP FIELD lv_time_e.

DESCRIBE TABLE lt_data LINES DATA(lv_num_2).
lv_text = lv_time_e - lv_time_s.

WRITE: /(14) '优化循环时间:', lv_text, '条目数量:', lv_num_2.

0 人点赞