hint概述
基于代价的优化器是很聪明的,在绝大多数情况下它会选择正确的优化器,减轻了dba的负担。但有时它也聪明反被聪明误,选择了很差的执行计划,使某个语句的执行变得奇慢无比。
此时就需要dba进行人为的干预,告诉优化器使用我们指定的存取路径或连接类型生成执行计划,从而使语句高效的运行。例如,如果我们认为对于一个特定的语句,执行全表扫描要比执行索引扫描更有效,则我们就可以指示优化器使用全表扫描。在oracle 中,是通过为语句添加 hints(提示)来实现干预优化器优化的目的。
不建议在代码中使用hint,在代码使用hint使得cbo无法根据实际的数据状态选择正确的执行计划。毕竟 数据是不断变化的, 10g以后的cbo也越来越完善,大多数情况下我们该让oracle自行决定采用什么执行计划。
oracle hints是一种机制,用来告诉优化器按照我们的告诉它的方式生成执行计划。我们可以用oracle hints来实现:
1) 使用的优化器的类型
2) 基于代价的优化器的优化目标,是all_rows还是first_rows。
3) 表的访问路径,是全表扫描,还是索引扫描,还是直接利用rowid。
4) 表之间的连接类型
5) 表之间的连接顺序
6) 语句的并行程度
除了”rule”提示外,一旦使用的别的提示,语句就会自动的改为使用cbo优化器,此时如果你的数据字典中没有统计数据,就会使用缺省的统计数据。所以建议大家如果使用cbo或hints提示,则最好对表和索引进行定期的分析。
如何使用hints:
hints只应用在它们所在sql语句块(statement block,由select、update、delete关键字标识)上,对其它sql语句或语句的其它部分没有影响。如:对于使用union操作的2个sql语句,如果只在一个sql语句上有hints,则该hints不会影响另一个sql语句。
我们可以使用注释(comment)来为一个语句添加hints,一个语句块只能有一个注释,而且注释只能放在select, update, or delete关键字的后面
使用oracle hints的语法:
{delete|insert|select|update} /* hint [text] [hint[text]]... */
or
{delete|insert|select|update} -- hint [text] [hint[text]]...
注解:
1) delete、insert、select和update是标识一个语句块开始的关键字,包含提示的注释只能出现在这些关键字的后面,否则提示无效。
2) “ ”号表示该注释是一个hints,该加号必须立即跟在”/*”的后面,中间不能有空格。
3) hint是下面介绍的具体提示之一,如果包含多个提示,则每个提示之间需要用一个或多个空格隔开。
4) text 是其它说明hint的注释性文本
5)使用表别名。如果在查询中指定了表别名,那么提示必须也使用表别名。例如:select /* index(e,dept_idx) */ * from emp e;
6)不要在提示中使用模式名称:如果在提示中指定了模式的所有者,那么提示将被忽略。例如:
select /* index(scott.emp,dept_idx) */ * from emp
注意:如果你没有正确的指定hints,oracle将忽略该hints,并且不会给出任何错误。
hint被忽略
如果cbo认为使用hint会导致错误的结果时,hint将被忽略,详见下例
sql> select /* index(t t_ind) */ count(*) from t;
execution plan
----------------------------------------------------------
plan hash value: 2966233522
-------------------------------------------------------------------
| id | operation | name | rows | cost (%cpu)| time |
-------------------------------------------------------------------
| 0 | select statement | | 1 | 57 (2)| 00:00:01 |
| 1 | sort aggregate | | 1 | | |
| 2 | table access full| t | 50366 | 57 (2)| 00:00:01 |
-------------------------------------------------------------------
因为我们是对记录求总数,且我们并没有在建立索引时指定不能为空,索引如果cbo选择在索引上进行count时,但索引字段上的值为空时,结果将不准确,故cbo没有选择索引。
sql> select /* index(t t_ind) */ count(id) from t;
execution plan
----------------------------------------------------------
plan hash value: 646498162
--------------------------------------------------------------------------
| id | operation | name | rows | bytes | cost (%cpu)| time |
--------------------------------------------------------------------------
| 0 | select statement | | 1 | 5 | 285 (1)| 00:00:04 |
| 1 | sort aggregate | | 1 | 5 | | |
| 2 | index full scan| t_ind | 50366 | 245k| 285 (1)| 00:00:04 |
--------------------------------------------------------------------------
因为我们只对id进行count,这个动作相当于count索引上的所有id值,这个操作和对表上的id字段进行count是一样的(组函数会忽略null值)
hint的具体用法
和优化器相关的hint
1、/* all_rows */
表明对语句块选择基于开销的优化方法,并获得最佳吞吐量,使资源消耗最小化.
select /* all _rows*/ emp_no,emp_nam,dat_in from bsempms where emp_no='scott';
2、/* first_rows(n) */
表明对语句块选择基于开销的优化方法,并获得最佳响应时间,使资源消耗最小化.
select /* first_rows(20) */ emp_no,emp_nam,dat_in from bsempms where emp_no='scott';
3、/* rule*/
表明对语句块选择基于规则的优化方法.
select /* rule */ emp_no,emp_nam,dat_in from bsempms where emp_no='scott';
和访问路径相关的hint
1、/* full(table)*/
表明对表选择全局扫描的方法.
select /* full(a)*/ emp_no,emp_nam from bsempms a where emp_no='scott';
2、/* index(table index_name) */
表明对表选择索引的扫描方法.
select /* index(bsempms sex_index) */ * from bsempms where sex='m';
5、/* index_asc(table index_name)*/
表明对表选择索引升序的扫描方法.
select /* index_asc(bsempms pk_bsempms) */ * from bsempms where dpt_no='scott';
6、/* index_combine*/
为指定表选择位图访问路经,如果index_combine中没有提供作为参数的索引,将选择出位图索引的布尔组合方式.
select /* index_combine(bsempms sal_bmi hiredate_bmi) */ * from bsempms
where sal<5000000 and hiredate
7、/* index_join(table index_name1 index_name2) */
当谓词中引用的列都有索引的时候,可以通过指定采用索引关联的方式,来访问数据
select /* index_join(t t_ind t_bm) */ id from t where id=100 and object_name='employees'
8、/* index_desc(table index_name)*/
表明对表选择索引降序的扫描方法.
select /* index_desc(bsempms pk_bsempms) */ * from bsempms where dpt_no='scott';
9、/* index_ffs(table index_name) */
对指定的表执行快速全索引扫描,而不是全表扫描的办法.
select /* index_ffs(bsempms in_empnam)*/ * from bsempms where dpt_no='tec305';
10、/* index_ss(t t_ind) */
从9i开始,oracle引入了这种索引访问方式。当在一个联合索引中,某些谓词条件并不在联合索引的第一列时,可以通过index skip scan来访问索引获得数据。当联合索引第一列的唯一值个数很少时,使用这种方式比全表扫描效率高。
sql> create table t as select 1 id,object_name from dba_objects;
table created.
sql> insert into t select 2,object_name from dba_objects;
50366 rows created.
sql> insert into t select 3,object_name from dba_objects;
50366 rows created.
sql> insert into t select 4,object_name from dba_objects;
50366 rows created.
sql> commit;
commit complete.
sql> create index t_ind on t(id,object_name);
index created.
sql> exec dbms_stats.gather_table_stats('hr','t',cascade=>true);
pl/sql procedure successfully completed.
执行全表扫描
sql> select /* full(t) */ * from t where object_name='employees';
6 rows selected.
execution plan
----------------------------------------------------------
plan hash value: 1601196873
--------------------------------------------------------------------------
| id | operation | name | rows | bytes | cost (%cpu)| time |
--------------------------------------------------------------------------
| 0 | select statement | | 5 | 135 | 215 (3)| 00:00:03 |
|* 1 | table access full| t | 5 | 135 | 215 (3)| 00:00:03 |
--------------------------------------------------------------------------
predicate information (identified by operation id):
---------------------------------------------------
1 - filter("object_name"='employees')
statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
942 consistent gets
0 physical reads
0 redo size
538 bytes sent via sql*net to client
385 bytes received via sql*net from client
2 sql*net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
6 rows processed
不采用hint
sql> select * from t where object_name='employees';
6 rows selected.
execution plan
----------------------------------------------------------
plan hash value: 2869677071
--------------------------------------------------------------------------
| id | operation | name | rows | bytes | cost (%cpu)| time |
--------------------------------------------------------------------------
| 0 | select statement | | 5 | 135 | 5 (0)| 00:00:01 |
|* 1 | index skip scan | t_ind | 5 | 135 | 5 (0)| 00:00:01 |
--------------------------------------------------------------------------
predicate information (identified by operation id):
---------------------------------------------------
1 - access("object_name"='employees')
filter("object_name"='employees')
statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
17 consistent gets
1 physical reads
0 redo size
538 bytes sent via sql*net to client
385 bytes received via sql*net from client
2 sql*net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
6 rows processed
当全表扫描扫描了942个块,联合索引只扫描了17个数据块。可以看到联合索引的第一个字段的值重复率很高时,即使谓词中没有联合索引的第一个字段,依然会使用index_ss方式,效率远远高于全表扫描效率。但当 第一个字段的值重复率很低时,使用 index_ss的效率要低于 全表扫描,读者可以自行实验
和表的关联相关的hint
/* leading(table_1,table_2) */
在多表关联查询中,指定哪个表作为驱动表,即告诉优化器首先要访问哪个表上的数据。
select /* leading(t,t1) */ t.* from t,t1 where t.id=t1.id;
/* order */
让oracle根据from后面表的顺序来选择驱动表,oracle建议使用leading,他更为灵活
select /* order */ t.* from t,t1 where t.id=t1.id;
/* use_nl(table_1,table_2) */
在多表关联查询中,指定使用nest loops方式进行多表关联。
select /* use_nl(t,t1) */ t.* from t,t1 where t.id=t1.id;
/* use_hash(table_1,table_2) */
在多表关联查询中,指定使用hash join方式进行多表关联。
select /* use_hash(t,t1) */ t.* from t,t1 where t.id=t1.id;
在多表关联查询中,指定使用hash join方式进行多表关联,并指定表t为驱动表。
select /* use_hash(t,t1) leading(t,t1) */ t.* from t,t1 where t.id=t1.id;
/* use_merge(table_1,table_2) */
在多表关联查询中,指定使用merge join方式进行多表关联。
select /* use_merge(t,t1) */ t.* from t,t1 where t.id=t1.id;
/* no_use_nl(table_1,table_2) */
在多表关联查询中,指定不使用nest loops方式进行多表关联。
select /* no_use_nl(t,t1) */ t.* from t,t1 where t.id=t1.id;
/* no_use_hash(table_1,table_2) */
在多表关联查询中,指定不使用hash join方式进行多表关联。
select /* no_use_hash(t,t1) */ t.* from t,t1 where t.id=t1.id;
/* no_use_merge(table_1,table_2) */
在多表关联查询中,指定不使用merge join方式进行多表关联。
select /* no_use_merge(t,t1) */ t.* from t,t1 where t.id=t1.id;
其他常用的hint
/* parallel(table_name n) */
在sql中指定执行的并行度,这个值将会覆盖自身的并行度
select /* parallel(t 4) */ count(*) from t;
/* no_parallel(table_name) */
在sql中指定执行的不使用并行
select /* no_parallel(t) */ count(*) from t;
/* append */以直接加载的方式将数据加载入库
insert into t /* append */ select * from t;
/* dynamic_sampling(table_name n) */
设置sql执行时动态采用的级别,这个级别为0~10
select /* dynamic_sampling(t 4) */ * from t where id > 1234
/* cache(table_name) */
进行全表扫描时将table置于lru列表的最活跃端,类似于table的cache属性
select /* full(employees) cache(employees) */ last_name from employees
附录hint表格
hints for optimization approaches and goals |
all_rows |
the all_rows hint explicitly chooses the cost-based approach to optimize a statement block with a goal of best throughput (that is, minimum total resource consumption). |
first_rows |
the first_rows hint explicitly chooses the cost-based approach to optimize a statement block with a goal of best response time (minimum resource usage to return first row). in newer oracle version you should give a parameter with this hint: first_rows(n) means that the optimizer will determine an executionplan to give a fast response for returning the first n rows. |
choose |
the choose hint causes the optimizer to choose between the rule-based approach and the cost-based approach for a sql statement based on the presence of statistics for the tables accessed by the statement |
rule |
the rule hint explicitly chooses rule-based optimization for a statement block. this hint also causes the optimizer to ignore any other hints specified for the statement block. the rule hint does not work any more in oracle 10g. |
hints for access paths |
full |
the full hint explicitly chooses a full table scan for the specified table. the syntax of the full hint is full(table) where table specifies the alias of the table (or table name if alias does not exist) on which the full table scan is to be performed. |
rowid |
the rowid hint explicitly chooses a table scan by rowid for the specified table. the syntax of the rowid hint is rowid(table) where table specifies the name or alias of the table on which the table access by rowid is to be performed. (this hint depricated in oracle 10g) |
cluster |
the cluster hint explicitly chooses a cluster scan to access the specified table. the syntax of the cluster hint is cluster(table) where table specifies the name or alias of the table to be accessed by a cluster scan. |
hash |
the hash hint explicitly chooses a hash scan to access the specified table. the syntax of the hash hint is hash(table) where table specifies the name or alias of the table to be accessed by a hash scan. |
hash_aj |
the hash_aj hint transforms a not in subquery into a hash anti-join to access the specified table. the syntax of the hash_aj hint is hash_aj(table) where table specifies the name or alias of the table to be accessed.(depricated in oracle 10g) |
index |
the index hint explicitly chooses an index scan for the specified table. the syntax of the index hint is index(table index) where:table specifies the name or alias of the table associated with the index to be scanned and index specifies an index on which an index scan is to be performed. this hint may optionally specify one or more indexes: |
no_index |
the no_index hint explicitly disallows a set of indexes for the specified table. the syntax of the no_index hint is no_index(table index) |
index_asc |
the index_asc hint explicitly chooses an index scan for the specified table. if the statement uses an index range scan, oracle scans the index entries in ascending order of their indexed values. |
index_combine |
if no indexes are given as arguments for the index_combine hint, the optimizer will use on the table whatever boolean combination of bitmap indexes has the best cost estimate. if certain indexes are given as arguments, the optimizer will try to use some boolean combination of those particular bitmap indexes. the syntax of index_combine is index_combine(table index). |
index_join |
explicitly instructs the optimizer to use an index join as an access path. for the hint to have a positive effect, a sufficiently small number of indexes must exist that contain all the columns required to resolve the query. |
index_desc |
the index_desc hint explicitly chooses an index scan for the specified table. if the statement uses an index range scan, oracle scans the index entries in descending order of their indexed values. |
index_ffs |
this hint causes a fast full index scan to be performed rather than a full table. |
no_index_ffs |
do not use fast full index scan (from oracle 10g) |
index_ss |
exclude range scan from query plan (from oracle 10g) |
index_ss_asc |
exclude range scan from query plan (from oracle 10g) |
index_ss_desc |
exclude range scan from query plan (from oracle 10g) |
no_index_ss |
the no_index_ss hint causes the optimizer to exclude a skip scan of the specified indexes on the specified table. (from oracle 10g) |
hints for query transformations
|
no_query_transformation |
prevents the optimizer performing query transformations. (from oracle 10g) |
use_concat |
the use_concat hint forces combined or conditions in the where clause of a query to be transformed into a compound query using the union all set operator. normally, this transformation occurs only if the cost of the query using the concatenations is cheaper than the cost without them. |
no_expand |
the no_expand hint prevents the optimizer from considering or-expansion for queries having or conditions or in-lists in the where clause. usually, the optimizer considers using or expansion and uses this method if it decides that the cost is lower than not using it. |
rewrite |
the rewrite hint forces the optimizer to rewrite a query in terms of materialized views, when possible, without cost consideration. use the rewrite hint with or without a view list. if you use rewrite with a view list and the list contains an eligible materialized view, then oracle uses that view regardless of its cost. |
norewrite / no_rewrite |
in oracle 10g renamed to no_rewrite. the norewrite/no_rewrite hint disables query rewrite for the query block, overriding the setting of the parameter query_rewrite_enabled. |
merge |
the merge hint lets you merge views in a query. |
no_merge |
the no_merge hint causes oracle not to merge mergeable views. this hint is most often used to reduce the number of possible permutations for a query and make optimization faster. |
fact |
the fact hint indicated that the table should be considered as a fact table. this is used in the context of the star transformation. |
no_fact |
the no_fact hint is used in the context of the star transformation to indicate to the transformation that the hinted table should not be considered as a fact table. |
star_transformation |
the star_transformation hint makes the optimizer use the best plan in which the transformation has been used. without the hint, the optimizer could make a query optimization decision to use the best plan generated without the transformation, instead of the best plan for the transformed query. |
no_star_transformation |
do not use star transformation (from oracle 10g) |
unnest |
the unnest hint specifies subquery unnesting. |
no_unnest |
use of the no_unnest hint turns off unnesting for specific subquery blocks. |
hints for join orders
|
leading |
give this hint to indicate the leading table in a join. this will indicate only 1 table. if you want to specify the whole order of tables, you can use the ordered hint. syntax: leading(table) |
ordered |
the ordered hint causes oracle to join tables in the order in which they appear in the from clause. if you omit the ordered hint from a sql statement performing a join , the optimizer chooses the order in which to join the tables. you may want to use the ordered hint to specify a join order if you know something about the number of rows selected from each table that the optimizer does not. such information would allow you to choose an inner and outer table better than the optimizer could. |
hints for join operations
|
use_nl |
the use_nl hint causes oracle to join each specified table to another row source with a nested loops join using the specified table as the inner table. the syntax of the use_nl hint is use_nl(table table) where table is the name or alias of a table to be used as the inner table of a nested loops join. |
no_use_nl |
do not use nested loop (from oracle 10g) |
use_nl_with_index |
specifies a nested loops join. (from oracle 10g) |
use_merge |
the use_merge hint causes oracle to join each specified table with another row source with a sort-merge join. the syntax of the use_merge hint is use_merge(table table) where table is a table to be joined to the row source resulting from joining the previous tables in the join order using a sort-merge join. |
no_use_merge |
do not use merge (from oracle 10g) |
use_hash |
the use_hash hint causes oracle to join each specified table with another row source with a hash join. the syntax of the use_hash hint is use_hash(table table) where table is a table to be joined to the row source resulting from joining the previous tables in the join order using a hash join. |
no_use_hash |
do not use hash (from oracle 10g) |
hints for parallel execution |
parallel |
the parallel hint allows you to specify the desired number of concurrent query servers that can be used for the query. the syntax is parallel(table number number). the parallel hint must use the table alias if an alias is specified in the query. the parallel hint can then take two values separated by commas after the table name. the first value specifies the degree of parallelism for the given table, the second value specifies how the table is to be split among the instances of a parallel server. specifying default or no value signifies the query coordinator should examine the settings of the initialization parameters (described in a later section) to determine the default degree of parallelism. |
noparallel / no_parallel |
the noparallel hint allows you to disable parallel scanning of a table, even if the table was created with a parallel clause. in oracle 10g this hint was renamed to no_parallel. |
pq_distribute |
the pq_distribute hint improves the performance of parallel join operations. do this by specifying how rows of joined tables should be distributed among producer and consumer query servers. using this hint overrides decisions the optimizer would normally make. |
no_parallel_index |
the no_parallel_index hint overrides a parallel attribute setting on an index to avoid a parallel index scan operation. |
additional hints |
append |
when the append hint is used with the insert statement, data is appended to the table. existing free space in the block is not used. if a table or an index is specified with nologging, this hint applied with an insert statement produces a direct path insert which reduces generation of redo. |
noappend |
overrides the append mode. |
cache |
the cache hint specifies that the blocks retrieved for the table in the hint are placed at the most recently used end of the lru list in the buffer cache when a full table scan is performed. this option is useful for small lookup tables. in the following example, the cache hint overrides the table default caching specification. |
nocache |
the nocache hint specifies that the blocks retrieved for this table are placed at the least recently used end of the lru list in the buffer cache when a full table scan is performed. this is the normal behavior of blocks in the buffer cache. |
push_pred |
the push_pred hint forces pushing of a join predicate into the view. |
no_push_pred |
the no_push_pred hint prevents pushing of a join predicate into the view. |
push_subq |
the push_subq hint causes nonmerged subqueries to be evaluated at the earliest possible place in the execution plan. |
no_push_subq |
the no_push_subq hint causes non-merged subqueries to be evaluated as the last step in the execution plan. |
qb_name |
specifies a name for a query block. (from oracle 10g) |
cursor_sharing_exact |
oracle can replace literals in sql statements with bind variables, if it is safe to do so. this is controlled with the cursor_sharing startup parameter. the cursor_sharing_exact hint causes this behavior to be switched off. in other words, oracle executes the sql statement without any attempt to replace literals by bind variables. |
driving_site |
the driving_site hint forces query execution to be done for the table at a different site than that selected by oracle |
dynamic_sampling |
the dynamic_sampling hint lets you control dynamic sampling to improve server performance by determining more accurate predicate selectivity and statistics for tables and indexes. you can set the value of dynamic_sampling to a value from 0 to 10. the higher the level, the more effort the compiler puts into dynamic sampling and the more broadly it is applied. sampling defaults to cursor level unless you specify a table. |
spread_min_analysis |
this hint omits some of the compile time optimizations of the rules, mainly detailed dependency graph analysis, on spreadsheets. some optimizations such as creating filters to selectively populate spreadsheet access structures and limited rule pruning are still used. (from oracle 10g) |
hints with unknown status |
merge_aj |
the merge_aj hint transforms a not in subquery into a merge anti-join to access the specified table. the syntax of the merge_aj hint is merge_aj(table) where table specifies the name or alias of the table to be accessed.(depricated in oracle 10g) |
and_equal |
the and_equal hint explicitly chooses an execution plan that uses an access path that merges the scans on several single-column indexes. the syntax of the and_equal hint is and_equal(table index index) where table specifies the name or alias of the table associated with the indexes to be merged. and index specifies an index on which an index scan is to be performed. you must specify at least two indexes. you cannot specify more than five. (depricated in oracle 10g) |
star |
the star hint forces the large table to be joined last using a nested loops join on the index. the optimizer will consider different permutations of the small tables. (depricated in oracle 10g) |
bitmap |
usage: bitmap(table_name index_name) uses a bitmap index to access the table. (depricated ?) |
hash_sj
|
use a hash anti-join to evaluate a not in sub-query. use this hint in the sub-query, not in the main query. use this when your high volume not in sub-query is using a filter or nested loops join. try merge_aj if hash_aj refuses to work.(depricated in oracle 10g) |
nl_sj |
use a nested loop in a sub-query. (depricated in oracle 10g) |
nl_aj |
use an anti-join in a sub-query. (depricated in oracle 10g) |
ordered_predicates |
(depricated in oracle 10g)
|
expand_gset_to_union |
(depricated in oracle 10g) |
参考至:《让oracle跑得更快》谭怀远著
]]>