一、創建索引的原則
檢索數據不超過表中數據的10%或15%時,創建索引
相對較小的表不宜創建索引
對包含在表連接操作中的列創建索引
對在where子句中頻繁使用的列創建索引
對包含在order by和group by或者涉及排序的union和distinct等其他操作中的列創建索引
有長字符組成的列不宜創建索引
被頻繁更改的列不宜創建索引
對錶中選擇性高的列創建索引
在OLTP中,表中索引宜少不宜多;而OLAP中,表中的索引可以多一點
where子句中經常涉及多列,可以考慮創建組合索引
二、估計索引的大小
declare
l_used_bytes number;
l_allocate_bytes number;
begin
dbms_space.create_index_cost(
ddl =>'create index index_name on schema.table_name(column_name)',
used_bytes =>l_used_bytes,
alloc_bytes =>l_allocate_bytes);
dbms_output.put_line('used_bytes='||l_used_bytes||','||'allocate_bytes='||l_allocate_bytes);
end;
三、創建唯一索引
create unique index index_name on schema.table_name(column_name);
四、創建B-樹索引 --適於在選擇性高、更改較頻繁的列上創建
create index index_name on schema.table_name(column_name);
五、創建位圖索引 --適於在選擇性低、更改不頻繁的列上創建
create bitmap index index_name on schema.table_name(column_name);
六、創建反鍵索引 --適用於列值前面差異小,後面差異大的列。在索引段中索引列的值反向存儲。
create index index_name on schema.table_name(column_name) reverse;
七、創建函數索引 --不能使用分組函數
create index index_name on schema.table_name(function_name(column_name));
八、創建組合索引 --應該把經常使用的列放在最前面
create index index_name on schema.table_name(column_name1,column_name2 [,column_name3 [, ...]]);
九、分區索引
1、全局索引 --索引激列和表的分區鍵不同;不能使用列表分區對索引進行分區;使用範圍分區時,必須包含maxsize分區;全局索引的分區和表的分區沒有關聯。
create index index_name on schema.table_name(columa_name)
global
partition by 分區方法(column_name)
(
partition par_name ..........
)
2、本地索引 --索引列和表的分區鍵不同,索引分區和表的分區一一對應。
create index index_name on schema.table(column_name)
local;
十、合併和重建索引 --合併僅合併碎片,重建索引使用原索引數據創建新索引,要求表空間能同時容納兩個索引。重建不僅達到合併碎片的目的,也使索引結構更加緊湊。
合併:alter index index_name coalesce;
重建:alter index index_name rebuild;
聯機重建:alter index index_name rebuild online;
十一、監控索引
1、監控
alter index index_name monitoring usage;
2、取消監控 --開始監控之後,經過一段時間,然後取消監控,最後查看索引是否使用
alter index index_name nomonitoring usage;
3、查看使用情況
select * from v$object_usage where index_name='index_name';