在數(shù)據(jù)量比較多時(shí)典蝌,建立合理的索引能降低數(shù)據(jù)查詢的時(shí)間粱快,但有些情況下恬砂,索引不一定會(huì)起作用
無法使用索引的select語句
1.對索引列使用了函數(shù),如:
select * from tb where max(id)=100
2.對索引列使用了'%xx'篡殷,如:
select * from tb where id like '%1'
需要注意的不是所有使用like關(guān)鍵字的select 語句都無法使用索引,比如
select * from tb where id like '1%'
就可以使用索引
3.在where子句中對列進(jìn)行類型轉(zhuǎn)換(其實(shí)也是使用到了函數(shù))
4.在組合索引的第1列不是使用最多的列,如在下面3個(gè)查詢語句中建立組合索引,按順序包含col2,col1,id列瞬内;
select * from tb where id='1' and col1='aa'
select id,sum(col1) from tb group by id
select * from tb where id='2' and col2='bb'
則第一句和第二句無法使用到索引 所以需要注意組合索引的順序
5.在where 子句中使用in關(guān)鍵字的某些句子
當(dāng)在in關(guān)鍵字后面使用嵌套的select語句卓箫,將無法使用在該列上定義的索引
如:
select * from tb where id in (select id from tb where ....)
這樣可以用到索引
select * from tb where id in('1','2')