--=================
-- Oracle Hint
--=================
讲到Hint就不得不提到执行计划,执行计划是目标sql语句执行及顺序,对于优化至关重要,首先得看懂执行计划
执行计划执行顺序:
先从最开头资质往右看,知道看到最右边的并列的地方;对于不并列的,靠右的先执行;如果并列的由上自下的执行。
一般我们都使用xplan包来查看执行计划,提前需要执行sql脚本。(已下载到网盘)
SQL> @'E:\app\xplan.sql';需要使用sys用户来执行
select sql_id,child_number,sql_text from v$sql where sql_text like '%empno%'
SQL> select * from table(xplan.display_cursor('0ws6d0ft5d0vp',0,'advanced'));
格式及注意事项:
{SELECT |INSERT |UPDATE |DELETE |MERGE } /*+ <具体内容> */
星号(*)和加号(+)之间不能有空格;
Hint中的具体内容可以使单个hint,也可以是多个hint(空格来分隔)
如果有别名,那么hint中就必须使用别名
分类:
Hints |
解释 |
示例 |
|
1 |
/*+ index(t1 indx)*/ |
使用指定索引 |
|
2 |
/*+ no_index(t1 indx)* |
不使用索引 |
|
/*+ index_desc(T2 id_idx) */ |
索引降序操作 |
||
/*+ index_join(t1 id_idx1 id_idx2)*/ |
索引连接 |
查询列都在表索引中 |
|
/*+ and_equal(t1 id_idx1 id_idx2)*/ |
对多个索引合并操作 |
谓词列都有单列索引 且等值查询 |
|
/*+ all_rows*/ |
全表扫描 |
||
/*+ rowid(T2)*/ |
对目标表采用rowid扫描 |
只有谓词使用了rowid 才生效 |
|
/*+ index_combine(T2 id_idx1 id_idx2) */ |
对2个或多个索引执行 位图布尔运算 |
||
/*+ rule */ |
启用RBO优化器 |
||
/*+ all_rows*/ |
启用CBO优化器,预评估权标 |
||
/*+ first_rows(n) */ |
|||
/*+ rule full(T2) */ |
|||
/*+ cardinality(dept 100) */ |
扫描dept 100行数据 |
||
/*+ index_ffs(T2 id_idx) */ |
索引快速全表扫描 |
||
/*+ ordered */ |
按照谓词出现的条件从 左到右依次连接 |
||
/*+ leading(T1 T2) */ |
指定T1作为后面表连接的 驱动表 |
||
/*+ parallel(T2 4) */ |
使用并行 |
||
/*+ use_hash (T2) */ |
使用hash表连接 |
hash只用于等值条件 |
|
3 |
/*+ no_merge(view_1)*/ |
视图合并 |
|
/*+ use_nl(t1 t2)*/ |
嵌套循环连接 |
||
/*+ use_merge(t1 t2) */ |
合并连接 |
||
/*+ no_use_merge(t1 t2) */ |
|||
/*+ merge_aj */ |
排序合并反连接 merge join ant1 |
||
/*+nl_aj */ |
嵌套循环反连接 nested loops ant1 |
||
/*+ hash_aj */ |
哈希反连接 |
||
/*+merge_sj */ |
排序合并半连接 merge join semi |
||
/*+nl_sj*/ |
嵌套循环半连接 nested loop semi |
||
/*+hash_sj*/ |
哈希半连接 |
||
/*+and_equal(sales sale_idx prod_idx)*/ |
索引合并 and-equal and-equal 又称为 index merge, 指如果出现多个条件, 每个条件都是用单列索引, oracle对依次过滤, 然后合并rowid,取出相同 的结果集 |
||
/*+use_concat*/ |
in-list扩展 concatenation |
||
/*+ gather_plan_statistics */ |
额外收集对象基数 执行时间及cost |
使用 select * from table(dbms_xplan.display_cursor(null,null,'allstats last'))来查看,这个很实用。 |
|
/*+ cache */ |
目标对象数据块缓存到 LRU热端 |
||
/*+ append */ |
直接插入方式insert数据 |
||
/*+ no_expand */ |
,
/*+ gather_plan_statistics */在优化中非常有用,
SQL> select /* gather_plan_statistics */ * from t2 where n1=1;
SQL> select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------------------------------------------
SQL_ID gft7m5y02t0r9, child number 0
-------------------------------------
select /* gather_plan_statistics */ * from t2 where n1=1
Plan hash value: 1513984157
-------------------------------------------
| Id | Operation | Name | E-Rows |
-------------------------------------------
| 0 | SELECT STATEMENT | | |
|* 1 | TABLE ACCESS FULL| T2 | 492 |
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("N1"=1)
Note
-----
- Warning: basic plan statistics not available. These are only collected when:
* hint 'gather_plan_statistics' is used for the statement or
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------------------------------------------
* parameter 'statistics_level' is set to 'ALL', at session or system level
24 rows selected
来源:oschina
链接:https://my.oschina.net/u/3862440/blog/2873339