。常用與UPPER、LOWER、TO_CHAR(date)等函數分類上,例:
create index idx_func on emp (UPPER(ename)) tablespace tablespace_name;
建立位元影像索引
。對基數較小,且基數相對穩定的列建立索引時,首先應該考慮位元影像索引,例:
create bitmap index idx_bitm on class (classno) tablespace tablespace_name;
明確地建立唯一索引
。可以用create unique index語句來建立唯一索引,例:
create unique index dept_unique_idx on dept(dept_no) tablespace idx_1;
建立與約束相關的索引
。可以用using index字句,為與unique和primary key約束相關的索引,例如:
alter table table_name
add constraint PK_primary_keyname primary key (field_name)
using index tablespace tablespace_name;
如何建立局部分區索引
。基礎資料表必須是分區表;
。分區數量與基礎資料表相同;
。每個索引分割區的子分區數量與相應的基礎資料表分區相同;
。基礎資料表的子分區中的行的索引項目,被儲存在該索引的相應的子分區中,例如:
Create Index TG_CDR04_SERV_ID_IDX On TG_CDR04(SERV_ID)
Pctfree 5
Tablespace TBS_AK01_IDX
Storage (
MaxExtents 32768
PctIncrease 0
FreeLists 1
FreeList Groups 1
)
local
/
如何建立定界分割的全域索引
。基礎資料表可以是全域表和分區表。
create index idx_start_date on tg_cdr01(start_date)
global partition by range(start_date)
(partition p01_idx vlaues less than (‘0106’)
partition p01_idx vlaues less than (‘0111’)
…
partition p01_idx vlaues less than (‘0401’ ))
/
重建現存的索引
重建現存的索引的當前時刻不會影響查詢;
重建索引可以刪除額外的資料區塊;
提高索引查詢效率;
alter index idx_name rebuild nologging;
對於分區索引:
alter index idx_name rebuild partition partiton_name nologging;
要刪除索引的原因
。不再需要的索引;
。索引沒有針對其相關的表所發布的查詢提供所期望的效能改善;
。應用沒有用該索引來查詢資料;
。該索引無效,必須在重建之前刪除該索引;
。該索引已經變的太碎了,必須在重建之前刪除該索引;
。語句:drop index idx_name;drop index idx_name drop partition partition_name;
。如果從以上幾個方面都查不出原因的話,我們只好用採用在語句中加hint的方式強制ORACLE使用最優的“執行計畫”。 hint採用注釋的方式,有行注釋和段注釋兩種方式。 如我們想要用到A表的IND_COL1索引的話,可採用以下方式: “SELECT /*+ INDEX(A IND_COL1)*/ * FROM A WHERE COL1 = XXX;"
如何屏蔽索引
語句的執行計畫中有不良索引時,可以人為地屏蔽該索引,方法:
。數值型:在索引欄位上加0,例如
select * from emp where emp_no+0 = v_emp_no;
。字元型:在索引欄位上加‘’,例如
select * from tg_cdr01 where msisdn||’’=v_msisdn;