I’ve previously discussed some basic concepts and formulas regarding how the CBO derives index related costings via the I/O costing model. Time to look at system statistics and the CPU costing model with specific regard to indexes.
The first point I would make is that the CPU costing model has some significant improvements over the older I/O costing method and I would strongly recommend adopting the CPU costing model where possible. I’ll explain some of these improvements and advantages over the coming posts.
The I/O costing model basically looks at the cost of a specific execution plan in terms of the estimated number of physical I/Os. The less I/Os, the less costly and more efficient the execution plan and the faster the expected response times. There are however a number of short falls with this basic I/O costing strategy in that is doesn’t automatically differentiate between the costs associated with different types of I/Os (eg. between single block and multiblock reads), it doesn’t automatically determine a typical or average size of a multiblock I/O and it doesn’t cost and take into consideration the time and overheads associated with likely CPU resources.
The CPU costing model attempts to take into consideration these previous limitations. It automatically takes into consideration discrepancies between the time to complete an average single block I/O versus a multiblock I/O, automatically determines the average size of a multiblock I/Os so it can more accurately determine the likely number of multiblock I/Os in a FTS and automatically determines the expected CPU time for a specific task.
To use the CBO CPU costing model, one needs to collect system statistics so that CBO has this additional information, based on the actual system hardware characteristics (Note: since 10g, the hidden parameter _optimizer_cost_model defaults to ‘cpu’ and so is used by default). You do this with thedbms_stats.gather_system_stats procedure. You can collect “Noworkload” statistics in which Oracle basically randomly reads the database data files to determine base statistics such as the average I/O seek time, the average I/O transfer speed and the CPU speed. However, I would rather recommend the collection of “Workload” stats which are based on the actual workload characteristics of your hardware, based on the real load on your system during the time in which system statistics are gathered (in which case Noworkload statistics are simply ignored).
You can gather Workload system statistics by either running:
dbms_stats.gather_system_stats(‘START’) to start the system stats collection process followed by dbms_stats.gather_system_stats(‘STOP’) to stop the collection process over a typical, workload period, or
dbms_stats.gather_system_stats(‘INTERVAL’, interval=> 120) to say collect system workload stats over a 120 minute period.
To view the collected system statistics, query SYS.AUX_STATS$.
SQL> SELECT pname, pval1 FROM SYS.AUX_STATS$
WHERE pname IN (‘SREADTIM’, ‘MREADTIM’, ‘MBRC’, ‘CPUSPEED’);
PNAME PVAL1 ------------ ---------- SREADTIM 5 MREADTIM 10 CPUSPEED 1745 MBRC 10
The four systems statistics that I’ll focus on for now are:
SREADTIM – time in milliseconds for a single block I/O
MREADTIM- time in milliseconds for a multiblock I/O
CPUSPEED - million of CPU cycles per second
MBRC – average number of blocks actually read during multiblock read operations
In the above figures, just note therefore that a multiblock read on average takes approximately double the time of that of a single block read and that on average, 10 blocks are read during a multiblock read operation. This provides the CBO with vital information regarding how to now cost and compare potential execution plans.
The CBO CPU costing model basically looks at the total time required to complete an execution plan by summing:
total time to complete all single block I/O activity +
total time to complete all multiblock I/O activity +
total time to complete all the CPU activity
This can basically be calculated by:
sum of all the single block I/Os x average wait time for a single block I/O +
sum of all the multiblock I/Os x average wait time for a multiblock I/O +
sum of all the required CPU cycles / CPU cycles per second
In theory, this should provide the total response time to service an execution plan. However, to keep the actual “cost” figures calculated by the CBO consistent with the I/O costing model, the CBO divides this total time by the average time for a single block I/O, such that the full formula becomes:
(sum of all the single block I/Os x average wait time for a single block I/O +
sum of all the multiblock I/Os x average wait time for a multiblock I/O +
sum of all the required CPU cycles / CPU cycles per second)
/
average wait time for a single block I/O
The final “cost” figure, even with the CPU costing model, is therefore still expressed in units of single block I/Os. This is an important point …
So how does the CBO determine the value of the various figures within this formula ? Well as we’ll see, the CBO get’s the required information both from the system statistics and from the costing formulas previously discussed with the I/O costing model.
However, for index related access paths, there’s some good news regarding being able to simplify matters somewhat.
The first bit of good news is that from the perspective of an index access path, there are no multiblock I/Os (except for a Fast Full Index Scan) and so the CPU costing formula can be simplified for indexes to remove the multiblock read component and be just:
(sum of all the single block I/Os x average wait time for a single block I/O +
sum of all the required CPU cycles / CPU cycles per second)
/
average wait time for a single block I/O
Secondly, if the CPU component is relatively trivial, it may not be sufficient enough to count towards the final cost. As smaller index scans are likely to consume little CPU, it means the CPU component can also generally be ignored. This reduces the formula for such index scans to just:
(sum of all the single block I/Os x average wait time for a single block I/O)
/
average wait time for a single block I/O
However, the average wait time for a single block I/O now becomes redundant in this simplified equation, reducing the cost to now be just:
sum of all the single block I/Os
Well the next bit of good news for those that have followed my previous blog entries with regard to the CBO and Indexes is that the previous formulas regarding the I/O costing model are still applicable when determining the sum of all expected I/Os. The sum of all the single block I/Os associated with an index scan is still basically:
sum of all the single block I/Os = index blevel + ceil(index selectivity x leaf blocks) + ceil(table selectivity x clustering factor)
In other words, for smaller index scan execution plans, the cost calculated by CBO using the CPU costing model is the same as with the I/O costing model. So no, I wasn’t wasting everyone’s time discussing the various formulas using the older I/O costing model
If we run the same demo as I ran previously in my initial post regarding theCBO and Indexes where the total cost of the index access plan was 18, but this time using the system statistics listed above:
SQL> alter session set “_optimizer_cost_model” = cpu;
Session altered.
SQL> SELECT * FROM bowie_stuff2 WHERE id = 420;
2000 rows selected.
Execution Plan
———————————————————-
Plan hash value: 134336835
——————————————————————————–
|Id|Operation |Name |Rows|Bytes|Cost (%CPU)|Time |
——————————————————————————–
| 0|SELECT STATEMENT | |2000|36000| 18 (0)|00:00:01|
| 1| TABLE ACCESS BY INDEX ROWID|BOWIE_STUFF2 |2000|36000| 18 (0)|00:00:01|
|*2| INDEX RANGE SCAN |BOWIE_STUFF2_I|2000| | 9 (0)|00:00:01|
——————————————————————————–
We notice that the cost remains exactly the same at 9 for the index range scan component and exactly the same at 18 for the total execution plan when comparing the cost of using the IO costing model vs. the CPU costing model. Introducing system statistics hasn’t changed things for this particular index related execution plan.
And this is a very common observation. As indexes use single block I/Os, as the CBO cost remains as a unit of single block I/Os and as CPU consumption for an index scan is often trivial, the resultant costs for index access paths often remain unchanged with the CPU costing model.
Previously, we looked at how changing parameters such as the optimizer_index_cost_adj impacts the costings of index related execution plans to create a level playing field between index and FTS execution plans.
The key point to make with regard to system statistics and the CPU costing model is that in general, the system statistics and the associated formula will automatically ensure a level playing field. However, unlike the optimizer parameters, it will do so by typically adjusting the associated costs of the FTS(rather than the index accesses) as the true costs and wait times associated with multiblock FTS are calculated, but are divided by and expressed in units of single block reads.
So rather than decreasing the associated costs of an index access path, system statistics and the CPU costing model will typically create a level playing by automatically increasing the associated costs of a FTS as appropriate.
To be discussed further …
如有错误,欢迎指正
邮箱:czmcj@163.com
相关推荐
- CBO的成本估算模型:CBO基于统计信息和数据库配置来估算每个操作的成本,如I/O、CPU等。 - 优化器模式:固定规则(Rule-Based Optimizer, RBO)与成本基础(CBO)的区别,以及何时应该使用CBO。 - 参数调优:学习...
cbooracle cbo
Oracle的Cost-Based Optimizer (CBO) 是一种自动选择最优执行计划的机制,它通过评估不同执行路径的成本来决定如何执行SQL查询。CBO会考虑各种因素,包括表的大小、索引的存在、数据分布等,以确定最有效率的查询...
为了验证CBO的成本计算,我们需要考虑CPU的运转次数(#CPUCycles),这是无法直接获取的。在分析不同的访问路径(如全表扫描Full Table Scan和索引扫描Index Range Scan)时,CPU资源的消耗会因操作不同而变化。全表...
**CBO数据库优化详解** 在Oracle数据库管理中,CBO(Cost-Based Optimizer)是一种重要的查询优化策略,它根据操作的成本来决定最佳的执行计划。CBO通过分析表的统计信息,如行数、块数、索引信息等,来评估不同...
### 如何理解CBO在Oracle数据库中的工作原理 本文旨在深入探讨成本基础优化器(Cost-Based Optimizer,简称CBO)在Oracle数据库中的工作方式及其重要性。通过一个具体的示例,我们将逐步分析CBO如何根据表统计信息...
ORACLE CBO RBO 优化
在Oracle数据库中,CBO(Cost-Based Optimizer)和RBO(Rule-Based Optimizer)是两种不同的SQL查询优化策略。CBO自Oracle 7引入,但在Oracle 8i时达到成熟,并在Oracle 9i及以后的版本中逐渐取代RBO成为主要的优化...
### Oracle CBO优化器与直方图(Histogram)解析 #### 概述 在数据库管理领域,Oracle CBO(Cost-Based Optimizer)成本基于优化器是Oracle数据库管理系统中用于确定查询执行计划的一种重要机制。通过分析SQL语句,...
CBO是Oracle自8i版本开始大力推荐的优化策略,它考虑了查询执行的实际成本,包括CPU、I/O和内存的消耗。CBO依赖于表和索引的统计信息来估算执行各种操作(如全表扫描或索引扫描)的代价,然后选择总代价最低的执行...
在Oracle数据库管理中,`dba_indexes`视图是DBA(数据库管理员)用来获取数据库中所有索引详细信息的关键工具。这个视图包含了与索引相关的各种属性,如索引名称、所有者、状态、类型、分区情况、并行度等。在性能...
标题 "UFIDA.U9.CBO.ItemMasterBE.zip" 指的是用友U9系统中的一个插件开发相关的代码示例。用友U9是一款面向大型企业的全面企业管理软件,其核心设计思想是“实时企业,全球商务”。CBO(Client Business Object)是...
在ASP.NET中,CBO通常指的是ComboBox控件,它结合了下拉列表框和文本框的功能,用户可以从中选择一个选项或者手动输入。在某些场景下,我们可能需要实现CBO(ComboBox)的多行选中功能,这在传统的ComboBox中通常是...
Oracle的Cost-Based Optimizer(CBO)是数据库管理系统中用于生成高效执行计划的关键组件。它通过对不同查询路径的成本估计来确定最优的查询执行策略。在本篇内容中,我们将深入探讨Oracle CBO的几种基本查询转换,...
Oracle Cost-Based Optimizer (CBO) 是Oracle数据库中的一种查询优化策略,它通过评估不同查询执行计划的成本来选择最优的执行路径。在这个学习笔记中,我们将深入探讨Oracle中的Hash Join操作,这是一种重要的联接...
### 如何充分利用Oracle的成本基础优化器(CBO) 在Oracle数据库管理系统中,成本基础优化器(Cost-Based Optimizer, CBO)是决定查询执行计划的关键组件。为了确保CBO能够选择最佳的执行策略,准确地收集和维护统计...