Cardinality Feedback

该特性主要针对 统计信息陈旧、无直方图或虽然有直方图但仍基数计算不准确的情况, Cardinality基数的计算直接影响到后续的JOIN COST等重要的成本计算评估,造成CBO选择不当的执行计划

Oracle Database - Enterprise Edition - Version 11.2.0.1 and later
Information in this document applies to any platform.

PURPOSE

This document records a number of Frequently Asked Questions pertaining to the tuning of SQL statements with the Statistics Feedback (formerly known as Cardinality Feedback) feature.

Scope & Application

DBAs and Support Engineers

QUESTIONS AND ANSWERS

What is Statistics Feedback?

Statistics Feedback is the ability of the  optimizer to automatically improves plans for repeated queries that have cardinality misestimates. The optimizer may estimate cardinalities incorrectly for many reasons, such as missing statistics, inaccurate statistics, or complex predicates. Statistics Feedback assists the optimizer to learn from its miscalculations in order to generate a potentially better plan using a more accurate cardinality estimation.

How does Statistics Feedback work?

Even when statistics are calculated as accurately as possible, an estimated cardinality may be inaccurate. On the first execution of a SQL statement an execution plan is generated. During the plan optimization, certain types of estimates are noted and the cursor that is produced is monitored. After the execution, some of the cardinality estimates in the plan are compared to the actual cardinalities seen during execution. If these estimates are found to differ significantly from the actual cardinalities then the corrected cardinalities are stored for later use. The next time the query is executed, it will be optimized (hard parsed) again, and this time the optimizer will use these corrected estimates in place of the originals used. A different plan, based on the more accurate statistics may be created.

Oracle is able to repeatedly re-optimize a statement using Statistics Feedback. This may be necessary since cardinality differences may depend on the structure and shape of a plan. Therefore it is possible that on the second execution of a query, after generating a new plan using Statistics Feedback, there are still more cardinality estimates that are found to deviate significantly from the actual cardinalities. In this case, Oracle can re-optimize yet again on the next execution.

There are however safeguards in place to guarantee that this will stabilize after a small number of executions, so you may see your plan changing in the first few executions, but  eventually one plan will be picked out and used for all subsequent executions.

A blog entry discussing cardinality feedback, including a short example, can be found here.

How is Statistics Feedback enabled ?

In 11gR2 Statistics Feedback is enabled by default. It can be disabled by setting the parameter "_OPTIMIZER_USE_FEEDBACK"  = FALSE.

How can Statistics Feedback be disabled ?

Statistics Feedback can be disabled by setting the parameter "_OPTIMIZER_USE_FEEDBACK"  = FALSE at either the system or session level
There is also a possibility to add an opt_param hint at the session level to disable cardinality feedback for a specific query as follows:

select   /*+ opt_param(‘_optimizer_use_feedback‘ ‘false‘) */  ...

Is Statistics Feedback persistent when the cursor is aged out?

Statistics Feedback is not persistent  when the cursor is aged out of the shared pool.
So any event that causes a statement to be flushed from the shared pool will cause the process to be repeated afresh.

How can we determine that Statistics Feedback was used?

Looking at the actual execution plan, there is a note stating "Statistics/Cardinality Feedback used for this statement" indicating that Statistics Feedback was used.

----------------------------------------------------------------------------------------------------
| Id  | Operation                               | Name     | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                        |          |       |       |    52 (100)|          |
|   1 |  NESTED LOOPS                           |          |       |       |            |          |
|   2 |   NESTED LOOPS                          |          |    13 |  1153 |    52   (3)| 00:00:01 |
|   3 |    VIEW                                 |          |     9 |   110 |    33   (4)| 00:00:01 |
|   4 |     HASH UNIQUE                         |          |     9 |    15 |    33   (4)| 00:00:01 |
|   5 |      COUNT                              |          |       |       |            |          |
|*  6 |       FILTER                            |          |       |       |            |          |
|   7 |        COLLECTION ITERATOR PICKLER FETCH| STR2TBL  |     9 |    15 |    31   (0)| 00:00:01 |
|*  8 |    INDEX RANGE SCAN                     | DATA_IDX |     2 |       |     3   (0)| 00:00:01 |
|   9 |   TABLE ACCESS BY INDEX ROWID           | DATA     |     2 |   184 |     4   (0)| 00:00:01 |
----------------------------------------------------------------------------------------------------

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

 6 - filter(ROWNUM>0)
 8 - access("DATA"."OBJECT_NAME"="T"."COLUMN_VALUE")

Note
-----
 - Cardinality Feedback used for this statement

What is the relationship between Statistics Feedback and USE_FEEDBACK_STATS in V$SQL_SHARED_CURSOR?

When a cursor is found to be a candidate for Statistics Feedback it will be hard parsed again using the new estimates. The child cursor will be marked as not being shareable and USE_FEEDBACK_STATS  set to ‘Y‘ in V$SQL_SHARED_CURSOR.
Note: As the need for Statistics Feedback was only detected while execution of this cursor, Statistics Feedback will not actually be used for this child. However it will be used for all further child cursors created.

At the next execution, as a result of the cursor being marked as not shareable, a hard parse will again be performed and a new child created with the optimizer having used  the new estimates for creating an optimizer plan.

If estimates are still found to be inaccurate, this process may need to be repeated

This will be done a fixed number of times - after which Statistics Feedback will not be attempted and the last child will be marked as shareable (USE_FEEDBACK_STATS =‘N‘)

column use_feedback_stats format a18
column sql_text format a80

select c.child_number, c.use_feedback_stats , s.sql_text from v$sql_shared_cursor c,v$sql s
where s.sql_id=c.sql_id and c.sql_id = ‘an4zdfz0h7513‘
and s.child_number= c.child_number;

CHILD_NUMBER USE_FEEDBACK_STATS SQL_TEXT
------------ ------------------ ------------------------------------------------------------
           0 Y                   select * from TABLE(cast( str_func(‘A,B,C‘ ) as s_type) ) t
           1 N                   select * from TABLE(cast( str_func(‘A,B,C‘ ) as s_type) ) t

Under what conditions is Statistics Feedback considered?

At present Statistics Feedback monitoring may be enabled in the following cases:

  • Tables with no statistics where dynamic sampling is not used
  • Multiple conjunctive or disjunctive filter predicates on a table and no extended statistics
  • Predicates containing complex operators that the optimizer cannot accurately compute selectivity estimates for.

In some cases, there are other techniques available to improve estimation; for instance, dynamic sampling or multi-column statistics allow the optimizer to more accurately estimate selectivity of conjunctive predicates. In cases where these techniques apply, Statistics Feedback is not enabled. 
However, if multi-column statistics are not present for the relevant combination of columns, the optimizer can fall back on Statistics Feedback.

时间: 2024-08-29 13:33:14

Cardinality Feedback的相关文章

Oracle优化器之基数反馈 (Cardinality Feedback)功能

概述 在Oracle 11gR2的版本上推出了基数反馈(Cardinality Feedback 以后简称CFB)功能,通过这个特性,对于某些查询在第一次执行时,如果CBO发现根据统计信息估算出的基数(Computed cardinality)和SQL执行时的实际值差距很大的情况发生时,在SQL下次执行时,会根据实际值调整基数,重新生成执行计划. 另外,基数反馈 (CFB)在12c版本上得到更进一步的扩展改称为统计反馈(Statistics Feedback),成为12c自动重新优化(Autom

Cardinality (基数)

名词 Cardinality:    优化器在计算成本的时候,需要从统计信息中取得数据,然后去估计每一步操作所涉及的行数,叫做Cardinality.    比如,一张表T有1000行数据,列COL1上没有直方图,没有空值,并且不重复的值(distinct value)有500个.那么,在使用条件“WHERE COL1=<VALUE>”去访问表的时候,优化器会假设数据均匀分布,它估计出会有1000/500=2行被选出来,2就是这步操作的Cardinality. 通常情况下,Cardinalit

DBA一天干的活

一.检查活动状态 通过查询基本视图,确认数据库和实例处于正常运行状态,可以对外提供数据服务. 1.1实例状态 SELECT instance_name,status FROM v$instance; 查询返回实例名称.状态,正常状态应为Open. 1.2会话信息 SELECT  SESSIONS_CURRENT,SESSIONS_HIGHWATER FROM v$license; 辅助查询,实例当前会话数和启动最高连接会话数量. SELECT inst_id,username,COUNT(*)

【问底】陈焕生:深入理解Oracle 的并行执行

摘要:Oracle并行执行是一种分而治之的方法.执行一个sql 时,分配多个并行进程同时执行数据扫描,连接以及聚合等操作,使用更多的资源,得到更快的sql 响应时间.并行执行是充分利用硬件资源,处理大量数据时的核心技术. Oracle并行执行是一种分而治之的方法.执行一个sql 时,分配多个并行进程同时执行数据扫描,连接以及聚合等操作,使用更多的资源,得到更快的sql 响应时间.并行执行是充分利用硬件资源,处理大量数据时的核心技术. 在本文中,在一个简单的星型模型上,我会使用大量例子和sql m

Oracle性能诊断艺术-读书笔记

create table test0605 as select * from dba_objects; select t1.owner,t1.object_name,t1.object_id from test0605 t1 where t1.object_id=3344; select t1.SQL_ID,t1.HASH_VALUE,t1.SQL_TEXT,t1.SQL_FULLTEXT,to_char(t1.LAST_ACTIVE_TIME,'yyyy-mm-dd hh24:mi:ss')

数据库服务器CPU 突然持续100%后自动下降原因诊断

1.CPU接近100% nmon数据 8月5日在9:20-9:40之间,出现CPU接近100%的情况,特点表现为9:20左右CPU急剧攀升,在9:45左右又快速下降 2.原因分析结果总述 2.1 持续时间与恢复方式 此次CPU攀高时间持续约20分钟,在无人工干预的情况下自动恢复 2.2 原因分析总述: 经过分析,原因为:4条SQL语句ORACLE优化器对LB_T_XXXVIDER视图.LB_T_XXXJECT_PROVIDER表.LA_XXCKAGE表的基数数据评估发生了巨大的差错,导致选择了错

如何获得正确的基数估计值

原文:http://www.oracle.com/technetwork/issue-archive/2013/13-jan/o13asktom-1886639.html I have a question about joining with collections and cardinality estimation. The optimizer is always estimating that 8,168 rows are coming back from my collection,

普通用户使用dbms_xplan包需要有的权限

普通用户使用dbms_xplan包查看执行计划需要对v$sql.v$sql_plan.v$session及v$sql_plan_statistics_all这四个视图同时具有select权限. 如果普通用户没有以上权限,执行SQL>select * from table(dbms_xplan.display_cursor(null,null,'advanced'));会报错如下: SQL> select * from table(dbms_xplan.display_cursor(null,n

转://从一条巨慢SQL看基于Oracle的SQL优化

http://mp.weixin.qq.com/s/DkIPwbDKIjH2FMN13GkT4w 本次分享的内容是基于Oracle的SQL优化,以一条巨慢的SQL为例,从快速解读SQL执行计划.如何从执行计划中找到SQL执行慢的Root Cause.统计信息与cardinality问题.探索性能杀手Filter操作.如何进行逻辑重写让SQL起飞等多个维度进行解析,最终优化巨慢SQL语句,希望能够抛砖引玉,和大家一起探讨SQL优化方法. 另外,还简单介绍了两种解决疑难SQL优化问题的工具:1005