SAP内表查询速度优化实例-OPEN SQL

一、FOR ALL ENTRIES IN 案例

今天碰到工单报工统计分析表查询速度特别慢

经查看源代码:

  SELECT afpo~dwerk afko~aufnr afpo~matnr AS plnbez afpo~charg afpo~psmng afko~fevor afko~aufpl
      FROM afko INNER JOIN afpo
        ON afko~aufnr = afpo~aufnr
      INTO CORRESPONDING FIELDS OF TABLE it_afpo
     WHERE afpo~dwerk = p_dwerk
       AND afpo~aufnr IN p_aufnr
       AND afpo~matnr IN p_matnr
       AND afko~fevor IN p_fevor
       AND afpo~charg IN p_charg.

  IF f_wr = 'X'.
    SELECT *
      FROM afru
      INTO CORRESPONDING FIELDS OF TABLE it_afru
      FOR ALL ENTRIES IN it_afpo
     WHERE werks = it_afpo-dwerk
       AND aufnr = it_afpo-aufnr
       AND ersda IN p_ersda
       AND prdut IN p_prdut
       AND stokz <> 'X'
       AND stzhl = 0 .
  ELSE.
    SELECT *
      FROM afru
      INTO CORRESPONDING FIELDS OF TABLE it_afru
      FOR ALL ENTRIES IN it_afpo
     WHERE werks = it_afpo-dwerk
      AND aufnr = it_afpo-aufnr
      AND ersda IN p_ersda
      AND prdut IN p_prdut.
  ENDIF.

FOR ALL ENTRIES IN 使用前需判断后面的内表是否为空,为空则查询出的数据特别多,影响查询速度,如内表数据量很大,速度也很慢。

这样就是 FOR ALL ENTRIES IN 后面内表的数据量很大。一年的工单有一万多条。

所以这里,不使用FOR ALL ENTRIES IN语句。

解决方法:

1、where语句in该内表,定义range类型内表:it_aufnr 存储 it_afpo-aufnr 数据。

  RANGES: it_aufnr FOR afko-aufnr.
  LOOP AT it_afpo.
    it_aufnr-low = it_afpo-aufnr.
    it_aufnr-sign = 'I'.
    it_aufnr-option = 'EQ'.
    APPEND it_aufnr.
  ENDLOOP.

也可以查询it_afpo的时候就直接赋值给it_aufnr

例:(查询出的数据要用low,需要定义sign和option)

  SELECT aufnr INTO it_aufnr-low FROM afko WHERE dwerk = p_dwerk AND gltri IN p_ersda.
    it_matnr-sign = 'I'.
    it_matnr-option = 'EQ'.
    APPEND it_matnr.
  ENDSELECT.

IF NOT it_aufnr[] IS INITIAL.

  SELECT * INTO CORRESPONDING FIELDS OF TABLE it_afru
     FROM afko WHERE dwerk = p_dwerk AND ersda IN p_ersda AND aufnr IN it_aufnr 
                     AND prdut IN p_prdut AND stokz <> 'X' AND stzhl = 0 .

也可以这样写:

append it_aufnr to it_aufnr[].
WHERE aufnr IN it_aufnr[].

 

2、loop 循环 it_afru 内表,删除 it_afpo 中不存在的 aufnr 行。

    SELECT * FROM afru
     INTO CORRESPONDING FIELDS OF TABLE it_afru
*     FOR ALL ENTRIES IN it_afpo
    WHERE werks = p_dwerk
*      AND aufnr = it_afpo-aufnr
      AND ersda IN p_ersda
      AND prdut IN p_prdut
      AND stokz <> 'X'
      AND stzhl = 0 .

  IF NOT it_afru[] IS INITIAL.

    LOOP AT it_afru .
      READ TABLE it_afpo WITH KEY aufnr = it_afru-aufnr.
      IF sy-subrc <> 0.
        DELETE it_afru[].
        CONTINUE..
      ENDIF.
    ENDLOOP.

 

 

未完待续~~~

 

posted @ 2019-05-22 10:12  不渡之人  阅读(1266)  评论(0编辑  收藏  举报