今天接到一个优化需求,跑个程序要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.

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

  1. MySQL查询语句中的IN 和Exists 对比分析

    背景介绍 最近在写SQL语句时,对选择IN 还是Exists 犹豫不决,于是把两种方法的SQL都写出来对比一下执行效率,发现IN的查询效率比Exists高了很多,于是想当然的认为IN的效率比Exist ...

  2. 查询语句中select from where group by having order by的执行顺序

    查询语句中select from where group by having order by的执行顺序 1.查询中用到的关键词主要包含六个,并且他们的顺序依次为  select--from--whe ...

  3. oracle查询语句中select from where group by having order by的解释与应用

    oracle查询语句中select from where group by having order by的解释与应用 查询中用到的关键词主要包含六个,并且他们的顺序依次为 select--from- ...

  4. 【MySQL】4、Select查询语句

    4.Select查询语句 4.1.select语句 <?php $servername = "localhost"; $username = "username&q ...

  5. python语法中infile语句_浅谈pymysql查询语句中带有in时传递参数的问题

    直接给出例子说明: cs = conn.cursor() img_ids = [1,2,3] sql = "select img_url from img_url_table where i ...

  6. 自定义字符串变量赋值在查询语句中使用

    自定义字符串变量赋值后,如何在查询语句中使用? 比如: DECLARE @isforbid VARCHAR(1000) SET @isforbid = 'XXXXXX' SELECT * FROM t ...

  7. python查询数据库带逗号_浅谈pymysql查询语句中带有in时传递参数的问题

    直接给出例子说明: cs = conn.cursor() img_ids = [1,2,3] sql = "select img_url from img_url_table where i ...

  8. Oracle 10g数据库基础之基本查询语句-中-函数

    Oracle 10g数据库基础之基本查询语句-中-函数 --资料参考:张烈 张建中<数据库管理员培训讲义> 函数: 使用函数的目的是为了操作数据 将输入的变量处理,返回一个结果. 变量可以 ...

  9. mysql五大子句_MySQL的查询语句中可以使用以下哪个子句来表示分组查询

    [多选题]人类行为遗传学工作者倾向于把人的行为遗传分为哪几类 [填空题]MySQL的连接操作包括内连接.( )和交叉连接. [判断题]社会生活类尤其是人文风光类纪录片的解说则多用文学. 散文手法, 既 ...

最新文章

  1. 和ISP合作需要了解什么?—Vecloud微云
  2. ImageView宽度填满屏幕,高度自适应
  3. windows 2003负载均衡故障切换
  4. NLP《语言模型(二)-- 神经网络NNLM语言模型》
  5. 四川长虹招聘机器视觉、图像识别工程师
  6. 30 友盟项目---体会篇
  7. componentDidUpdate vs componentWillReceiveProps
  8. redis入门基础知识(一)
  9. 游戏服务器当中的唯一名设计方法
  10. java爬虫微信公众号信息_微信公众号爬虫项目(reptile)
  11. windows常用端口对应表
  12. java实例 之 商品管理系统
  13. 商用密码产品认证-电子签章系统
  14. 视频直播的购物平台,网站,app
  15. 如何搭建企业自己的邮箱服务器,企业如何搭建属于自己的企业邮箱
  16. [PhotoShop]用ps制作遮罩图层
  17. 差点跑进奥运会赛场的“人类计算机之父”
  18. CSS3实现精美的纸张折角效果 -- 进阶版
  19. 新浪sina gitlab邮件收不到
  20. 剑指Offer题目:从扑克牌中随机抽 5 张牌,判断是不是顺子,即这 5 张牌是不是连续的。 2-10 为数字本身,A 为 1,J 为 11,Q 为 12,K 为 13,而大小王可以看成任意的 数字。

热门文章

  1. 抖音之后,互联网失去创造力
  2. 户外私密Party| 在大峡谷中聊点平时不能聊的产品干货(报名结束)
  3. 20个顶尖产品经理都在用的APP
  4. 有逼格的产品经理的工作台长啥样?
  5. (11)Spring Boot配置ContextPath【从零开始学Spring Boot】
  6. Socket网络编程--聊天程序(3)
  7. Java反射实现几种方式
  8. 完全二叉树的JAVA实现(以及非递归遍历方法)
  9. CCNA第十一章学习笔记OSPF简介
  10. 职业经理十项管理技能必修课