[20140108]12c新特性_Statistics Feedback.txt

[20140108]12c新特性_Statistics Feedback.txt

摘自:Apress.Pro.Oracle.SQL.2nd.Edition.Nov.2013.pdf

Statistics feedback, known as cardinality feedback prior to Oracle 12c, is a mechanism used by the optimizer to
improve automatically plans' repeated query executions that have cardinality misestimates. The first time a SQL
statement executes, the optimizer determines the execution plan and marks the plan to enable statistics feedback
monitoring if.

.Any of the tables in the statement have missing statistics
.There are ANDed or ORed filter predicates on a table
.Any predicates contain complex operators for which the optimizer cannot compute  cardinality estimates accurately

After a completed statement execution, the optimizer then compares the original cardinality estimates with
the actual cardinalities. If there are any significant differences, it stores the correct estimates to use during the next
execution of that statement to reoptimize (reparse) it. Prior to Oracle 12c, this is as far as things went. But, in 12c, the
optimizer also stores a SQL plan directive. A SQL plan directive contains additional information and instructions
the optimizer can use to generate a better plan the next time the statement is executed; but, it isn't pertinent just to a
single cursor. It is pertinent to any cursor that uses similar expressions.

自己写一个例子来说明:
SCOTT@ztest> @ver
BANNER                                                                               CON_ID
-------------------------------------------------------------------------------- ----------
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production              0

CREATE TABLE t1 AS SELECT ROWNUM id1,FLOOR(SQRT(ROWNUM)) id2 ,'test' NAME FROM DUAL CONNECT BY LEVEL
insert into t1 select rownum+9999,100 ,'test' NAME FROM DUAL CONNECT BY LEVEL
commit;

create index i_t1_id2 on t1(id2);
exec dbms_stats.gather_table_stats(user, 'T1',  method_opt=>'for all columns size 1 ',no_invalidate => false);

column low_value format a10
column high_value format a10
column data_type format a10
select column_name,data_type,num_distinct,low_value,high_value,num_buckets,histogram from dba_tab_cols where owner=user and table_name='T1';

COLUMN_NAME          DATA_TYPE  NUM_DISTINCT LOW_VALUE  HIGH_VALUE NUM_BUCKETS HISTOGRAM
-------------------- ---------- ------------ ---------- ---------- ----------- ---------------
NAME                 CHAR                  1 74657374   74657374             1 NONE
ID2                  NUMBER              100 C102       C202                 1 NONE
ID1                  NUMBER            19998 C102       C3026463             1 NONE

--ID2字段没有建立直方图.

alter session set statistics_level=all;

SCOTT@ztest> select * from t1 where id2=100 and name='Test';
no rows selected

SCOTT@ztest> @dpc '' ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID  g90m6jyj35qj7, child number 0
-------------------------------------
select * from t1 where id2=100 and name='Test'

Plan hash value: 588477579

-----------------------------------------------------------------------------------------------------------------------
| Id  | Operation                           | Name     | Starts | E-Rows | Cost (%CPU)| A-Rows |   A-Time   | Buffers |
-----------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                    |          |      1 |        |     2 (100)|      0 |00:00:00.01 |      46 |
|*  1 |  TABLE ACCESS BY INDEX ROWID BATCHED| T1       |      1 |      1 |     2   (0)|      0 |00:00:00.01 |      46 |
|*  2 |   INDEX RANGE SCAN                  | I_T1_ID2 |      1 |    200 |     1   (0)|   9999 |00:00:00.01 |      21 |
-----------------------------------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter("NAME"='Test')
   2 - access("ID2"=100)

--可以发现扫描索引时E-rows=200,而A-row=9999,两者存在很大差异.

SCOTT@ztest> select sql_id,sql_text,is_reoptimizable from v$sql where sql_id = 'g90m6jyj35qj7';
SQL_ID        SQL_TEXT                                                     I
------------- ------------------------------------------------------------ -
g90m6jyj35qj7 select * from t1 where id2=100 and name='Test'               Y

--is_reoptimizable=Y,说明存在下次执行有再优化的可能.

SELECT D.DIRECTIVE_ID,
       d.TYPE,
       d.state,
       d.auto_drop,
       d.reason,
       d.created,
       o.owner,
       O.OBJECT_type,
       o.object_name,
       o.SUBOBJECT_NAME
  FROM dba_sql_plan_directives d, dba_sql_plan_dir_objects o
 WHERE     d.directive_id = o.directive_id
       AND o.owner = USER
       AND o.object_name = 'T1'
order by 1;

no rows selected

--执行如下
exec dbms_spd.flush_sql_plan_directive;

SELECT D.DIRECTIVE_ID,
       d.TYPE,
       d.state,
       d.auto_drop,
       d.reason,
       d.created,
       o.owner,
       O.OBJECT_type,
       o.object_name,
       o.SUBOBJECT_NAME
  FROM dba_sql_plan_directives d, dba_sql_plan_dir_objects o
 WHERE     d.directive_id = o.directive_id
       AND o.owner = USER
       AND o.object_name = 'T1'
order by 1;

SCOTT@ztest> /
        DIRECTIVE_ID TYPE                 STATE         AUT REASON                               CREATED                        OWNER  OBJECT OBJECT_NAME          SUBOBJECT_
-------------------- -------------------- ------------- --- ------------------------------------ ------------------------------ ------ ------ -------------------- ----------
 1837740869431841840 DYNAMIC_SAMPLING     NEW           YES SINGLE TABLE CARDINALITY MISESTIMATE 2014-01-08 10:43:56.000000     SCOTT  TABLE  T1
 1837740869431841840 DYNAMIC_SAMPLING     NEW           YES SINGLE TABLE CARDINALITY MISESTIMATE 2014-01-08 10:43:56.000000     SCOTT  COLUMN T1                   ID2

--提示字段ID2存在SINGLE TABLE CARDINALITY MISESTIMATE.
SCOTT@ztest> select * from t1 where id2=100 and name='Test';
no rows selected

SCOTT@ztest> @dpc '' ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID  g90m6jyj35qj7, child number 1
-------------------------------------
select * from t1 where id2=100 and name='Test'
Plan hash value: 3617692013
-------------------------------------------------------------------------------------------------
| Id  | Operation         | Name | Starts | E-Rows | Cost (%CPU)| A-Rows |   A-Time   | Buffers |
-------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |      1 |        |    16 (100)|      0 |00:00:00.01 |      53 |
|*  1 |  TABLE ACCESS FULL| T1   |      1 |      1 |    16   (0)|      0 |00:00:00.01 |      53 |
-------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter(("ID2"=100 AND "NAME"='Test'))
Note
-----
   - statistics feedback used for this statement

--可以发现我并没有建立直方图在ID2字段,oracle在执行时发现统计信息存在差异,第2次执行纠正了执行计划,选择了全部扫描.

column is_bind_aware format a10
column is_bind_sensitive format a10
column is_obsolete format a10
column is_reoptimizable format a10
column is_resolved_adaptive_plan format a10
column is_shareable format a10

SELECT sql_id,
       child_number,
       sql_text,
       is_bind_aware,
       is_bind_sensitive,
       is_obsolete,
       is_reoptimizable,
       is_resolved_adaptive_plan,
       is_shareable
  FROM v$sql
 WHERE sql_id = 'g90m6jyj35qj7';

SQL_ID                CHILD_NUMBER SQL_TEXT                                                     IS_BIND_AW IS_BIND_SE IS_OBSOLET IS_REOPTIM IS_RESOLVE IS_SHAREAB
------------- -------------------- ------------------------------------------------------------ ---------- ---------- ---------- ---------- ---------- ----------
g90m6jyj35qj7                    0 select * from t1 where id2=100 and name='Test'               N          N          N          Y                     N
g90m6jyj35qj7                    1 select * from t1 where id2=100 and name='Test'               N          N          N          N                     Y

--child_numnber=0 的is_shareable='N',这样以后会剔除共享池.

时间: 2024-09-12 10:10:54

[20140108]12c新特性_Statistics Feedback.txt的相关文章

[20130808]12c新特性Temporary undo.txt

[20130808]12c新特性Temporary undo.txt 12C提供特性临时表执行dml时生成的undo保存在临时表空间,这个特性叫Temporary Undo,由数据库参数temp_undo_enabled控制,有两个优点:     1.很明显减少redo在临时表做dml操作时.    2.允许在physical standby database上对临时表执行dml操作.(没有环境无法测试) 测试: SQL> @ver BANNER                         

[20131121]12c新特性Session sequences.txt

[20131121]12c新特性Session sequences.txt http://docs.oracle.com/cd/E16655_01/server.121/e17209/statements_6016.htm SESSION Specify SESSION to create a session sequence, which is a special type of sequence that is specifically designedto be used with glo

[20140218]12c 新特性heat map.txt

[20140218]12c 新特性heat map.txt 12c 新特性,可以记录那些对象经常使用. 它可以在session或者system打开,不过我的测试好像在cdb数据库无效. SCOTT@ztest> @ver BANNER                                                                               CON_ID -----------------------------------------------

[20130915]12c新特性 varchar2支持32K长度.txt

[20130915]12c新特性 varchar2支持32K长度.txt oracle 12c以前如果字符串长度超过4000,必须使用blob或者clob类型.12c开始支持超过4000的字符串长度,提高一些应用的灵活性,达到32K,避免一些字段定义为clob,blob类型,提高处理能力. 但是12c默认的方式不支持大于4000的字符串长度,必须经过一些步骤升级完成,自己测试如下: SYS@test> @ver BANNER                                    

[20131212]12c新特性建表 属性DEFAULT ON NULL.txt

[20131212]12c新特性建表 属性DEFAULT ON NULL.txt 在12c上建立表可以让插入NULL等于某个特定的值.举一个例子: SCOTT@ztest> @verBANNER                                                                               CON_ID--------------------------------------------------------------------

12c新特性:Last Successful login time

[20150920]12c新特性:Last Successful login time.txt --12c有一个新特性,就是使用sqlplus登录时,会提示最后成功的登录时间.Last Successful login time. --起始这个不算什么特性,我个人想了解这个时间是从那里得来的. sqlplus scott/xxxxxx@test01p SQL*Plus: Release 12.1.0.1.0 Production on Sun Sep 20 17:54:37 2015 Copyr

《Oracle数据库管理与维护实战》——1.3 Oracle 12c新特性

1.3 Oracle 12c新特性 Oracle数据库管理与维护实战 纵观甲骨文全球大会和甲骨文公司的各种资讯,我们可以发现云计算和大数据是两个重要的主题,Oracle 12c则融合了这两大主题.与以往的Oracle数据库相比,Oracle 12c在16个方面进行了更新.本节将详细介绍Oracle 12c数据库中的16个新特性. 1.3.1 支持多线程模式 在Oracle 12c中,Oracle引入了多线程模式,允许在Windows平台之外的UNIX.Linux等系统使用多线程模式.结合多进程与

软件大会分享PPT:面向开发和DBA的Oracle 12c新特性

在2016年12月10日的『中国软件大会上』,我分享了一个主题:<面向开发人员和DBA的Oracle 12c新特性>,从安全的主题开始,以在线变更为主线,分享了Oracle 12c的一些新特性,尤其是12.2的部分新特性. 在这个主题中,12.2 的 lockdown profile 成为我的出发点,通过这一新的安全机制,Oracle 12c 的PDB权限得以被限制,可以防范PDB的高权限操作对全局产生影响. 而在12.2中PDB的Clone,可以在线进行,这是较12.1的又一大进步: Ora

Oracle Database 12c新特性对企业影响的思考

当今的世界,是云的世界. 而关系型数据库管理系统 (RDBMS) 也正在发生一场重大变革 ---- 从固定的服务器和存储转向灵活的云部署,从存储优化的数据库管理转向内存优化的数据库管理. 新一代的数据库环境已经来临.传统数据库中的Oracle也发布了12C的版本,经过一段时间的使用和测试,我们感觉Oracle Database 12C能为我们提供进行创新的基础,并企业以安全.可靠且经济高效的方式进行IT管理.本文主要探讨12C中的几个对新特性带来的影响. 1. 多租户架构(Multitenant