最近看主管优化了一个HINT相关的查询

借此机会学习下HINT

参考Notes:

Note 129385 - Database hints in Open SQL

http://www.stechno.net/sap-notes.html?view=sapnote&id=129385

Note 150037 - Database hints in Open SQL for DB6 (DB2 for LUW)

http://www.stechno.net/sap-notes.html?view=sapnote&id=150037

-----------------------------------------------------------------------

概述:

有时查询数据时 

查询语句足够复杂

或者使用的二级索引过多

导致的查询方案并不是最好的

所以要手动调整

Hint主要用来优化数据库的查询计划 提高效率

(直接使用native sql的hint应该也是可以的

但是我们这群屌丝开发人员显然对底层数据库的hint使用没把握

会搞这个何苦来做abap >_<

估计也就那几个dba能搞)

 

使用场景:

1.只能作用在透明表和视图上

缓存表  池表 簇表都是不可以的

2.慎用hint 其他方案不行才使用

并且要保证hint的正确性

3.只有在执行是才能验证是否正确

abap syntax是不检查的

错误的hint导致runtime error

无效的hint可能直接被忽略(看具体底层的数据库)

4.不同数据库使用不同的hint语法

 

附语法的说明:

前一段大致将%_hint的使用规则

后一段讲述如何定位一个sql中的某一个table

Open SQL notation

In the clause %_HINTS, hints are entered as the last clause of a SELECT, DELETE, UPDATE Open SQL statement or subquery. Hints cannot be entered as SELECT, DELETE or UPDATE in work area statements or other statements:
   SELECT [..] FROM [..]
    WHERE [..] GROUP BY [..] HAVING [..]
    ORDER BY [..]
   %_HINTS <selector> '<text>' <selector> '<text>' [..] .

  • The %_HINTS clause consists of a list of pairs that consist of a database selector and the actual hint text.
  • Selectors are keywords and are therefore not included in '..':      ADABAS, AS400, DB2, DB6, INFORMIX, MSSQLNT, ORACLE
  • In an actual system, only hint texts with a suitable selector are considered. A hint text is ignored, if it is empty after the evaluation of the substitutions described below or if it only contains blanks.
  • A hint text is either a '...' literal character or a character area. If changeable character fields or (FORM-) parameters are entered, the entire statement will become dynamic. In this case a statement cache is more difficult to use.
  • You can enter several hint texts per database. The database-specific note mentioned above describes if you can process several hint texts and if so, how you do so.

Text substitutions

The database interface evaluates hint texts before forwarding to a database and carries out some substitutions in these texts.These substitutions are a pure text operation without consideration of the surrounding text. In each case two '&' symbols are used for the compound. Invalid or unknown substitutions result in a runtime error. - && --> &
           Technical addition: So that you can enter the character '& ' in a hint text

&TABLE [[<block>,] <table>]& --> <table name>
           is replaced with the name which uses the database interface for the entered table opposite the database. Application developers generally do not know this, since the R/3 database interface generates synthetic table aliases of its own accord.

           <Table> is the number of a table in the FROM clause, counted from 1, if the FROM clause is read from 'left to right.'The default is '1', that is the first table of the FROM clause.

           <Block> specifies which FROM clause is meant: The default '0' specifies the FROM clause of the SELECT..FROM..WHERE block where the hint is also entered. In a subquery '-1', '-2' ... describe the FROM clause of the next outer block along the nesting of blocks/subqueries.

           Due to the defaults mentioned above, &TABLE& is synonymous with &TABLE 1& and &TABLE 0,1&.

           &table [[<block>,] <table>]& --> < table name>

           The same as &TABLE& where upper case is replaced with lower case in the substitution result.

 

 

DB2中的两种hint:

1.optimization guidelines

强制使用我们指定的执行计划

SELECT VERSION FROM SVERS
%_HINTS DB6 '<IXSCAN TABLE=''SVERS'' INDEX=''"SVERS~0"'' />' .

              forces an index access to the table SVERS using the index SVERS~0.

2.indirect hint

open sql可能是动态的

直接指定有困难 

  1. For dynamic OPEN SQL statements that are converted into different DB2 SQL statements for each execution (for, example, statements with dynamic WHERE clauses, statements with dynamic table names, statements with IN predicates to ABAP range tables), you may not be able to directly influence the DB2 Optimizer using an "Optimization Guideline". In these exceptional cases, you can improve the access plan by using a statement for the SAP database interface (indirect hint).

-------------------------------------------------------------------------------------------

附实际的问题:

(主管优化的代码  优化了很多地方

我估计问题不是出在这里的  但是正好学习下Hint)

在查询采购订单历史的执行计划中

没有使用默认的索引

原因是因为有两个二级索引在

恰好有一个查询条件在一个二级索引中

而Optimizer选择了使用二级索引

 

现在通过hint指定使用默认索引

使用的是Optimization Guidelines直接指定

估计0就是指定使用默认的索引吧

附代码:

SELECT ebeln ebelp wesbs xblnr shkzg bwart lfpos lfbnr lfgja belnr buzei gjahr
FROM ekbe
INTO CORRESPONDING FIELDS OF TABLE i_ekbe
FOR ALL ENTRIES IN i_mseg
WHERE ebeln = i_mseg-ebeln
AND ebelp = i_mseg-ebelp
AND xblnr = i_mseg-xblnr
and vgabe = '1'
AND bwart IN ('103','104','105','106','124','125')
%_HINTS db6 '<IXSCAN TABLE=''EKBE'' SAP_INDEX=''0''/>'.

最新文章

  1. MBProgressHUD 显示后,为何不能点击屏幕其他地方
  2. Python画图笔记
  3. 转 猫都能学会的Unity3D Shader入门指南(二)
  4. php strcmp引起的问题
  5. Binary Tree Level Order Traversal
  6. 深入理解object C中复制对象的用法(一)
  7. Common Words
  8. HTML 表格入门
  9. Android 根据规划 Touch 分配和消费机制的事件
  10. ecb, 找不到button
  11. Material Design之NavigationView和DrawerLayout实现侧滑菜单栏
  12. Android项目实战(四十一):游戏和视频类型应用 状态栏沉浸式效果
  13. Nginx code 常用状态码学习小结
  14. C# CefSharp 可监听请求等
  15. 这些 .Net and Core 相关的开源项目,你都知道吗?(持续更新中...)
  16. deepin安装Mariadb后,登录时出现ERROR 1045 (28000): Access denied for user &#39;root&#39;@&#39;localhost&#39;
  17. LeetCode——N-Queens
  18. log4j配置输出到多个日志文件(转)
  19. Python学习 :面向对象 -- 三大特性
  20. 根据Unicode码生成汉字

热门文章

  1. 在ascx中调用另一个ascx的写法
  2. 基于jQuery的宽屏可左右切换的焦点图插件
  3. 21 Free SEO Tools For Bloggers--reference
  4. 关于Android(Java)创建匿名线程
  5. docker-hub 账户
  6. Handler发送消息
  7. jQery简单Tab选项卡效果
  8. [经典算法] 蒙地卡罗法求 PI
  9. PHP trim()函数的一些用法
  10. 破解软件系列-PE文件深入浅出之Section Table节表