【力荐】Select查询语句中LIKE关键词的优化方法分析

今天接到一个优化需求,跑个程序要12+个小时,周期是每天一次,所以时效性极差,不能响应快速的实际业务需求,下面我们看一段LIKE的优化方法。

  1. SELECT     bukrs
  2.            werks
  3.            lgort
  4.            matnr
  5.            lifnr
  6.            sobkz
  7.            servg
  8.            matkl
  9.            prdha
  10.            labst
  11.            ean11
  12.       APPENDING CORRESPONDING FIELDS OF TABLE i_s770
  13.       FROM s770
  14.      WHERE matnr IN s_matnr
  15.        AND servg = ''
  16.        AND sobkz = ''
  17.        AND bukrs IN r_bukrs
  18.        AND werks IN r_werks
  19.        AND ( lgort = '0001' OR lgort = '0002' OR lgort = '0003' OR lgort = '0005' OR
  20.              lgort LIKE '7%' OR lgort LIKE '8%' OR lgort LIKE '9%' OR lgort LIKE 'A%' OR
  21.              lgort LIKE 'B%' OR lgort LIKE 'C%' OR lgort LIKE 'D%' OR lgort LIKE 'E%' OR
  22.              lgort LIKE 'F%' OR lgort LIKE 'G%' OR lgort LIKE 'H%' OR lgort LIKE 'I%' )
  23.        %_HINTS ORACLE 'FULL(S770)'.

其实这条语句以前已经被三星的顾问优化过了,现在看看还有可优化的潜力;%_HINTS ORACLE 'FULL(S770)'是扫描全表的意思,一条条查找,不写走索引反而会很慢。

下面是测试案例:
1、se30测试这两种语句的性能

  1. DATA: record_count TYPE INT4.
  2. RANGES: r_lgort FOR s770-bukrs.

  3. r_lgort-sign = 'I'.
  4. r_lgort-option = 'BT'.
  5. r_lgort-low = '7000'.
  6. r_lgort-high = '9ZZZ'.
  7. APPEND r_lgort.

  8. SELECT count(*) INTO record_count FROM s770 
  9. WHERE lgort IN r_lgort.

  10. WRITE:record_count.
  1. DATA: record_count TYPE INT4.

  2. SELECT count(*) INTO record_count FROM s770 
  3. WHERE lgort LIKE '7%' OR 
  4.       lgort LIKE '8%' OR lgort LIKE '9%'.

  5. WRITE:record_count.

2、然后我们se38新建一个程序,看看这两条sql查询的数量是否一致


看到了吧,性能提升明显,快到一半了!但是这还不够快,因为sql中还有模糊查询,我们可以用下面的这种方法:

  1.   SELECT   bukrs
  2.            werks
  3.            lgort
  4.            matnr
  5.            lifnr
  6.            sobkz
  7.            servg
  8.            matkl
  9.            prdha
  10.            labst
  11.            ean11
  12.       APPENDING CORRESPONDING FIELDS OF TABLE i_s770
  13.       FROM s770
  14.      WHERE matnr IN s_matnr
  15.        AND servg = ''
  16.        AND sobkz = ''
  17.        AND bukrs IN r_bukrs
  18.        AND werks IN r_werks.
  19.   LOOP AT i_s770.
  20.     IF i_s770-lgort = '0001' OR i_s770-lgort = '0002' OR i_s770-lgort = '0003' OR i_s770-lgort = '0005'.
  21.       CONTINUE.
  22.     ELSEIF i_s770-lgort+0(1) NE '7' AND i_s770-lgort+0(1) NE '8' AND i_s770-lgort+0(1) NE '9'
  23.        AND i_s770-lgort+0(1) NE 'A' AND i_s770-lgort+0(1) NE 'B' AND i_s770-lgort+0(1) NE 'C'
  24.        AND i_s770-lgort+0(1) NE 'D' AND i_s770-lgort+0(1) NE 'E' AND i_s770-lgort+0(1) NE 'F'
  25.        AND i_s770-lgort+0(1) NE 'G' AND i_s770-lgort+0(1) NE 'H' AND i_s770-lgort+0(1) NE 'I'.
  26.       DELETE i_s770.
  27.     ENDIF.
  28.   ENDLOOP.
这样处理内表比在数据库 里直接处理就快多了。


Tips:

当执行SQL时,如果有符合选择条件的INDEX存在,但是数据库并未按照INDEX来执行,那么我们可以在SQL语句中明确的写上执行的INDEX。


①用过的两个写法:
1、指定使用全表扫描:%_HINTS ORACLE 'FULL(table_name)'
2、指定索引:%_HINTS ORACLE 'INDEX(table_name index_name)'
其他Oracle Hints的写法可以参见这篇文章:Oracle Hint的用法,在SQL语句优化过程中,经常会用到hint。
②Using secondary indexes
Consider the following example:
SELECT * FROM SPFLI
  %_HINTS ORACLE 'INDEX("SPFLI" "SPFLI~001")'
.......
ENDSELECT.
In the above example, 001 is the secondary index of the table SPFLI. It's a well-known fact that the efficient way of retrieving data from the database tables is by using secondary indexes. Many database vendors provide the optimizer hints for the same. From SAP v4.5, optimizer hints can be provided by the %_HINTS parameter. This is dependent on the database systems that support optimizer hints. The point to be noted here is these optimizer hints are not standardized by the SQL standards. Each database vendor is free to provide the optimizer hints.


Now to know which index to use for our table:
1. Go to SE11 and there specify the table name
2. Now from the menu, goto --> indexes
3. select the required index.
Now suppose that the identifier 001 represents a non-unique secondary index comprising of the columns CITYFROM and CITYTO. The index name should be defined as:  ~like SPFLI~001 in the above example.The sequence of fields in the WHERE condition is of no relevance in using this optimizers index. If you specify hints incorrectly, ABAP ignores them but doesn't return a syntax error or runtime error.The code was written in R/3 4.6C.
Consider the following example: 
REPORT Suresh_test.
TABLES: spfli.
DATA : t_spfli LIKE spfli OCCURS 0 WITH HEADER LINE.
SELECT * FROM spfli
 INTO TABLE t_spfli
     %_HINTS ORACLE 'INDEX("SPFLI" "SPFLI~001")'.
 
 LOOP AT t_spfli.
      WRITE :/ t_spfli.
ENDLOOP.
 
③ABAP--如何在SELECT语句中指定索引(example)
report z_generic_test_program .
tables: csks. start-of-selection.  
select * up to 10 rows
     from csks                         
      where kokrs <> space
           and kostl <> space                         
      %_hints oracle 'index(csks"csks~J")'.  
          write: / csks.
endselect. 
④Control over FOR ALL ENTRIES Hints Under the heading Database Interface Hints, Note 129385 describes the options you have for influencing the database interface by entering hints. The hints are evaluated in the database interface itself and are not passed on to the database. Starting with kernel Release 4.6B all the above mentioned FOR ALL ENTRIES parameters can be set via such a hint for a single statement. In the example:  
SELECT *
    FROM [..]
    FOR ALL ENTRIES IN [..]
    WHERE [..]  
    %_HINTS ORACLE '&prefer_in_itab_opt 1&&prefer_fix_blocking -1&'.
This way, the boolean parameter 'prefer_in_itab_opt' is explictly set and the boolean parameter 'prefer_fix_blocking' is set to its default value. FOR ALL ENTRIES hints, like hints are generally only used as a a corrective device in emergency situations.

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SAP剑客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值