[20190823]关于CPU成本计算3.txt

[20190823]关于CPU成本计算3.txt

--//前几天探究CPU cost时遇到的问题,获取行成本时我的测试查询结果出现跳跃,不知道为什么,感觉有点奇怪,分析看看。
--//ITPUB原始链接已经不存在,我的日记本还有记录,现在想想当时的记录思路很乱,不过这些都是猜测的过程,以前思路混乱也是正常的。
--//顺便做一些必要补充唯一索引的相关CPU COST。

1.环境:
SCOTT@test01p> @ ver1
PORT_STRING          VERSION    BANNER                                                                       CON_ID
-------------------- ---------- ---------------------------------------------------------------------------- ------
IBMPC/WIN_NT64-9.1.0 12.2.0.1.0 Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production      0

2.测试:
SCOTT@test01p> create table t as select rownum a1 , rownum a2 ,rownum a3 from dual connect by level<=100 ;
Table created.

create unique index i_t_a1 on t(a1);
create unique index i_t_a2 on t(a2);
create unique index i_t_a3 on t(a3);
--//分析略.

column STATEMENT_ID format a10
column OPTIONS format a20
explain plan set statement_id='a1' for select a1 from t where a1=100;
explain plan set statement_id='a2' for select a2 from t where a2=100;
explain plan set statement_id='a3' for select a3 from t where a3=100;

SCOTT@test01p> select STATEMENT_ID,OPERATION, OPTIONS, COST, CPU_COST, IO_COST, TIME from plan_table where options='UNIQUE SCAN';
STATEMENT_ OPERATION OPTIONS     COST CPU_COST IO_COST TIME
---------- --------- ----------- ---- -------- ------- ----
a1         INDEX     UNIQUE SCAN    0     1050       0    1
a2         INDEX     UNIQUE SCAN    0     1050       0    1
a3         INDEX     UNIQUE SCAN    0     1050       0    1
--//通过唯一索引访问cpu cost=1050.这里面包含行成本150,比较50。
--//通过hack看看变化.

SCOTT@test01p> select BLEVEL,LEAF_BLOCKS from user_indexes where index_name = 'I_T_A3';
BLEVEL LEAF_BLOCKS
------ -----------
     0           1

SCOTT@test01p> exec dbms_stats.set_index_stats(ownname=> NULL,indname=>'I_T_A3',indlevel=> 1000);
PL/SQL procedure successfully completed.

SCOTT@test01p> select BLEVEL,LEAF_BLOCKS from user_indexes where index_name = 'I_T_A3';
    BLEVEL LEAF_BLOCKS
---------- -----------
       255           1

--//注意:我修改indlevel=> 1000,实际上最大仅仅255.也就是最大blevel=255.
--//注:我记忆blevel最大64.如果hack blevel最好不要超过6X.不然在11g下会直接导致使用该索引的sql语句挂起!!切记!!
SCOTT@test01p> exec dbms_stats.set_index_stats(ownname=> NULL,indname=>'I_T_A3',indlevel=> 1000);
PL/SQL procedure successfully completed.

SCOTT@test01p> alter system flush shared_pool;
System altered.

column STATEMENT_ID format a10
column OPTIONS format a20

SCOTT@test01p> explain plan set statement_id='x3' for select /*+ index(t) */ a3 from t where a3=100;
Explained.

SCOTT@test01p> select STATEMENT_ID,OPERATION, OPTIONS, COST, CPU_COST, IO_COST, TIME from plan_table where STATEMENT_ID='x3';
STATEMENT_ OPERATION            OPTIONS                    COST   CPU_COST    IO_COST       TIME
---------- -------------------- -------------------- ---------- ---------- ---------- ----------
x3         SELECT STATEMENT                                   2    1817017          2          1
x3         INDEX                UNIQUE SCAN                 255    1817017        255          1
--//访问索引根节点,叶子节点占255块.
7121.44*255 = 1815967.20
1817017- 1815967.20 = 1049.80

--//修改blevel=254看看.
SCOTT@test01p> exec dbms_stats.set_index_stats(ownname=> NULL,indname=>'I_T_A3',indlevel=> 1000);
PL/SQL procedure successfully completed.

SCOTT@test01p> alter system flush shared_pool;
System altered.

SCOTT@test01p> explain plan set statement_id='x3' for select /*+ index(t) */ a3 from t where a3=100;
Explained.

SCOTT@test01p> select STATEMENT_ID,OPERATION, OPTIONS, COST, CPU_COST, IO_COST, TIME from plan_table where STATEMENT_ID='x3';
STATEMENT_ OPERATION            OPTIONS                    COST   CPU_COST    IO_COST       TIME
---------- -------------------- -------------------- ---------- ---------- ---------- ----------
x3         SELECT STATEMENT                                   2    1809896          2          1
x3         INDEX                UNIQUE SCAN                 254    1809896        254          1
--//访问索引根节点,叶子节点占255块.
7121.44*254 = 1808845.76
1809896 - 1808845.76 = 1050.24
7121.44*254+1050 = 1809895.76
--//无法通过HACK blevel测试.估计就是1050.
--//这样扣除行成本150,比较成本50. UNIQUE SCAN block 成本在=850.

5.看看索引范围的情况呢?
--//重新分析表T!!

column STATEMENT_ID format a10
column OPTIONS format a10
explain plan set statement_id='x100' for select /*+ index(t) */ 1 from t where a3>=100;
explain plan set statement_id='x050' for select /*+ index(t) */ 1 from t where a3>=050;

SCOTT@test01p> select STATEMENT_ID,OPERATION, OPTIONS, COST, CPU_COST, IO_COST, TIME from plan_table ;
STATEMENT_ OPERATION            OPTIONS                    COST   CPU_COST    IO_COST       TIME
---------- -------------------- -------------------- ---------- ---------- ---------- ----------
x100       SELECT STATEMENT                                   1       7321          1          1
x100       INDEX                RANGE SCAN                    1       7321          1          1
x050       SELECT STATEMENT                                   1      17521          1          1
x050       INDEX                RANGE SCAN                    1      17521          1          1

--//看看STATEMENT_ID=050的情况.
SCOTT@test01p> select BLEVEL,LEAF_BLOCKS from user_indexes where index_name = 'I_T_A3';
BLEVEL LEAF_BLOCKS
------ -----------
     0           1
--//Blevel=0 , LEAF_BLOCKS=1
--//块1块 7121.44 行1行 150, 比较 1次 50
--//索引选择率 = (100-50)/99+1/100 = 0.51505050505050505050,也就是card=52.
--//访问块  0.5150*1 = .5150 = 1 ,块成本= 1*7121.44= 7121
--//(行成本+比较成本)*card = 200*52 = 10400
--//10400+7121.44 = 17521.44

--//仅仅大致确定UNIQUE SCAN 块成本是1050.

posted @ 2019-08-24 20:17  lfree  阅读(212)  评论(0编辑  收藏  举报