B-Tree Index 成本計算
如果執行計劃採用索引,其成本計算公式與tablescan成本計算公式是不同的,具體實驗如下:
1、建立測試表
SQL>execute dbms_random.seed(0)
SQL>create table t1 as
select
trunc(dbms_random.value(0,25)) n1,
rpad('x', 40) ind_pad,
trunc(dbms_random.value(0,20)) n2,
lpad(rownum, 10, '0') small_vc,
rpad('x',200) padding
from
all_objects
where rownum<=10000;
SQL>create index t1_i1 on t1(n1, ind_pad, n2)
pctfree 91;
2、統計分析
SQL>begin
dbms_stats.gather_table_stats (
user,
't1',
cascade => true,
estimate_percent => null,
method_opt => 'for all columns size 1'
);
end;
3、生成跟蹤檔案
SQL>alter session set events '10053 trace name context forever, level 2';
SQL>select small_vc from t1
where n1=2
and ind_pad =rpad('x',40)
and n2 = 3;
SQL>alter session set events '10053 trace name context off';
提取關鍵指標值:
BLEVEL=LVLS: 2
LEAF_BLOCKS=#LB=1111
effective index selectivity= ix_sel: 0.002 = (1/#DK: 500)
clustering_factor=CLUF: 9745.00
effective table selectivity= ix_sel_with_filters: 0.002
=(1/N1.density:0.04)*(1/IND_PAD.density:1)*(1/N2.density:0.05)
或檢視檢視:
SQL>select table_name, blocks, num_rows
from user_tables
where table_name='T1';
TABLE_NAME BLOCKS NUM_ROWS
------------------------------ ---------- ----------
T1 387 10000
SQL>select num_rows, distinct_keys,
blevel, leaf_blocks, clustering_factor,
avg_leaf_blocks_per_key, avg_data_blocks_per_key
from user_indexes
where table_name='T1'
and index_name='T1_I1';
NUM_ROWS DISTINCT_KEYS BLEVEL LEAF_BLOCKS CLUSTERING_FACTOR AVG_LEAF_BLOCKS_PER_KEY AVG_DATA_BLOCKS_PER_KEY
---------- ------------- ---------- ----------- ----------------- ----------------------- -----------------------
10000 500 2 1111 9745 2 19
SQL>select column_name,
num_nulls, num_distinct, density
from user_tab_columns
where table_name='T1'
and column_name in ('N1', 'N2', 'IND_PAD')
order by column_name;
COLUMN_NAME NUM_NULLS NUM_DISTINCT DENSITY
------------------------------ ---------- ------------ ----------
IND_PAD 0 1 1
N1 0 25 .04
N2 0 20 .05
4、手工成本計算值與實際成本計算值比較
cost = blevel +
ceiling(leaf_blocks * effective index selectivity) +
ceiling(clustering_factor * effective table selectivity)
= 2 +
ceiling(1111*0.002) +
ceiling(9745*0.002)
=2+3+20=25
實際執行計劃如下:
-----------------------------------------------+-----------------------------------+
| Id | Operation | Name | Rows | Bytes | Cost | Time |
-----------------------------------------------+-----------------------------------+
| 0 | SELECT STATEMENT | | | | 25 | |
| 1 | TABLE ACCESS BY INDEX ROWID | T1 | 20 | 1160 | 25 | 00:00:01 |
| 2 | INDEX RANGE SCAN | T1_I1 | 20 | | 5 | 00:00:01 |
-----------------------------------------------+-----------------------------------+
結論:兩者值完全相同
備註:實驗依據和公式來源於jonathan lewis 《CBO基礎》
1、建立測試表
SQL>execute dbms_random.seed(0)
SQL>create table t1 as
select
trunc(dbms_random.value(0,25)) n1,
rpad('x', 40) ind_pad,
trunc(dbms_random.value(0,20)) n2,
lpad(rownum, 10, '0') small_vc,
rpad('x',200) padding
from
all_objects
where rownum<=10000;
SQL>create index t1_i1 on t1(n1, ind_pad, n2)
pctfree 91;
2、統計分析
SQL>begin
dbms_stats.gather_table_stats (
user,
't1',
cascade => true,
estimate_percent => null,
method_opt => 'for all columns size 1'
);
end;
3、生成跟蹤檔案
SQL>alter session set events '10053 trace name context forever, level 2';
SQL>select small_vc from t1
where n1=2
and ind_pad =rpad('x',40)
and n2 = 3;
SQL>alter session set events '10053 trace name context off';
提取關鍵指標值:
BLEVEL=LVLS: 2
LEAF_BLOCKS=#LB=1111
effective index selectivity= ix_sel: 0.002 = (1/#DK: 500)
clustering_factor=CLUF: 9745.00
effective table selectivity= ix_sel_with_filters: 0.002
=(1/N1.density:0.04)*(1/IND_PAD.density:1)*(1/N2.density:0.05)
或檢視檢視:
SQL>select table_name, blocks, num_rows
from user_tables
where table_name='T1';
TABLE_NAME BLOCKS NUM_ROWS
------------------------------ ---------- ----------
T1 387 10000
SQL>select num_rows, distinct_keys,
blevel, leaf_blocks, clustering_factor,
avg_leaf_blocks_per_key, avg_data_blocks_per_key
from user_indexes
where table_name='T1'
and index_name='T1_I1';
NUM_ROWS DISTINCT_KEYS BLEVEL LEAF_BLOCKS CLUSTERING_FACTOR AVG_LEAF_BLOCKS_PER_KEY AVG_DATA_BLOCKS_PER_KEY
---------- ------------- ---------- ----------- ----------------- ----------------------- -----------------------
10000 500 2 1111 9745 2 19
SQL>select column_name,
num_nulls, num_distinct, density
from user_tab_columns
where table_name='T1'
and column_name in ('N1', 'N2', 'IND_PAD')
order by column_name;
COLUMN_NAME NUM_NULLS NUM_DISTINCT DENSITY
------------------------------ ---------- ------------ ----------
IND_PAD 0 1 1
N1 0 25 .04
N2 0 20 .05
4、手工成本計算值與實際成本計算值比較
cost = blevel +
ceiling(leaf_blocks * effective index selectivity) +
ceiling(clustering_factor * effective table selectivity)
= 2 +
ceiling(1111*0.002) +
ceiling(9745*0.002)
=2+3+20=25
實際執行計劃如下:
-----------------------------------------------+-----------------------------------+
| Id | Operation | Name | Rows | Bytes | Cost | Time |
-----------------------------------------------+-----------------------------------+
| 0 | SELECT STATEMENT | | | | 25 | |
| 1 | TABLE ACCESS BY INDEX ROWID | T1 | 20 | 1160 | 25 | 00:00:01 |
| 2 | INDEX RANGE SCAN | T1_I1 | 20 | | 5 | 00:00:01 |
-----------------------------------------------+-----------------------------------+
結論:兩者值完全相同
備註:實驗依據和公式來源於jonathan lewis 《CBO基礎》
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/354732/viewspace-613607/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- OPTIMIZER_INDEX_COST_ADJ與成本計算Index
- B-tree and Bitmap IndexIndex
- 【轉】OPTIMIZER_INDEX_COST_ADJ與成本計算Index
- oracle實驗記錄(INDEX fast full scan 的成本計算)OracleIndexAST
- 平衡樹索引(b-tree index)索引Index
- 標準成本計算和平均成本計算比較
- Oracle標準成本計算和平均成本計算比較Oracle
- Oracle 成本計算公式Oracle公式
- CBO成本計算初探
- 淺析oracle b-tree index搜尋原理OracleIndex
- NESTED LOOPS 成本計算OOP
- 產品成本計算方法
- PostgreSQL DBA(139) - PG 12(B-tree index improvement 1#)SQLIndex
- 雲端計算降低辦公成本
- 專案成本管理計算公式公式
- What is the difference between Mysql InnoDB B+ tree index and hash index? Why does MongoDB use B-tree?MySqlIndexMongoDB
- 雲端計算如何避免隱性成本
- 質量成本如何計算(轉載)
- Index Range Scan成本與10053Index
- CBO各種型別成本計算公式型別公式
- 有關工序成本的計算邏輯
- CBO Cost Formulas基於成本優化器的成本計算公式大全ORM優化公式
- oracle基於cbo成本計算方式說明Oracle
- 索引成本計算的基礎知識索引
- [zt] CBO在查詢中如何計算成本
- Index Range Scan成本 Histogram 和 10053IndexHistogram
- B-tree Indexes on UROWID Columns for Heap- and Index-Organized Tables (235)IndexZed
- 抖音小程式開發成本是如何計算的?
- 恆訊科技分析:雲端計算的成本貴嗎?
- B-tree
- 【Bitmap Index】B-Tree索引與Bitmap點陣圖索引的鎖代價比較研究Index索引
- 智慧景區無線AP覆蓋方案的成本計算
- [20190821]關於CPU成本計算.txt
- CO聯產品成本差異分攤計算邏輯
- SQL Server三大演算法的I/O成本計算SQLServer演算法
- 分享Epicor開發的月加權計算成本的案例
- 雲端計算技術降低了IT成本,但是降低的幅度會多大?
- 北鯤雲超算平臺——致力於提高科研效率,降低計算成本的雲超算平臺