加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

oracle 12c AUTO_SAMPLE_SIZE动态采用工作机制

发布时间:2020-12-12 13:42:35 所属栏目:百科 来源:网络整理
导读:The ESTIMATE_PERCENT parameter in DBMS_STATS.GATHER_*_STATS procedures controls the percentage of rows to sample when gathering optimizer statistics. What percentage of rows? should ?you sample to achieve accurate statistics? 100% will ens

The ESTIMATE_PERCENT parameter in DBMS_STATS.GATHER_*_STATS procedures controls the percentage of rows to sample when gathering optimizer statistics. What percentage of rows?should?you sample to achieve accurate statistics? 100% will ensure that statistics are accurate,but it could take a long time. A 1% sample will finish much more quickly but it could result in poor statistics. It’s not an easy question to answer,which is why it is best practice to use the default: AUTO_SAMPLE_SIZE.

?

?

In this post,I’ll cover how the AUTO_SAMPLE_SIZE algorithm works in Oracle Database 12c and how it affects the accuracy of the statistics being gathered. If you want to learn more of the history prior to Oracle Database 12c,then?this post?on Oracle Database 11g is a good place to look. I will indicate below where there are differences between Oracle Database 11g and Oracle Database 12c.

It’s not always appreciated that (in general) a large proportion of the time and resource cost required to gather statistics is associated with evaluating the number of distinct values (NDVs) for each column. Calculating NDV using an exact algorithm can be expensive because the database needs to record and sort column values while statistics are being gathered. If the NDV is high,retaining and sorting column values can become resource-intensive,especially if the sort spills to TEMP. Auto sample size instead uses an approximate (but accurate) algorithm to calculate NDV that avoids the need to sort column data or spill to TEMP. In return for this saving,the database can afford to use a full table scan to ensure that the other basic column statistics are accurate.

Similarly,it can be resource-intensive to generate histograms but the Oracle Database mitigates this cost as follows:

  • Frequency and top frequency histograms are created as the database gathers basic column statistics (such as NDV,MIN,MAX) from the full table scan mentioned above. This is new to Oracle Database 12c.
  • If a?frequency?or?top frequency?histogram is not feasible,then the database will collect?hybrid histograms?using a?sample?of the column data. Top frequency is only feasible when the top 254 values constitute more than 99% of the entire non null column values and frequency is only feasible if NDV is 254 or less.
  • When the user has specified ‘SIZE AUTO‘ in the METHOD_OPT clause for automatic histogram creation,the Oracle Database chooses which columns to consider for histogram creation based column usage data that’s gathered by the optimizer. Columns that are not used in WHERE-clause predicates or joins are not considered for histograms.

Both Oracle Database 11g and Oracle Database 12c use the following query to gather basic column statistics (it is a simplified here for illustrative purposes).

SELECT COUNT(c1),MIN(c1),MAX(c1)
FROM? t;

The query reads the table (T) and scans?all?rows (rather than using a sample). The database also needs to calculate the number of distinct values (NDV) for each column but the query does not use?COUNT(DISTINCT c1)?and so on,but instead,during execution,?a special statistics gathering row source is injected into the query. The statistics gathering row source uses a one-pass,hash-based distinct algorithm to gather NDV. The algorithm requires a full scan of the data,uses a bounded amount of memory and yields a highly accurate NDV that is nearly identical to a 100 percent sampling (a fact that can be proven mathematically). The statistics gathering row source also gathers the number of rows,number of nulls and average column length. Since a full scan is used,the number of rows,average column length,minimum and maximum values are 100% accurate.

Effect of auto sample size on histogram gathering

Hybrid histogram gathering is decoupled from basic column statistics gathering and uses a?sampleof column values. This technique was used in Oracle Database 11g to build height-balanced histograms. More information on this can be found in this?blog post. Oracle Database 12c replaced height-balanced histograms with hybrid histograms.

Effect of auto sample size on index stats gathering

AUTO_SAMPLE_SIZE affects how index statistics are gathered. Index statistics gathering is sample-based and it can potentially go through several iterations if the sample contains too few blocks or the sample size was too small to properly gather number of distinct keys (NDKs). The algorithm has not changed since Oracle Database 11g,so I’ve left it to the?previous blog?to go more detail. There one other thing to note:

At the time of writing,there are some cases where index sampling can lead to NDV mis-estimates for composite indexes. The best work-around is to create a column group on the relevant columns and use gather_table_stats. Alternatively,there is a one-off fix - 27268249. This patch changes the way NDV is calculated for indexes on large tables (and no column group is required). It is available for 12.2.0.1 at the moment,but note that it cannot be backported. As you might guess,it‘s significantly slower than index block sampling,but it‘s still very fast. At the time of writing,if you find a case where index NDV is causing an issue with a query plan,then the?recommended approach is to add a column group rather than attempting to apply this patch.

Summary:

Note that top frequency and hybrid histograms are new to Oracle Database 12c. Oracle Database 11g had frequency and height-balanced histograms only. Hybrid histograms replaced height-balanced histograms.

  1. The auto sample size algorithm uses a full table scan (a 100% sample) to gather basic column statistics.
  2. The cost of a full table scan (verses row sampling) is mitigated by the approximate NDV algorithm,which eliminates the need to sort column data.
  3. The approximate NDV gathered by AUTO_SAMPLE_SIZE is close to the accuracy of a 100% sample.
  4. Other basic column statistics,such as the number of nulls,minimal and maximal values have an accuracy equivalent to 100% sampling.
  5. Frequency and top frequency histograms are created using a 100%* sample of column values and are created when basic column statistics are gathered. This is different to Oracle Database 11g,which decoupled frequency histogram creation from basic column statistics gathering (and used a sample of column values).
  6. Hybrid histograms are created using a sample of column values. Internally,this step is decoupled from basic column statistics gathering.
  7. Index statistics are gathered using a sample of column values. The sample size is determined automatically.
*There is an exception to? case? 5,above. Frequency histograms are created using a sample if OPTIONS=>‘GATHER AUTO‘ is used after a bulk load where statistics have been gathered using online statistics gathering.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读