本文将回答:什么是动态抽样?动态抽样有啥作用?以及不同级别的动态抽样的意思? 1、什么是动态采样? 动态抽样从 oracle 9i第2版引入。它使得优化器(cbo)在硬解析期间有能力抽样一个未分析的表 (any table that has been created and loaded but not ye
本文将回答:什么是动态抽样?动态抽样有啥作用?以及不同级别的动态抽样的意思?
1、什么是动态采样?
动态抽样从 oracle 9i第2版引入。它使得优化器(cbo)在硬解析期间有能力抽样一个未分析的表
(any table that has been created and loaded but not yet analyzed)的统计(决定表默认统计),并且可以验证优化器的”猜想“。
因其只在查询硬解析期间为优化器动态生成更好的统计,得名动态采样。
动态采样提供11个设置级别。注意:9i中其默认值为1 到了10g默认值为2
2、动态采样如何工作?
有两种使用方式:
△ 设置optimizer_dynamic_sampling参数,可以再实例和会话级别设置动态采样。
△ 使用dynamic_sampling hint
来看一下不使用动态采样的日子怎么过的
create table tasselect owner, object_typefrom all_objects/select count(*) from t;count(*)------------------------68076
code1: 禁用动态采样观察默认基数
set autotrace traceonly explainsql> select /*+ dynamic_sampling(t 0) */ * from t;execution plan------------------------------plan hash value: 1601196873--------------------------------------------------------------------------| id | operation | name | rows | bytes | cost (%cpu)| time |--------------------------------------------------------------------------| 0 | select statement | | 16010 | 437k| 55 (0)| 00:00:01 || 1 | table access full| t | 16010 | 437k| 55 (0)| 00:00:01 |--------------------------------------------------------------------------
--注意0级别即为禁用动态采样,环境默认是开启动态采样的
执行计划显示基数:16010远低于上面查询的68076,明显不靠谱。
code2: 更加接近显示的基数
select * from t;execution plan------------------------------plan hash value: 1601196873--------------------------------------------------------------------------| id | operation | name | rows | bytes | cost (%cpu)| time |--------------------------------------------------------------------------| 0 | select statement | | 77871 | 2129k| 56 (2)| 00:00:01 || 1 | table access full| t | 77871 | 2129k| 56 (2)| 00:00:01 |--------------------------------------------------------------------------note------------------------------------------- dynamic sampling used for this statementcode3: 被高估的基数sql> delete from t;68076 rows deleted.sql> commit;commit complete.sql> set autotrace traceonly explainsql> select /*+ dynamic_sampling(t 0) */ * from t;execution plan------------------------------plan hash value: 1601196873--------------------------------------------------------------------------| id | operation | name | rows | bytes | cost (%cpu)| time |--------------------------------------------------------------------------| 0 | select statement | | 16010 | 437k| 55 (0)| 00:00:01 || 1 | table access full| t | 16010 | 437k| 55 (0)| 00:00:01 |--------------------------------------------------------------------------sql> select * from t;execution plan-----------------------------plan hash value: 1601196873------------------------------------------------------------------------【本文来自鸿网互联 (http://www.68idc.cn)】--| id | operation | name | rows | bytes | cost (%cpu)| time |--------------------------------------------------------------------------| 0 | select statement | | 1 | 28 | 55 (0)| 00:00:01 || 1 | table access full| t | 1 | 28 | 5 (0)| 00:00:01 |--------------------------------------------------------------------------note---------------------------------------- dynamic sampling used for this statement
3、动态采样何时帮助优化器验证其猜测?
我们知道当使用dbms_stats收集了表信息后,优化器会得到以下统计:
1)表,行数,平均行宽等;
2)单独列,高低值,唯一值数量,直方图(可能)等;
3)单独索引,聚集因素,叶子块数量,索引高度等。
但注意这里面缺少了某些关键统计信息,例如表中不同列数据之间的关联!
假设你你有一个全球人口普查表!
一个属性是:出生月份month_born_in,另一个属性是:所属星座zodiac_sign。收集信息后,你问优化器出生在11月份的人数?
假设12个月人数正常分布,那么优化器很快给出答案是全量数据的1/12!再问一个:星座是双鱼座的人数呢?答案也是1/12!
迄今为止优化器对答如流!!!nice work!
但是第3个问题来了:出生在11月份并且星座是双鱼座的人数是多少呢?
明眼人转下脑子就知道答案是0(双鱼座2月19日-3月20日)!但是我们看优化器的答案:1/12/12!!! 多么异想天开的答案,思维定式!这样就会诞生差的执行计划,
也正是在此时我们的动态采样开始干预:
code4: 创建模拟数据
sql > create table tas select decode( mod(rownum,2), 0, 'n', 'y' ) flag1,decode( mod(rownum,2), 0, 'y', 'n' ) flag2, a.*from all_objects a/table created.sql > create index t_idx on t(flag1,flag2);index created.sql > begindbms_stats.gather_table_stats( user, 't',method_opt=>'for all indexed columns size 254' );end;/pl/sql procedure successfully completed.sql> select num_rows, num_rows/2,num_rows/2/2 from user_tableswhere table_name = 't';num_rows num_rows/2 num_rows/2/2-------- ---------- ------------68076 34038 17019code5:验证一下上面的说法:sql> set autotrace traceonly explainsql> select * from t where flag1='n';execution plan------------------------------plan hash value: 1601196873--------------------------------------------------------------------------| id | operation | name | rows | bytes | cost (%cpu)| time |--------------------------------------------------------------------------| 0 | select statement | | 33479 | 3432k| 292 (1)| 00:00:04 ||* 1 | table access full| t | 33479 | 3432k| 292 (1)| 00:00:04 |--------------------------------------------------------------------------predicate information (identified by operation id):---------------------------------------------------1 - filter(flag1='n')sql> select * from t where flag2='n';execution plan----------------------------plan hash value: 1601196873---------------------------------------------------------------------------| id | operation | name | rows | bytes | cost (%cpu)| time |---------------------------------------------------------------------------| 0 | select statement | | 34597 | 3547k| 292 (1)| 00:00:04 ||* 1 | table access full| t | 34597 | 3547k| 292 (1)| 00:00:04 |---------------------------------------------------------------------------predicate information (identified by operation id):---------------------------------------------------1 - filter(flag2='n')--至此一切正常!so far, so good!code5: here comes the problemsql> select * from t where flag1 = 'n' and flag2 = 'n';execution plan----------------------------plan hash value: 1601196873--------------------------------------------------------------------------| id | operation | name | rows | bytes | cost (%cpu)| time |--------------------------------------------------------------------------| 0 | select statement | | 17014 | 1744k| 292 (1)| 00:00:04 ||* 1 | table access full| t | 17014 | 1744k| 292 (1)| 00:00:04 |--------------------------------------------------------------------------predicate information (identified by operation id):----------------------------------------------------1 - filter(flag1 = 'n' and flag2 = 'n')--验证了我们前面说的优化器此时异想天开了code7: 动态采样听令,开始介入sql> select /*+ dynamic_sampling(t 3) */ * from t where flag1 = 'n' and flag2 = 'n';execution plan-----------------------------plan hash value: 470836197------------------------------------------------------------------------------------| id | operation | name | rows | bytes | cost (%cpu)| time |------------------------------------------------------------------------------------| 0 | select statement | | 6 | 630 | 2 (0)| 00:00:01 || 1 | table access by index rowid| t | 6 | 630 | 2 (0)| 00:00:01 ||* 2 | index range scan | t_idx | 6 | | 1 (0)| 00:00:01 |------------------------------------------------------------------------------------predicate information (identified by operation id):----------------------------------------------------2 - access(flag1='n' and flag2='n')
code8: 我们打开sql_trace会看到以下语句:
select /* opt_dyn_samp */ /*+ all_rows ignore_where_clauseno_parallel(samplesub) opt_param('parallel_execution_enabled', 'false')no_parallel_index(samplesub) no_sql_tune */ nvl(sum(c1),:sys_b_00),nvl(sum(c2),:sys_b_01), nvl(sum(c3),:sys_b_02)from(select /*+ ignore_where_clause no_parallel(t) full(t)no_parallel_index(t) */ :sys_b_03 as c1, case when t.flag1=:sys_b_04 and t.flag2=:sys_b_05 then :sys_b_06 else :sys_b_07end as c2, case when t.flag2=:sys_b_08 and t.flag1=:sys_b_09then :sys_b_10 else :sys_b_11 end as c3 from t sample block(:sys_b_12 , :sys_b_13) seed (:sys_b_14) t) samplesub
可以看出来优化器在验证其猜想。。。
4、动态采样级别:
现在列出11个级别,详细请参考:http://docs.oracle.com/cd/b19306_01/server.102/b14211/stats.htm#i43032
1)level 0: do not use dynamic sampling.
0级:不使用动态采样。
2)level 1: sample all tables that have not been analyzed if the following criteria are met: (1) there is at least 1 unanalyzed table in the query; (2) this unanalyzed table is joined to another table or appears in a subquery or non-mergeable view; (3) this unanalyzed table has no indexes; (4) this unanalyzed table has more blocks than the number of blocks that would be used for dynamic sampling of this table. the number of blocks sampled is the default number of dynamic sampling blocks (32).
1级:满足以下条件则采样所有没被分析的表:
(1)查询中至少有一个未分析表;
(2)这个未分析表被关联另外一个表或者出现在子查询或非merge视图中;
(3)这个未分析表有索引;
(4)这个未分析表有多余动态采样默认的数据块数(默认是32块)。
3)level 2: apply dynamic sampling to all unanalyzed tables. the number of blocks sampled is two times the default number of dynamic sampling blocks.
2级:对所有未分析表进行动态采样。采样数据块数量是默认数量的2倍。
4)level 3: apply dynamic sampling to all tables that meet level 2 criteria, plus all tables for which standard selectivity estimation used a guess for some predicate that is a potential dynamic sampling predicate. the number of blocks sampled is the default number of dynamic sampling blocks. for unanalyzed tables, the number of blocks sampled is two times the default number of dynamic sampling blocks.
3级:在2级基础上加上那些使用了猜想选择消除表,采样数据块数量等于默认数量。对于未分析表,采样数量2倍于默认数量。
5)level 4: apply dynamic sampling to all tables that meet level 3 criteria, plus all tables that have single-table predicates that reference 2 or more columns. the number of blocks sampled is the default number of dynamic sampling blocks. for unanalyzed tables, the number of blocks sampled is two times the default number of dynamic sampling blocks.
4级:在3级基础上加上那些有单表谓词关联2个或多个列,采样数据块数量等于默认数量。对于未分析表,采样数量2倍于默认数量。
6)levels 5, 6, 7, 8, and 9: apply dynamic sampling to all tables that meet the previous level criteria using 2, 4, 8, 32, or 128 times the default number of dynamic sampling blocks respectively.
5,6,7,8,9级在4级基础上分别使用2,4,8,32,128倍于默认动态采样数据块数量。
7)level 10: apply dynamic sampling to all tables that meet the level 9 criteria using all blocks in the table.
10级:在9级基础上对表中所有数据块进行采样。
5、什么时候适合采用动态采样?
这是一个狡猾的问题,没有一定使用经验,还真不好意思说。
通常:
1)我们使用3和4级进行动态采样。
2)如果我们sql的解析时间很快但是执行时间巨慢,可以考虑使用动态采样。典型的就是数据仓库系统。
3)oltp系统中都是一个sql重复执行,解析和执行都在瞬息之间,所以不建议使用高级别的动态采样。这会给sql带来硬解析消耗。
这个时候可以考虑sql profile,你可以理解为“静态采样”。