• 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.

    未完待续~~~

    每天学习一个知识点,累积就是财富。
  • 相关阅读:
    修改CentOS的yum源为国内yum镜像源
    CentOS7利用yum安装node.js
    Ansible系列(一):安装
    动态链接库引起的yum故障
    《C++ Templates: The Complete Guide》读书笔记
    Linux下编译clang、libcxx及其相关库——C++11环境搭建
    shell小工具:findstr 和 findfile
    关于newexpression、new operator、operator delete的总结
    Makefile编写示例:构建一个库
    无计划就不行动!
  • 原文地址:https://www.cnblogs.com/buduzhiren/p/10904224.html
Copyright © 2020-2023  润新知