oracle索引與約束(zhuang)

來源:互聯網
上載者:User
Oracle的索引
    索引和對應的表應該位於不同的資料表空間中,oracle能夠並行讀取位於不同硬碟上的資料,可以避免產生I/O衝突
B樹索引:在B樹的分葉節點中儲存索引欄位的值與ROWID。
唯一索引和不唯一索引都只是針對B樹索引而言.
Oracle最多允許包含32個欄位的複合索引

索引建立策略
1.匯入資料後再建立索引
2.不需要為很小的表建立索引
3.對於取值範圍很小的欄位(比如性別欄位)應當建立位元影像索引
4.限制表中的索引的數目
5.為索引設定合適的PCTFREE值
6.儲存索引的資料表空間最好單獨設定

建立不唯一索引 Sql代碼  

  1. create index emp_ename on employees(ename)    
  2. tablespace users    
  3. storage(......)    
  4. pctfree 0;   
create index emp_ename on employees(ename) tablespace users storage(......) pctfree 0; 

建立唯一索引 Sql代碼  

  1. create unique index emp_email on employees(email)    
  2. tablespace users;   
create unique index emp_email on employees(email) tablespace users; 

建立位元影像索引 Sql代碼  

  1. create bitmap index emp_sex on employees(sex)    
  2. tablespace users;   
create bitmap index emp_sex on employees(sex) tablespace users; 

建立反序索引 Sql代碼  

  1. create unique index order_reinx on orders(order_num,order_date)    
  2. tablespace users    
  3. reverse;   
create unique index order_reinx on orders(order_num,order_date) tablespace users reverse; 

建立函數索引(函數索引即可以是普通的B樹索引,也可以是位元影像索引) Sql代碼  

  1. create index emp_substr_empno    
  2. on employees(substr(empno,1,2))    
  3. tablespace users;   
create index emp_substr_empno on employees(substr(empno,1,2)) tablespace users; 

修改索引儲存參數(與表類似,INITIAL和MINEXTENTS參數在索引建立以後不能再改變) Sql代碼  

  1. 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代碼  

  1. alter table employees enable primary key using index pctfree 5;   
alter table employees enable primary key using index pctfree 5; 

清理索引片段
1.合并索引(只是簡單的將B樹葉結點中的儲存片段合并在一起,並不會改變索引的物理組織圖) Sql代碼  

  1. alter index emp_pk coalesce;   
alter index emp_pk coalesce; 

2.重建索引(不僅能夠消除儲存片段,還可以改變索引的全部儲存參數設定,並且可以將索引移動到其它的資料表空間中,重建索引
實際上就是再指定的資料表空間中重建立立一個新的索引,然後刪除原來的索引) Sql代碼  

  1. alter index emp_pk rebuild;   
alter index emp_pk rebuild; 

刪除索引 Sql代碼  

  1. 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 索引對應的欄位資訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.