[20230425]CBO cost與行遷移關係.txt
[20230425]CBO cost與行遷移關係.txt
--//一般現在很少使用analyze table分析表,如果出現大量行遷移是否考慮看看是否考慮cbo cost成本.
--//測試參考連結:
--//
--//
1.環境:
SCOTT@book> @ver1
PORT_STRING VERSION BANNER
------------------- ---------- ----------------------------------------------------------------------------
x86_64/Linux 2.4.xx 11.2.0.4.0 Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
2.測試建立:
SCOTT@book> create table t1 pctfree 0 as select rownum id ,cast (null as varchar2(10)) vc from dual connect by level<=1e5;
Table created.
SCOTT@book> create index i_t1_id on t1(id);
Index created.
SCOTT@book> @ gts t1 '' ''
...
Gather Table Statistics for table t1...
exec dbms_stats.gather_table_stats('SCOTT', 'T1', estimate_percent => NULL, method_opt=>'FOR TABLE FOR ALL COLUMNS SIZE REPEAT', cascade=>true, no_invalidate=>false)
if lock table t1, add force=>true.
press ctrl+c cancel, enter continue...
PL/SQL procedure successfully completed.
$ cat v_cnt.txt
SELECT table_name
, num_rows
, blocks
, empty_blocks
, avg_space
, avg_row_len
, chain_cnt
FROM user_tables
WHERE table_name = 'T1';
select index_name, blevel, leaf_blocks, clustering_factor from user_indexes where table_name='T1';
$
SCOTT@book> @ v_cnt.txt
TABLE_NAME NUM_ROWS BLOCKS EMPTY_BLOCKS AVG_SPACE AVG_ROW_LEN CHAIN_CNT
---------- ---------- ---------- ------------ ---------- ----------- ----------
T1 100000 149 0 0 5 0
INDEX_NAME BLEVEL LEAF_BLOCKS CLUSTERING_FACTOR
---------- ------ ----------- -----------------
I_T1_ID 1 222 137
3.測試:
SCOTT@book> @ sl all
alter session set statistics_level = all;
Session altered.
SCOTT@book> select count(distinct vc) from t1 where id > 1 and id < 1001;
COUNT(DISTINCTVC)
-----------------
0
SCOTT@book> @ dpc '' ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID f985dd35g9kav, child number 0
-------------------------------------
select count(distinct vc) from t1 where id > 1 and id < 1001
Plan hash value: 1993888300
---------------------------------------------------------------------------------------------------------------------------------------
|Id|Operation |Name |Starts|E-Rows|E-Bytes|Cost(%CPU)|E-Time |A-Rows| A-Time |Buffers|OMem |1Mem |Used-Mem|
---------------------------------------------------------------------------------------------------------------------------------------
| 0|SELECT STATEMENT | | 1| | | 7 (100)| | 1|00:00:00.01| 6| | | |
| 1| SORT AGGREGATE | | 1| 1| 7 | | | 1|00:00:00.01| 6| | | |
| 2| VIEW |VW_DAG_0| 1| 1| 7 | 7 (15)|00:00:01| 1|00:00:00.01| 6| | | |
| 3| HASH GROUP BY | | 1| 1| 5 | 7 (15)|00:00:01| 1|00:00:00.01| 6|2834K|2834K| 748K(0)|
| 4| TABLE ACCESS BY INDEX ROWID|T1 | 1| 999| 4995 | 6 (0)|00:00:01| 999|00:00:00.01| 6| | | |
|*5| INDEX RANGE SCAN |I_T1_ID | 1| 999| | 4 (0)|00:00:01| 999|00:00:00.01| 4| | | |
---------------------------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$C33C846D
2 - SEL$5771D262 / VW_DAG_0@SEL$C33C846D
3 - SEL$5771D262
4 - SEL$5771D262 / T1@SEL$1
5 - SEL$5771D262 / T1@SEL$1
Predicate Information (identified by operation id):
---------------------------------------------------
5 - access("ID">1 AND "ID"<1001)
--//簡單說明cbo cost的計算:
Selectivity = (Highest Bound Value – Lowest Bound Value) / (Highest Value – Lowest Value)
--//Selectivity=(1001-1)/(100000-1) = .01000010000100001
Index Scan Cost = (blevel + ceil(effective index selectivity x leaf_blocks)) + ceil(effective table selectivity x clustering_factor)
--//1+celi(0.01*222)+ceil(0.01*137)
--//1+3+2=6
--//cost=6 ,感覺原始連結少算1個.
SCOTT@book> update t1 set vc=to_char(rownum)||lpad('a',4,'a') ;
100000 rows updated.
SCOTT@book> commit ;
Commit complete.
SCOTT@book> @ gts t1 '' ''
...
Gather Table Statistics for table t1...
exec dbms_stats.gather_table_stats('SCOTT', 'T1', estimate_percent => NULL, method_opt=>'FOR TABLE FOR ALL COLUMNS SIZE REPEAT', cascade=>true, no_invalidate=>false)
if lock table t1, add force=>true.
press ctrl+c cancel, enter continue...
PL/SQL procedure successfully completed.
SCOTT@book> @ v_cnt.txt
TABLE_NAME NUM_ROWS BLOCKS EMPTY_BLOCKS AVG_SPACE AVG_ROW_LEN CHAIN_CNT
---------- ---------- ---------- ------------ ---------- ----------- ----------
T1 100000 886 0 0 15 0
INDEX_NAME BLEVEL LEAF_BLOCKS CLUSTERING_FACTOR
---------- ------ ----------- -----------------
I_T1_ID 1 222 137
--//分析表後索引統計沒有變化.而表因為發生了行遷移,佔用塊數量從149=>886.
SCOTT@book> select count(distinct vc) from t1 where id > 1 and id < 1001;
COUNT(DISTINCTVC)
-----------------
999
SCOTT@book> @ dpc '' ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID f985dd35g9kav, child number 0
-------------------------------------
select count(distinct vc) from t1 where id > 1 and id < 1001
Plan hash value: 1993888300
---------------------------------------------------------------------------------------------------------------------------------------
|Id|Operation |Name |Starts|E-Rows|E-Bytes|Cost(%CPU)|E-Time |A-Rows| A-Time |Buffers|OMem |1Mem |Used-Mem|
---------------------------------------------------------------------------------------------------------------------------------------
| 0|SELECT STATEMENT | | 1| | | 7 (100)| | 1|00:00:00.01| 1992| | | |
| 1| SORT AGGREGATE | | 1| 1| 7 | | | 1|00:00:00.01| 1992| | | |
| 2| VIEW |VW_DAG_0| 1| 999| 6993 | 7 (15)|00:00:01| 999|00:00:00.01| 1992| | | |
| 3| HASH GROUP BY | | 1| 999| 14985 | 7 (15)|00:00:01| 999|00:00:00.01| 1992|1818K|1818K|1346K(0)|
| 4| TABLE ACCESS BY INDEX ROWID|T1 | 1| 999| 14985 | 6 (0)|00:00:01| 999|00:00:00.01| 1992| | | |
|*5| INDEX RANGE SCAN |I_T1_ID | 1| 999| | 4 (0)|00:00:01| 999|00:00:00.01| 4| | | |
---------------------------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$C33C846D
2 - SEL$5771D262 / VW_DAG_0@SEL$C33C846D
3 - SEL$5771D262
4 - SEL$5771D262 / T1@SEL$1
5 - SEL$5771D262 / T1@SEL$1
Predicate Information (identified by operation id):
---------------------------------------------------
5 - access("ID">1 AND "ID"<1001)
--//cost=7,與前面沒有變化.
SCOTT@book> analyze table t1 compute statistics;
Table analyzed.
SCOTT@book> @ v_cnt.txt
TABLE_NAME NUM_ROWS BLOCKS EMPTY_BLOCKS AVG_SPACE AVG_ROW_LEN CHAIN_CNT
---------- ---------- ---------- ------------ ---------- ----------- ----------
T1 100000 886 10 425 24 99835
INDEX_NAME BLEVEL LEAF_BLOCKS CLUSTERING_FACTOR
---------- ------ ----------- -----------------
I_T1_ID 1 222 137
--//使用analyze分析後,CHAIN_CNT=99835
SCOTT@book> select /*+index(t1 i_t1_id) */ count(distinct vc) from t1 where id > 1 and id < 1001;
COUNT(DISTINCTVC)
-----------------
999
--//注:必須加入提示/*+index(t1 i_t1_id) */,不然執行計劃選擇全表掃描.
SCOTT@book> @ dpc '' ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID ahtcmzznvj0c9, child number 0
-------------------------------------
select /*+index(t1 i_t1_id) */ count(distinct vc) from t1 where id > 1
and id < 1001
Plan hash value: 1993888300
---------------------------------------------------------------------------------------------------------------------------------------
|Id|Operation |Name |Starts|E-Rows|E-Bytes|Cost(%CPU)|E-Time |A-Rows| A-Time |Buffers|OMem |1Mem |Used-Mem|
---------------------------------------------------------------------------------------------------------------------------------------
| 0|SELECT STATEMENT | | 1| | |1006 (100)| | 1|00:00:00.01| 1992| | | |
| 1| SORT AGGREGATE | | 1| 1| 7 | | | 1|00:00:00.01| 1992| | | |
| 2| VIEW |VW_DAG_0| 1| 999| 6993 |1006 (1)|00:00:13| 999|00:00:00.01| 1992| | | |
| 3| HASH GROUP BY | | 1| 999| 12987 |1006 (1)|00:00:13| 999|00:00:00.01| 1992|1818K|1818K|1362K(0)|
| 4| TABLE ACCESS BY INDEX ROWID|T1 | 1| 999| 12987 |1005 (1)|00:00:13| 999|00:00:00.01| 1992| | | |
|*5| INDEX RANGE SCAN |I_T1_ID | 1| 999| | 4 (0)|00:00:01| 999|00:00:00.01| 4| | | |
---------------------------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$C33C846D
2 - SEL$5771D262 / VW_DAG_0@SEL$C33C846D
3 - SEL$5771D262
4 - SEL$5771D262 / T1@SEL$1
5 - SEL$5771D262 / T1@SEL$1
Predicate Information (identified by operation id):
---------------------------------------------------
5 - access("ID">1 AND "ID"<1001)
--//現在cost=1006.
SCOTT@book> @ v_cnt.txt
TABLE_NAME NUM_ROWS BLOCKS EMPTY_BLOCKS AVG_SPACE AVG_ROW_LEN CHAIN_CNT
---------- ---------- ---------- ------------ ---------- ----------- ----------
T1 100000 886 10 425 24 99835
INDEX_NAME BLEVEL LEAF_BLOCKS CLUSTERING_FACTOR
---------- ------ ----------- -----------------
I_T1_ID 1 222 137
Index Scan Cost = blevel +
ceil(effective index selectivity x leaf_blocks) +
ceil(effective table selectivity x clustering_factor) +
ceil(effective table selectivity x chain_cnt)
--//1+ceil(0.01*222)+ceil(0.01*137)+ceil(0.01*99835)
--//1+3+2+999 = 1005
--//cost=1005 ,基本接近1006.感覺原始連結少算1個.也許是一些oracle版本細節上的一些區別.
4.總結:
--//可以看出oracle選擇索引範圍查詢的cost計算會考慮行遷移的情況,不過這個給使用analyze分析,現在已經不用了.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-2949124/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- [20180327]行遷移與ITL浪費.txt
- [20190120]行連結遷移與dml.txt
- [20180402]行連結行遷移與ITL槽6.txt
- 深入淺出Calcite與SQL CBO(Cost-Based Optimizer)優化SQL優化
- [20230425]注意snapshot standby與activate standby的區別.txt
- EntityFramework Core 遷移忽略主外來鍵關係Framework
- [20221111]CBO and Partial indexing.txtIndex
- 世界銀行報告:全球遷移人口增長10%與缺水有關
- Oracle 行遷移 & 行連結的檢測與消除Oracle
- 遷移執行緒migration執行緒
- Oracle資料庫中資料行遷移與行連結Oracle資料庫
- [20200824]12c sqlplus rowprefetch arraysize 顯示行數量的關係.txtSQL
- PV 與 PVC 狀態遷移
- Cookie與Session 關係CookieSession
- Hadoop與Spark關係HadoopSpark
- 移動端與H5頁面畫素的差異與關係H5
- 模擬Oracle行遷移和行連結Oracle
- 光流與視訊行為識別的關係
- 關於禪道的資料遷移
- 關於 Laravel 遷移遇到的問題Laravel
- 傳統 Web 框架部署與遷移Web框架
- GNU與Linux 的關係Linux
- equals與hashCode關係梳理
- Scala與Java的關係Java
- Object與Class的關係Object
- Process與Socket,Select與Accept關係
- 企業辦公平臺與移動辦公工具之間的關係
- SQL與NoSQL(關係型與非關係型)資料庫的區別SQL資料庫
- Jenkins搭建與資料遷移實踐Jenkins
- Docker與containerd的關係與區別DockerAI
- Excutors 與 ThreadPoolExcutor 的關係與區別thread
- 圖片與向量的關係
- JSON 與 JS 物件的關係JSON物件
- TLS與SSL之間關係TLS
- 棧遷移
- 遷移公告
- EF Core 小技巧:遷移已經應用到資料庫,如何進行遷移回退操作?資料庫
- 使用Conda Pack進行環境打包遷移