https://blog.csdn.net/indexman/article/details/42324465
https://blog.csdn.net/renfengjun/article/details/21385335
http://www.killdb.com/2012/04/12/in-depth-analysis-of-the-parameter-optimizer_dynamic_sampling.html
1、什么是动态采样?
动态抽样从 oracle 9i第2版引入。它使得优化器(CBO)在硬解析期间有能力抽样一个未分析的表
(any table that has been created and loaded but not yet analyzed)的统计(决定表默认统计),并且可以验证优化器的”猜想“。
因其只在查询硬解析期间为优化器动态生成更好的统计,得名动态采样。
动态采样提供11个设置级别。注意:9i中其默认值为1 到了10g默认值为2
表级别的采样块数,一个简单的公式:
2^(level - 1)*_optimizer_dyn_smp_blks (_optimizer_dyn_smp_blks的默认值为 32)
因此level 1的采样块数为
2^0*32 = 1*32 = 32
level 4的采样块数为
2^3*32 = 8 * 32 = 256
level 9的采样块数为
2^8*32 = 256 * 32 = 8192
level 10再次出现了跳跃,有2的32次方个块,也就是一个表中所有的块。读者可以自己对比下,表级别的采样块数与session/system和cursor level的不同。
2、动态采样如何工作?
有两种使用方式:
△ 设置OPTIMIZER_DYNAMIC_SAMPLING参数,可以再实例和会话级别设置动态采样。
△ 使用DYNAMIC_SAMPLING hint
动态采样默认为启动状态,可以设置 OPTIMIZER_DYNAMIC_SAMPLING=0来禁用掉这一特性。
OPTIMIZER_DYNAMIC_SAMPLING也是和动态采样最重要的参数,它控制着动态采样级别。
3、什么时候适合采用动态采样?
这是一个狡猾的问题,没有一定使用经验,还真不好意思说。
通常:
1)我们使用3和4级进行动态采样,默认数据库是级别2的动态采样。
2)如果我们SQL的解析时间很快但是执行时间巨慢,可以考虑使用动态采样。典型的就是数据仓库系统。
3)OLTP系统中都是一个SQL重复执行,解析和执行都在瞬息之间,所以不建议使用高级别的动态采样。这会给SQL带来硬解析消耗。
4)表分析(使用dbms_Stats)可以提供较高品质的统计信息,而动态采样则是一个折中方案,只能提供粗粒度的统计信息,所以对于一些对统计信息要求较高的查询中,因为统计信息缺失而使用动态采样是无法保证优化器选择最优的执行计划
这个时候可以考虑SQL Profile,你可以理解为“静态采样”。
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.应用场景
下面列举3个动态采样的典型应用场景
5.1 缺失统计信息
当查询中的一个或者多个表没有统计信息,那么优化器就会收集关于表的基本信息用来执行优化操作。
1).创建表
SQL> create table tuning8_tab1 nologging as select level as id , 'name'|| level as name from dual connect by level <= 10000 ;
Table created.
2).创建索引
SQL> create index idx_tuning8_tab1_id on tuning8_tab1(id) ;
Index created.
3).查询测试
--先看一下参数的设置
SQL> show parameter samp
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
optimizer_dynamic_sampling integer 2
SQL>
SQL> set autot trace;
SQL> select * from tuning8_tab1 where id=2 ;
Execution Plan
----------------------------------------------------------
Plan hash value: 3712969662
---------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 37 | 2 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| TUNING8_TAB1 | 1 | 37 | 2 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | IDX_TUNING8_TAB1_ID | 1 | | 1 (0)| 00:00:01 |
---------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("ID"=2)
Note
https://blog.csdn.net/renfengjun/article/details/21385335
http://www.killdb.com/2012/04/12/in-depth-analysis-of-the-parameter-optimizer_dynamic_sampling.html
1、什么是动态采样?
动态抽样从 oracle 9i第2版引入。它使得优化器(CBO)在硬解析期间有能力抽样一个未分析的表
(any table that has been created and loaded but not yet analyzed)的统计(决定表默认统计),并且可以验证优化器的”猜想“。
因其只在查询硬解析期间为优化器动态生成更好的统计,得名动态采样。
动态采样提供11个设置级别。注意:9i中其默认值为1 到了10g默认值为2



表级别的采样块数,一个简单的公式:
2^(level - 1)*_optimizer_dyn_smp_blks (_optimizer_dyn_smp_blks的默认值为 32)
因此level 1的采样块数为
2^0*32 = 1*32 = 32
level 4的采样块数为
2^3*32 = 8 * 32 = 256
level 9的采样块数为
2^8*32 = 256 * 32 = 8192
level 10再次出现了跳跃,有2的32次方个块,也就是一个表中所有的块。读者可以自己对比下,表级别的采样块数与session/system和cursor level的不同。
2、动态采样如何工作?
有两种使用方式:
△ 设置OPTIMIZER_DYNAMIC_SAMPLING参数,可以再实例和会话级别设置动态采样。
△ 使用DYNAMIC_SAMPLING hint
动态采样默认为启动状态,可以设置 OPTIMIZER_DYNAMIC_SAMPLING=0来禁用掉这一特性。
OPTIMIZER_DYNAMIC_SAMPLING也是和动态采样最重要的参数,它控制着动态采样级别。
3、什么时候适合采用动态采样?
这是一个狡猾的问题,没有一定使用经验,还真不好意思说。
通常:
1)我们使用3和4级进行动态采样,默认数据库是级别2的动态采样。
2)如果我们SQL的解析时间很快但是执行时间巨慢,可以考虑使用动态采样。典型的就是数据仓库系统。
3)OLTP系统中都是一个SQL重复执行,解析和执行都在瞬息之间,所以不建议使用高级别的动态采样。这会给SQL带来硬解析消耗。
4)表分析(使用dbms_Stats)可以提供较高品质的统计信息,而动态采样则是一个折中方案,只能提供粗粒度的统计信息,所以对于一些对统计信息要求较高的查询中,因为统计信息缺失而使用动态采样是无法保证优化器选择最优的执行计划
这个时候可以考虑SQL Profile,你可以理解为“静态采样”。
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.应用场景
下面列举3个动态采样的典型应用场景
5.1 缺失统计信息
当查询中的一个或者多个表没有统计信息,那么优化器就会收集关于表的基本信息用来执行优化操作。
1).创建表
SQL> create table tuning8_tab1 nologging as select level as id , 'name'|| level as name from dual connect by level <= 10000 ;
Table created.
2).创建索引
SQL> create index idx_tuning8_tab1_id on tuning8_tab1(id) ;
Index created.
3).查询测试
--先看一下参数的设置
SQL> show parameter samp
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
optimizer_dynamic_sampling integer 2
SQL>
SQL> set autot trace;
SQL> select * from tuning8_tab1 where id=2 ;
Execution Plan
----------------------------------------------------------
Plan hash value: 3712969662
---------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 37 | 2 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| TUNING8_TAB1 | 1 | 37 | 2 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | IDX_TUNING8_TAB1_ID | 1 | | 1 (0)| 00:00:01 |
---------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("ID"=2)
Note