Oracle的索引
索引和對應的表應該位於不同的資料表空間中,oracle能夠並行讀取位於不同硬碟上的資料,可以避免產生I/O衝突
B樹索引:在B樹的分葉節點中儲存索引欄位的值與ROWID。
唯一索引和不唯一索引都只是針對B樹索引而言.
Oracle最多允許包含32個欄位的複合索引
索引建立策略
1.匯入資料後再建立索引
2.不需要為很小的表建立索引
3.對於取值範圍很小的欄位(比如性別欄位)應當建立位元影像索引
4.限制表中的索引的數目
5.為索引設定合適的PCTFREE值
6.儲存索引的資料表空間最好單獨設定
建立不唯一索引 Sql代碼
- create index emp_ename on employees(ename)
- tablespace users
- storage(......)
- pctfree 0;
create index emp_ename on employees(ename) tablespace users storage(......) pctfree 0;
建立唯一索引 Sql代碼
- create unique index emp_email on employees(email)
- tablespace users;
create unique index emp_email on employees(email) tablespace users;
建立位元影像索引 Sql代碼
- create bitmap index emp_sex on employees(sex)
- tablespace users;
create bitmap index emp_sex on employees(sex) tablespace users;
建立反序索引 Sql代碼
- create unique index order_reinx on orders(order_num,order_date)
- tablespace users
- reverse;
create unique index order_reinx on orders(order_num,order_date) tablespace users reverse;
建立函數索引(函數索引即可以是普通的B樹索引,也可以是位元影像索引) Sql代碼
- create index emp_substr_empno
- on employees(substr(empno,1,2))
- tablespace users;
create index emp_substr_empno on employees(substr(empno,1,2)) tablespace users;
修改索引儲存參數(與表類似,INITIAL和MINEXTENTS參數在索引建立以後不能再改變) Sql代碼
- alter index emp_ename storage(pctincrease 50);
alter index emp_ename storage(pctincrease 50);
由於定義約束時由oracle自動建立的索引通常是不知道名稱的,對這類索引的修改經常是利用alter table ..using index語句進行的,而不是alter index語句
利用下面的語句將employees表中primary key約束對應的索引的PCTFREE參數修改為5 Sql代碼
- alter table employees enable primary key using index pctfree 5;
alter table employees enable primary key using index pctfree 5;
清理索引片段
1.合并索引(只是簡單的將B樹葉結點中的儲存片段合并在一起,並不會改變索引的物理組織圖) Sql代碼
- alter index emp_pk coalesce;
alter index emp_pk coalesce;
2.重建索引(不僅能夠消除儲存片段,還可以改變索引的全部儲存參數設定,並且可以將索引移動到其它的資料表空間中,重建索引
實際上就是再指定的資料表空間中重建立立一個新的索引,然後刪除原來的索引) Sql代碼
- alter index emp_pk rebuild;
alter index emp_pk rebuild;
刪除索引 Sql代碼
- drop index emp_ename;
drop index emp_ename;
如果索引中包含損壞的資料區塊,或者包含過多的儲存片段,需要首先刪除這個索引,然後再重建它.
如果索引是在建立約束時由oracle自動產生的,可以通過禁用約束或刪除約束的方法來刪除對應的索引.
在刪除一個表時,oracle會自動刪除所有與該表相關的索引.
索引資料字典
all_indexes/dba_indexes/user_indexes 索引的基本資料
all_ind_columns/dba_ind_columns/user_ind_columns 索引對應的欄位資訊