我们没有收集操作系统统计信息,所以CBO采用了非工作量统计模式(noworkload)来计算代价
LEO1@LEO1> select blocks from user_tables where table_name='LEO1'; LEO1表总数据块为1051
BLOCKS
------------------
1051
LEO1@LEO1> set autotrace trace explain
LEO1@LEO1> select * from leo1;
Execution Plan
----------------------------------------------------------
Plan hash value: 2716644435
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 71968 | 6817K| 233 (1)| 00:00:03 |
| 1 | TABLE ACCESS FULL| LEO1 | 71968 | 6817K| 233 (1)| 00:00:03 |
--------------------------------------------------------------------------
全表扫描的成本等于233,其中CPU代价占整个权重百分比的1%
###################################################################################
成本的计算公式如下:
Cost = (
#SRds * sreadtim +
#MRds * mreadtim +
CPUCycles / cpuspeed
) / sreadtime
#SRds - number of single block reads 单块读的次数
#MRds - number of multi block reads 多块读的次数
#CPUCyles - number of CPU cycles 一个CPU周期
sreadtim - single block read time 读取单个数据块的平均时间
mreadtim - multi block read time 读取多个数据块的平均时间
cpuspeed - CPU cycles per second CPU周期/秒
注意:如果Oracle收集了操作系统统计信息,那么CBO采用工作量统计模式计算代价
如果oracle没有收集操作系统统计信息,那么CBO采用非工作量统计模式计算代价我们现在处于“非工作量统计模式”
#SRds=0,因为是全表扫描,单块读为0,全都使用的是多块读
#MRds=表的块数/多块读参数=1051/16=65.6875
mreadtim=ioseektim+db_file_multiblock_count*db_block_size/iotftspeed=10+16*8192/4096=42
sreadtim=ioseektim+db_block_size/iotfrspeed=10+8192/4096=12
CPUCycles等于PLAN_TABLE里面的CPU_COST
LEO1@LEO1> explain plan for select * from leo1;
Explained.
LEO1@LEO1> select cpu_cost from plan_table;
CPU_COST
-----------------
38430873
cpuspeed等于CPUSPEEDNW= 2657.0122
COST=65.6875*42/12+38430873/2657.0122/12/1000(毫秒换算成秒)=229.90625+1.20532=231.11157
229.90625是IO代价
1.20532 是CPU代价
手工计算出来的COST用四舍五入等于232,和我们看到的233有差别,这是由于隐含参数_table_scan_cost_plus_one参数造成的
LEO1@LEO1> conn / as sysdba 切换到sys用户才能查看隐含参数
SYS@LEO1> SELECT x.ksppinm NAME, y.ksppstvl VALUE, x.ksppdesc describ FROM x$ksppi x,x$ksppcv y
WHERE x.inst_id = USERENV ('Instance')
AND y.inst_id = USERENV ('Instance')
AND x.indx = y.indx
AND x.ksppinm LIKE '%_table_scan_cost_plus_one%'; 2 3 4 5
NAME VALUE DESCRIB
----------------------------------------------------------------------------------------------------------------------------------------------
_table_scan_cost_plus_one TRUE bump estimated full table scan and index ffs cost by one
根据该参数的描述,在table full scan和index fast full scan的时候会将cost+1即232+1=233
我们把_table_scan_cost_plus_one参数禁用看看cost变化
SYS@LEO1> alter session set "_table_scan_cost_plus_one"=false; 禁用
Session altered.
SYS@LEO1> SELECT x.ksppinm NAME, y.ksppstvl VALUE, x.ksppdesc describ FROM x$ksppi x,x$ksppcv y
WHERE x.inst_id = USERENV ('Instance')
AND y.inst_id = USERENV ('Instance')
AND x.indx = y.indx
AND x.ksppinm LIKE '%_table_scan_cost_plus_one%'; 2 3 4 5 生效
NAME VALUE DESCRIB
----------------------------------------------------------------------------------------------------------------------------------------------
_table_scan_cost_plus_one FALSE bump estimated full table scan and index ffs cost by one
SYS@LEO1> select * from leo1.leo1;
Execution Plan
----------------------------------------------------------
Plan hash value: 2716644435
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 71968 | 6817K| 232 (1)| 00:00:03 |
| 1 | TABLE ACCESS FULL| LEO1 | 71968 | 6817K| 232 (1)| 00:00:03 |
--------------------------------------------------------------------------
