日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
MySQL索引失效的幾種類型以及解決方式

MySQL索引失效的情況有哪些呢?本文就列舉下面七種類型以及解決方式。

1. ?索引列不獨立

是指被索引的這列不能是表達式的一部分,不能是函數(shù)的參數(shù),比如下面的這種情況:

select id,name,age,salary from table_name where salary + 1000 = 6000;

salary 列被用戶表達式的計算了,這種情況下索引就會失效,解決方式就是提前計算好條件值,不要讓索引列參與表達式計算。

索引字段作為函數(shù)的參數(shù):

select id,name,age,salary from table_name where substring(name,1,3)= 'luc';

解決方式是什么呢,可以提前計算好條件,不要使用索引,或者可以使用其他的 sql 替換上面的,比如,上面的sql 可以使用 like 來代替:

select id,name,age,salary from table_name where name like 'luc%';

2. 使用了左模糊

select id,name,age,salary from table_name where name like '%lucs%';

平時盡可能避免用到左模糊,可以這樣寫:

select id,name,age,salary from table_name where name like 'lucs%';

如果實在避免不了左模糊查詢的話,考慮一下搜索引擎 比如 ES。

3. or 查詢部分字段沒有使用索引

select id,name,age,salary from table_name where name ='lucs' and age >25

這種情況,可以為 name 和 age 都建立索引,否則會走全表掃描。

4. 字符串條件沒有使用 ''

select id,name,age,salary from table_name where phone=13088772233

上面的這條 sql phone 字段類型是 字符串類型的,但是沒有使用 '13088772233 ', SQL 就全表掃描了,所以字符串索引要使用 ‘’:

select id,name,age,salary from table_name where phone='13088772233 '

5. 不符合最左前綴原則的查詢

例如有這樣一個組合索引 index(a,b,c):

select * from table_name where b='1'and c='2'
select * from table_name where c='2'

// 上面這兩條 SQL 都是無法走索引執(zhí)行的

最左原則,就是要最左邊的優(yōu)先存在,我不在的話,你們自己就玩不動了,除非你自己單獨創(chuàng)立一個索引,下面這幾條 SQL 就可以走索引執(zhí)行:

select * from table_name where a = 'asaa' and b='1'and c='2'
select * from table_name where a = 'asda' and b='1231'
// 上面這兩條是走索引的,但是下面這條你覺得索引應(yīng)該怎么走,是全部走,還是部分走索引?
select * from table_name where a = 'asda' and c='dsfsdafsfsd'

6. 索引字段沒有添加 not null 約束:

select * from table_name where a is null;
// 這條sql就無法走索引執(zhí)行了,is null 條件 不能使用索引,只能全表掃描了
// mysql 官方建議是把字段設(shè)置為 not null

所以針對這個情況,在mysql 創(chuàng)建表字段的時候,可以將需要索引的字符串設(shè)置為 not null default '' 默認空字符串即可

7. 隱式轉(zhuǎn)換

關(guān)聯(lián)表的兩個字段類型不一致會發(fā)生隱式轉(zhuǎn)換?:

select * from table_name t1 left join table_name2 t2 on t1.id=t2.tid;
// 上面這條語句里,如果 t1 表的id 類型和 t2 表的tid 類型不一致的時候,就無法
// 按索引執(zhí)行了。
// 解決方式就是統(tǒng)一設(shè)置字段類型。

分享名稱:MySQL索引失效的幾種類型以及解決方式
本文網(wǎng)址:http://www.5511xx.com/article/djidigp.html