1.索引分類
a) 唯一索引, 作用是數據約束,保證數據唯一,還有就是數據索引,提高查詢效率
b)一般索引,只有數據索引的作用,
2.唯一索引的建立
create unique index 索引名 on 表名(字段名)
ok,假設有一個Emploeyy表,裏面有一個empName字段,我們來爲empName添加唯一索引
create unique index idx_empname on employee(empname);
3.一般索引
create index 索引名 on 表名(字段名)
ok,現在我們爲employee的address字段,添加一般索引
create index idx_address on employee(address);
我們還可以爲兩多個字段建立索引
create unique index idx_test on employee(field1,field2);
這樣,爲field1,field2添加了唯一索引,field1和field2的組合是唯一的了
還可以指定索引排序
create index idx_test employee(field1 ,field2 desc);;
4.函數索引
如果在我們的查詢條件使用了函數,那麼索引就不可用了。
可以用建立函數索引的方式,來解決這個問題
例如:
select * from product where nvl(price,0.0)>1000.0 ;
這裏,nvl(price,0.0)使用了函數,索引不能利用price字段上做的索引了
ok,我們來創建函數索引
create index index_price on product(nvl(price,0.0));
5.索引的刪除
drop index 索引名
drop index idx_empname;
6.其它的
唯一索引能極大的提高查詢速度,而且還有唯一約束的作用
一般索引,只能提高30%左右的速度
經常插入,修改,應在查詢允許的情況下,儘量減少索引,因爲添加索引,插入,修改等操作,需要更多的時間
可以在order by的字段,where的條件字段,join的關聯字段添加索引
比如:
select * from table1 t1
left join table2 t2 on t1.字段A=t2.字段B
where t1.字段C = '值'
order by t1.字段D
這裏,A,B,C,D字段,都應該添加索引