暂无图片
暂无图片
4
暂无图片
暂无图片
暂无图片

【译】谎言,该死的谎言与统计信息(一)

原创 赵勇 2022-05-17
977

Nigel Bayliss | November 8, 2011
产品经理
原文链接:https://blogs.oracle.com/optimizer/post/lies-damned-lies-and-statistics

理解和管理优化器统计信息是优化SQL执行的关键。知道何时和如何及时收集统计信息对于保持可接受的性能是至关重要的。为了澄清与统计信息相关的所有信息,我们将两篇有关优化器统计信息的白皮书一并提供出来,他们是:

使用Oracle Database 12c Release 2收集优化器统计信息的最佳实践
理解Oracle Database 12c Release 2的优化器统计信息

介绍

当Oracle数据库首次引入如何执行SQL语句的决策时,是由基于规则的优化器(RBO)决定的。顾名思义,基于规则的优化器遵循一组规则来确定SQL语句的执行计划。这些规则是经过排序的,因此,如果有两个可能的规则可以应用于SQL语句,则将使用排名最低的规则。

在Oracle Database 7中,为了处理当时添加到数据库中的增强功能,包括并行执行和分区,以及处理实际的数据内容和分布,基于成本的优化器(CBO)被引入了。基于成本的优化器会检查一条SQL可能的所有执行计划,并选取成本最低的一个。这里的成本表示对于给定的执行计划,资源使用量的评估。成本越低,执行计划的效率越高。为了使基于成本的优化器可以精确地确定给定执行计划的成本,就必须拥有SQL中访问的所有对象(表和索引)的信息,以及运行该SQL的系统的信息。

这些必须的信息通常统称为优化器统计信息。理解和管理优化器统计信息是优化SQL执行的关键。知道何时和如何及时收集统计信息对保持可接受的性能是至关重要的。本白皮书是关于优化器统计信息的两部分系列文章中的第一篇,通过实例,详细描述了优化器统计信息的不同概念,包括:

  • 统计信息是什么
  • 统计信息收集
  • 统计信息管理
  • 统计信息的附加类型

统计信息是什么?

统计信息是描述数据库及数据库中对象的数据的集合。统计信息供优化器为每条SQL选择最优的执行计划,它存储在数据字典中,并可通过诸如USER_TAB_STATISTICS这类数据库字典视图来访问。

图片.png
图1:统计信息存储在数据字典,供优化器用于确定执行计划

表和列统计信息

表统计信息包括表中的行数,表使用的数据块数,以及表中的平均行长等。优化器使用这些信息,并结合其它统计信息,计算执行计划中各种操作的成本,并评估相关操作所产生的行数。例如,表的访问成本是用数据块数,结合参数DB_FILE_MULTIBLOCK_READ_COUNT的值来计算的。可以在数据字典视图USER_TAB_STATISTICS中查看表的统计信息。

列统计信息包括一个列上的唯一值数据(NDV),以及在该列上找到的最小和最大值。可以在数据字典视图USER_TAB_COL_STATISTICS中查看列的统计信息。优化器使用列统计信息,并结合表统计信息(行数)来评估SQL操作将会返回的行数。例如,如果一个表有100行记录,并且对一个具有10个不同值的列上的相等谓词来访问表,那么优化器在假设数据分布一致的情况下,将基数估计为表中的行数除以该列不同值的数量,即,100/10=10。

图片.png

下一篇《谎言,该死的谎言与统计信息(二)》请点击这里

原文链接:https://blogs.oracle.com/optimizer/post/lies-damned-lies-and-statistics
原文内容:
Nigel Bayliss | November 8, 2011
Product Manager
Understanding and managing Optimizer statistics is key to optimal SQL execution. Knowing when and how to gather statistics in a timely manner is critical to maintaining acceptable performance. In order to clarify all of the information surrounding statistics we have put together two whitepapers on optimizer statistics. They are:

Best Practices for Gathering Optimizer Statistics with Oracle Database 12c Release 2
Understanding Optimizer Statistics With Oracle Database 12c Release 2
Introduction
When the Oracle database was first introduced the decision of how to execute a SQL statement was determined by a Rule Based Optimizer (RBO). The Rule Based Optimizer, as the name implies, followed a set of rules to determine the execution plan for a SQL statement. The rules were ranked so if there were two possible rules that could be applied to a SQL statement the rule with the lowest rank would be used.

In Oracle Database 7, the Cost Based Optimizer (CBO) was introduced to deal with the enhanced functionality being added to the Oracle Database at this time, including parallel execution and partitioning, and to take the actual data content and distribution into account. The Cost Based Optimizer examines all of the possible plans for a SQL statement and picks the one with the lowest cost, where cost represents the estimated resource usage for a given plan. The lower the cost the more efficient an execution plan is expected to be. In order for the Cost Based Optimizer to accurately determine the cost for an execution plan it must have information about all of the objects (tables and indexes) accessed in the SQL statement, and information about the system on which the SQL statement will be run.

This necessary information is commonly referred to as optimizer statistics. Understanding and managing Optimizer statistics is key to optimal SQL execution. Knowing when and how to gather statistics in a timely manner is critical to maintaining acceptable performance. This whitepaper is the first in a two part series on Optimizer statistics, and describes in detail, with worked examples, the different concepts of Optimizer statistics including;

What are Optimizer statistics
Gathering statistics
Managing statistics
Additional types of statistics
What are Optimizer Statistics?
Optimizer statistics are a collection of data that describe the database, and the objects in the database. These statistics are used by
the optimizer to choose the best execution plan for each SQL statement. Statistics are stored in the data dictionary, and can be accessed using data dictionary views such as USER_TAB_STATISTICS.

图片.png
Figure 1:Optimizer statistics stored in the data dictionary used by the Oracle Optimizer to determine execution plans

Table and Column Statistics
Table statistics include information on the number of rows in the table, the number of data blocks used for the table, as well as the average row length in the table. The optimizer uses this information, in conjunction with other statistics, to compute the cost of various operations in an execution plan, and to estimate the number of rows the operation will produce. For example, the cost of a table access is calculated using the number of data blocks combined with the value of the parameter DB_FILE_MULTIBLOCK_READ_COUNT. You can view table
statistics in the dictionary view USER_TAB_STATISTICS.

Column statistics include information on the number of distinct values in a column (NDV) as well as the minimum and maximum value found in the column. You can view column statistics in the dictionary view USER_TAB_COL_STATISTICS. The optimizer uses the column statistics information in conjunction with the table statistics (number of rows) to estimate the number of rows that will be returned by a SQL operation. For example, if a table has 100 records, and the table access evaluates an equality predicate on a column that has 10 distinct values, then the optimizer, assuming uniform data distribution, estimates the cardinality to be the number of rows in the table divided by the number of distinct values for the column or 100/10 = 10.
图片.png

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论