最近看主管优化了一个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''/>'.

使用Hint来优化执行计划的更多相关文章

  1. SQL Server 优化-执行计划

    对于SQL Server的优化来说,优化查询可能是很常见的事情.由于数据库的优化,本身也是一个涉及面比较的广的话题, 因此本文只谈优化查询时如何看懂SQL Server查询计划.毕竟我对SQL Ser ...

  2. Mysql SQL优化&执行计划

    SQL优化准则 禁用select * 使用select count(*) 统计行数 尽量少运算 尽量避免全表扫描,如果可以,在过滤列建立索引 尽量避免在where子句对字段进行null判断 尽量避免在 ...

  3. MySQL 执行计划详解

    我们经常使用 MySQL 的执行计划来查看 SQL 语句的执行效率,接下来分析执行计划的各个显示内容. EXPLAIN SELECT * FROM users WHERE id IN (SELECT ...

  4. 分析oracle的执行计划(explain plan)并对对sql进行优化实践

    基于oracle的应用系统很多性能问题,是由应用系统sql性能低劣引起的,所以,sql的性能优化很重要,分析与优化sql的性能我们一般通过查看该sql的执行计划,本文就如何看懂执行计划,以及如何通过分 ...

  5. 根据执行计划优化sql语句

    优化前:表连接使用merge SQL> alter session set statistics_level=all; Session altered. SQL> select e.sal ...

  6. 基于Oracle的SQL优化(崔华著)-整理笔记-第2章“Oracle里的执行计划”

    详细介绍了Oracle数据里与执行计划有关的各个方面的内容,包括执行计划的含义,加何查看执行计划,如何得到目标SQL真实的执行计划,如何查看执行计划的执行顺序,Oracle数据库里各种常见的执行计划的 ...

  7. 第九课——MySQL优化之索引和执行计划

    一.创建索引需要关注什么? 1.关注基数列唯一键的数量: 比如性别,该列只有男女之分,所以性别列基数是2: 2.关注选择性列唯一键与行数的比值,这个比值范围在0~1之前,值越小越好: 其实,选择性列唯 ...

  8. mysql之优化器、执行计划、简单优化

    mysql之优化器.执行计划.简单优化 2018-12-12 15:11 烟雨楼人 阅读(794) 评论(0) 编辑 收藏 引用连接: https://blog.csdn.net/DrDanger/a ...

  9. [z]Oracle性能优化-读懂执行计划

    http://blog.csdn.net/lifetragedy/article/details/51320192 Oracle的执行计划   得到执行计划的方式       Autotrace例子 ...

随机推荐

  1. 性能监视器(SSAS)

    使用性能监视器,您可以通过性能计数器监视 Microsoft SQL Server Analysis Services (SSAS) 实例的性能. 性能监视器是用于跟踪资源使用情况的 Microsof ...

  2. 项目源码--Android答题类游戏源码

    下载源码 技术要点: 1. 精致的答题UI 2. Android的Http通信技术 3. Android数据库QLITE与其他数据存储技术 4. Android在线音乐背景技术 5. Android答 ...

  3. eclipse的scala环境搭建

    两种方法使eclipse安装scala环境(eclipse luna) 1.下载eclipse for scala IDE http://scala-ide.org/download/sdk.html ...

  4. VSS 之 未知的用户名或密码错误

    原因:导致这种现象的原因并不是真的用户名和密码错误,而是验证失败.验证失败也自然就会提示成用户名或密码错误了,只需要做以下更改就能正常验证用户名和密码了.解决方案:运行 组策略编辑器 gpedit.m ...

  5. Mac 下抓包工具 Charles 修改特定请求

    Charles 是 Mac 下常用的抓包工具,它通过将自己设置成系统的代理服务器,从而完成了抓包的工作. 同类的工具还有 fiddler 和 Wireshark,其中 fiddler 是微软公司使用 ...

  6. 【Shell脚本学习11】Shell注释

    以“#”开头的行就是注释,会被解释器忽略. sh里没有多行注释,只能每一行加一个#号.只能像这样: #-------------------------------------------- # 这是 ...

  7. mina 粘包、多包和少包的解决方法

    转载自:http://freemart.iteye.com/blog/836654 使用过 mina 的同学应该都遇到到过,在解码时少包.多包的问题,查阅了很多资料还是迷迷糊糊的,经过不懈努力,终于解 ...

  8. ASP.NET页面与IIS底层交互和工作原理详解(第一回)

    引言 我查阅过不少Asp.Net的书籍,发现大多数作者都是站在一个比较高的层次上讲解Asp.Net.他们耐心.细致地告诉你如何一步步拖放控件.设置控件属性.编写CodeBehind代码,以实现某个特定 ...

  9. asp.net在word页眉插入条形码

    条形码控件需要引用MSBCODE9.OCX组件.该组件的默认位置是C:\Program Files\Microsoft Office\Office12\2052\MSBCODE9.OCX using  ...

  10. 虚拟机的MAC地址分配与修改

    虚拟世界的MAC地址 先看一下真实世界的MAC地址是如何分配,如何保证没有重复的.每块网卡都有一个MAC地址,MAC地址是一个6字节.也即48bit的数据.前3字节称为OUI ,是由IEEE组织注册给 ...