一. 概念
(1) 类似书的目录结构,可以提高数据检索的速度
(2) 索引直接指向包含所查询值的行的位置,减少磁盘I/O
(3) 索引与表在物理上独立,Oracle 自动使用并维护索引,插入、删除、更新表后,自动更新索引
(4) 建立索引时,要考虑好索引的存储开销与性能上的获益之间的比重,不要盲目建立索引
二.分类
1.唯一索引
当建立Primary Key(主键)或者Unique constraint(唯一约束)时,唯一索引将被自动建立
唯一索引可为空,但不为空时,值需都不相同。
语法:CREATE UNIQUE INDEX index_name ON table (column);
2.组合索引
当两个或多个列经常一起出现在where条件中时,则在这些列上创建组合索引
(1)RBO 下 where 子句为非前导列
rbo下where为非前导列时,执行计划不走索引。
SQL> explain plan for select /*+rule*/* from t1 where c2=1;
Explained
SQL> select * from table (dbms_xplan.display);
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Plan hash value: 3617692013
----------------------------------
| Id | Operation | Name |
----------------------------------
| 0 | SELECT STATEMENT | |
|* 1 | TABLE ACCESS FULL| T1 |
----------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("C2"=1)
Note
-----
- rule based optimizer used (consider using cbo)
17 rows selected
(2)CBO 下 where 子句为非前导列
从9i起,oracle在cbo下引入了索引跳跃扫描(index skip scan),即使SQL语句的where子句中没有组合索引的前导列,也能使用该索引。
SQL> explain plan for select * from t1 where c2=2;
Explained
SQL> select * from table (dbms_xplan.display);
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Plan hash value: 3617692013
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------