索引訪問與表大小的關係
當我們通過索引來訪問表的資料時候,當索引的掃瞄範圍一定時,表的大小對訪問的
效能並沒有太大的影響.
sql> create table t1 as select * from dba_objects;
table created.
sql> select count(*) from t1;
count(*)
sql> create table t2 as select * from dba_objects where object_id<1000;
table created.
sql> select count(*) from t2;
count(*)
sql> create index t1_id on t1(object_id);
index created.
sql> create index t2_id on t2(object_id);
index created.
建立兩個表,並分別在object_id欄位上增加過索引.
sql> exec dbms_stats.gather_table_stats('sys','t1',method_opt=>'for all columns size auto');
pl/sql procedure successfully completed.
sql> exec dbms_stats.gather_table_stats('sys','t2',method_opt=>'for all columns size auto');
pl/sql procedure successfully completed.
sql> select owner,index_name,blevel,leaf_blocks
2 from dba_ind_statistics
3 where table_name in ('t1','t2') and owner='sys'
4 ;
owner index_name blevel leaf_blocks
sys t1_id 1 157
sys t2_id 1 2
sql> select owner,table_name,num_rows,blocks
2 from dba_tables
3 where table_name in ('t1','t2') and owner='sys';
owner table_name num_rows blocks
sys t2 940 12
sys t1 70768 1041
查詢比較大的表
sql> select count(object_name) from t1 where object_id<10;
count(object_name)
execution plan
plan hash value: 863914470
| id | operation | name | rows | bytes | cost (%cpu)| time
| 0 | select statement | | 1 | 30 | 3 (0)| 00:0
0:01 |
| 1 | sort aggregate | | 1 | 30 | |
| 2 | table access by index rowid| t1 | 4 | 120 | 3 (0)| 00:0
0:01 |
|* 3 | index range scan | t1_id | 4 | | 2 (0)| 00:0
0:01 |
predicate information (identified by operation id):
3 - access("object_id"<10)
statistics
164 recursive calls
0 db block gets
24 consistent gets
0 physical reads
0 redo size
532 bytes sent via sql*net to client
519 bytes received via sql*net from client
2 sql*net roundtrips to/from client
4 sorts (memory)
0 sorts (disk)
1 rows processed
查詢小表
sql> select count(object_name) from t2 where object_id<10;
count(object_name)
execution plan
plan hash value: 729095221
| id | operation | name | rows | bytes | cost (%cpu)| time
| 0 | select statement | | 1 | 20 | 3 (0)| 00:0
0:01 |
| 1 | sort aggregate | | 1 | 20 | |
| 2 | table access by index rowid| t2 | 8 | 160 | 3 (0)| 00:0
0:01 |
|* 3 | index range scan | t2_id | 8 | | 2 (0)| 00:0
0:01 |
predicate information (identified by operation id):
3 - access("object_id"<10)
statistics
164 recursive calls
0 db block gets
23 consistent gets
0 physical reads
0 redo size
532 bytes sent via sql*net to client
519 bytes received via sql*net from client
2 sql*net roundtrips to/from client
4 sorts (memory)
0 sorts (disk)
1 rows processed
可以看出查詢小表和查詢大表,consistent gets沒有太的區別,cost比較接近.經常有比如乙個表中
存放180天和30天的資料,而通過索引只查詢前一天的資料,這個表裡是否存放30天資料就比較合理呢.
如果僅從索引訪問的方式來看,其實並沒有影響.
從oracle的index range scan的角度來說,索引的層級對效能影響更加小,當掃瞄索引第乙個值時,
由於索引是已經排好序,可以直接往後面掃瞄,而不需要通過從上到下再次查詢的方式。
Oracle 批處理 索引 自增
oracle 得到許多企業的青睞,在企業中處理大批量匯入到b表裡,使用批處理 批處理使用到的資料庫許可權 oba 許可權 資料庫一般連線 private static object executesql org string ssql,object params catch oraclepar.pa...
mysql普通索引自增 MySQL索引
mysql索引 什麼是索引 索引 index 是幫助mysql高效獲取資料的資料結構,也就是說索引的本質就是資料結構。生活中書本的目錄就可以理解為一種索引。mysql中索引分類聚簇索引 聚集索引 將資料儲存與索引放到了一塊,找到索引也就找到了資料 非聚簇索引 二級索引 將資料儲存於索引分開結構,索引...
資料索引自增
b tree 資料結構索引 從這個資料介面上,細細咀嚼了一下索引自增。如果資料id是自增的情況下,可以看上面圖,資料的整合是很整齊的。前面兩層對應的資料是滿的。但是如果id不是自增的情況,那就不是了。從而導致需要更多前兩層的資料。優點 1.自增,趨勢自增,可作為聚集索引,提公升查詢效率2.節省磁碟空...