oracle聯合索引在CBO下的分析
SQL> set autot on
SQL> desc yibin_test;
名稱 是否為空? 型別
----------------------------------------- -------- ----------------------------
OWNER VARCHAR2(30)
OBJECT_NAME NOT NULL VARCHAR2(128)
SUBOBJECT_NAME VARCHAR2(30)
OBJECT_ID NOT NULL NUMBER
DATA_OBJECT_ID NUMBER
OBJECT_TYPE VARCHAR2(18)
CREATED DATE
LAST_DDL_TIME DATE
TIMESTAMP VARCHAR2(19)
STATUS VARCHAR2(7)
TEMPORARY VARCHAR2(1)
GENERATED VARCHAR2(1)
SECONDARY VARCHAR2(1)
對OBJECT_NAME ,OBJECT_ID 建立主鍵聯合索引,object_name在前
SQL> select index_name,table_name,column_name from user_ind_columns;
INDEX_NAME TABLE_NAME
------------------------------ ------------------------------
COLUMN_NAME
-------------------------------------------------------------------------------
IDX_TEST YIBIN_TEST
OBJECT_ID
IDX_TEST YIBIN_TEST
OBJECT_NAME
並且分析索引
SQL> analyze table object_test compute statistics;
Table analyzed
看下面 兩個欄位都列上時,不管後面欄位擺放順序 都會用上 INDEX (UNIQUE SCAN) 索引,
後面兩個欄位對調位置
SQL> select * from yibin_test where object_id=97 and object_name='access$' ;
未選定行
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT ptimizer=CHOOSE (Cost=2 Card=1 Bytes=76)
1 0 TABLE ACCESS (BY INDEX ROWID) OF 'YIBIN_TEST' (Cost=2 Card
=1 Bytes=76)
2 1 INDEX (UNIQUE SCAN) OF 'IDX_TEST' (UNIQUE) (Cost=1 Card=
1)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
2 consistent gets
1 physical reads
0 redo size
942 bytes sent via SQL*Net to client
372 bytes received via SQL*Net from client
1 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
0 rows processed
沒調換位置
SQL> select * from yibin_test where object_name='access$' and object_id=97
2 ;
未選定行
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT ptimizer=CHOOSE (Cost=2 Card=1 Bytes=76)
1 0 TABLE ACCESS (BY INDEX ROWID) OF 'YIBIN_TEST' (Cost=2 Card
=1 Bytes=76)
2 1 INDEX (UNIQUE SCAN) OF 'IDX_TEST' (UNIQUE) (Cost=1 Card=
1)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
2 consistent gets
0 physical reads
0 redo size
942 bytes sent via SQL*Net to client
372 bytes received via SQL*Net from client
1 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
0 rows processed
SQL>
單獨把object_name列出來 會用上索引範圍掃描
SQL> select * from yibin_test where object_name='access$' ;
未選定行
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT ptimizer=CHOOSE (Cost=3 Card=1 Bytes=76)
1 0 TABLE ACCESS (BY INDEX ROWID) OF 'YIBIN_TEST' (Cost=3 Card
=1 Bytes=76)
2 1 INDEX (RANGE SCAN) OF 'IDX_TEST' (UNIQUE) (Cost=2 Card=1
)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
2 consistent gets
0 physical reads
0 redo size
942 bytes sent via SQL*Net to client
372 bytes received via SQL*Net from client
1 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
0 rows processed
而只用後面這個時,不會用上索引,走全表掃描
SQL> select * from yibin_test where object_id=97 ;
OWNER
------------------------------
OBJECT_NAME
--------------------------------------------------------------------------------
SUBOBJECT_NAME OBJECT_ID DATA_OBJECT_ID OBJECT_TYPE
------------------------------ ---------- -------------- ------------------
CREATED LAST_DDL_T TIMESTAMP STATUS T G S
---------- ---------- ------------------- ------- - - -
SYS
ACCESS$
97 97 TABLE
26-10月-08 26-10月-08 2008-10-26:01:03:06 VALID N N N
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT ptimizer=CHOOSE (Cost=10 Card=1 Bytes=76)
1 0 TABLE ACCESS (FULL) OF 'YIBIN_TEST' (Cost=10 Card=1 Bytes=
76)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
83 consistent gets
0 physical reads
0 redo size
1167 bytes sent via SQL*Net to client
503 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL>
總結:根據上面的分析,得出以下結論
CBO下聯合索引:
要是where 後面聯合索引欄位都列上,不管先後順序都會用上索引
要是where後面只列單獨一個欄位,那只有按照建立索引欄位順序排在最前的會用上索引,後面的不會用上
所以要根據應用來結合,建立合適的索引
經過測試 發現在rbo下也是和上面規律一樣
如果大家對這個結果有問題,請提出來
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7199859/viewspace-533331/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- ORACLE 組合索引 使用分析Oracle索引
- MySQL的聯合索引MySql索引
- 索引在ORACLE中的應用分析索引Oracle
- CBO,RBO在ORACLE中的應用Oracle
- 【Mysql】InnoDB 中的聚簇索引、二級索引、聯合索引MySql索引
- mysql聯合索引的選擇性MySql索引
- 在已存在的表結構上新增主鍵、外來鍵、聯合主鍵、聯合索引的例子索引
- 聯合索引的最左字首匹配原則索引
- SQL優化(二)(聯合索引的使用)SQL優化索引
- MySQL 聯合索引測試3MySql索引
- oracle 索引分析及索引重建Oracle索引
- 【效能優化】CBO,RBO在ORACLE中的應用優化Oracle
- Oracle表與索引的分析及索引重建Oracle索引
- mysql 聯合索引的兩種特殊場景MySql索引
- mysql 建立和刪除聯合索引MySql索引
- CBO_ORACLEOracle
- Oracle索引合併coalesce操作Oracle索引
- Oracle表與索引的分析及索引重建(轉)Oracle索引
- MySQL建立表的時候建立聯合索引的方法MySql索引
- Oracle對索引分析的優化Oracle索引優化
- oracle複合索引介紹(多欄位索引)Oracle索引
- 聯合索引和多個單列索引使用中的索引命中情況及索引建立原則索引
- Oracle中組合索引的使用詳解Oracle索引
- oracle 表分析和索引Oracle索引
- Oracle Optimizer CBO RBOOracle
- Oracle CBO 與 RBOOracle
- Oracle對錶、索引和簇的分析Oracle索引
- ORACLE分析表和索引的指令碼Oracle索引指令碼
- 【效能優化】CBO,RBO在ORACLE 10g 中的應用優化Oracle 10g
- Oracle複合索引的建立和注意事項Oracle索引
- oracle 定期表及索引分析Oracle索引
- treedump研究下oracle索引的結構Oracle索引
- 關於聯合索引,範圍查詢,時間列索引的幾個問題索引
- MySQL 調優之如何正確使用聯合索引MySql索引
- oracle的B-tree索引結構分析Oracle索引
- oracle利用索引關聯獲得資料的方法Oracle索引
- 組合索引的前導列與查詢——ORACLE索引Oracle
- 在oracle中監視索引的使用情況Oracle索引