left join 索引失效无条件_从零开始学数据分析-mysql索引优化方案
接上節,執行計劃還有一個重要的字段是extra,以下是出現的選項。
1、using filesort: 需要一次額外的查詢或者排序,性能開銷大。常見于order by語句中。
create對于單索引來說,如果排序和查找是同一個字段,則不會出現using filesort,反之會使用using filesort。
復合索引:不能跨列(最佳左前綴),如果跨列必然出現using filesort。
alter table test_02 add index idx_a1_a2_a3(a1, a2, a3); # using filesort a2 被跨了 explain select * from test_02 where a1 = '' order by a3; # using filesort a1被垮了 explain select * from test_02 where a2 = '' order by a3; # 不會出現using filesort explain select * from test_02 where a1 = '' order by a2;只要復合索引的順序是保證從左到右,無論where和order by是什么組合的,都不會出現filesort, a1 -> (a2) -> (a3)。
2、using temporary,用到了臨時表,性能開銷較大,盡量避免。一般出現在group by中。
原則:查詢哪些列就按照哪些列進行group by分組。
# 不出現 using temporary explain select a1 from test_02 where a1 in ('1', '2', '3') group by a1; # 出席那 using temporary,原因是因為查詢一次之后又使用了a2分組。 explain select a1 from test_02 where a1 in ('1', '2', '3') group by a2;如果發現語句出現問題,下面這篇文章解決,原因是mysql默認不允許對空行分組
https://blog.csdn.net/yalishadaa/article/details/72861737?blog.csdn.net再來熟悉一下sql解析過程:
from ... on ... join ... where ... group by ... having ... select distinct ... order by ... limit
3、using index,不讀取源文件,即從索引中獲取數據,不需要回表查詢,性能提升。即索引覆蓋。
// 使用到的數據均在索引里,使用索引即可獲得所有數據 select a1, a2 from test_02 where a1 = '' and a2 = ''; // 無發使用using index drop index idx_a1_a2_a3 on test_02; alter table test_02 add index idx_a1_a2(a1, a2); explain select a1, a3 from test_02 where a1 = '' and a2 = '';如果使用到了using index, 會對possiblekeys和key造成影響,如果沒有where則索引只出現在key中,如果有where,則索引只出現在key和possible_keys中。
4、using where,需要回表查詢,開銷大,在where中使用。
假設一個字段age是索引,但是其他字段不是,那么需要回表查詢。
# 繼上表,where查詢a3,那么該查詢需要回表。 explain select a1, a3 from test_02 where a1 = '' and a3 = '';5、impossible where,永遠不可為true的查詢where。
優化實例
create table test_03 (a1 int(3) not null,a2 int(3) not null,a3 int(3) not null,a4 int(3) not null ); alter table test_03 add index idx_a1_a2_a3_a4(a1, a2, a3, a4); # 這里雖然不是按照順序進行查詢,但是優化器做了優化,依舊可以利用索引 explain select a1, a2, a3, a4 from test_03 where a1 = 1 and a3 = 2 and a2 = 3 and a4 = 1;# a1->a2-> a4失效(回表)-> a3 using where explain select a1,a2,a3,a4 from test_03 where a1 = 1 and a2 = 2 and a4 = 4 order by a3; # a1-> a4 失效-> a3跨列 using where ,using filesort explain select a1,a2,a3,a4 from test_03 where a1 = 1 and a4 = 4 order by a3;# 推薦寫法 explain select a1, a2, a3, a4 from test_03 where a1 = 1 and a2 = 2 and a3 = 3 and a4 = 4;# 編寫時不按順序,但是優化器做了優化 explain select a1, a2, a3, a4 from test_03 where a1 = 1 and a2 = 2 and a4 = 3 and a3 = 4;# 不會using filesort 但是會觸發using index using where explain select a1, a2, a3, a4 from test_03 where a1 = 1 and a4 = 2 ordet by a2, a3; # a4失效,其他不夸列,using where, a4從using where中查找優化案例
單表優化、雙表優化、多表優化。
單表優化
create查詢authorid為1并且typeid為2或3的bid。
explain select bid from book where typeid in (2, 3) and authorid = 1 order by typeid;執行發現性能很差。
優化:加索引。
alter table book add index idx_bid_typeid_authorid(bid, typeid, authorid); explain select bid from book where typeid in (2, 3) and authorid = 1 order by typeid;type有了優化,但是不夠。
根據sql語句的實際解析過程,添加索引。先解析where 后解析select
drop性能提升。但是這里in可能會失效,導致整個索引失效,所以將in放到后面
drop總結:a、保持索引的定義和使用順序是一致的。b、將含有in的范圍查詢放到where最后,防止干擾。盡量少用in。
雙表優化
create table teacher2(tid int(4) primary key,cid int(4) not null ); insert into teacher2 values(1, 2); insert into teacher2 values(2, 1); insert into teacher2 values(3, 3);create table course2(cid int(4),cname varchar(20) ); insert into course2 value(1, 'java'); insert into course2 value(2, 'python'); insert into course2 value(3, 'js');左連接
explain select * from teacher2 t left join course2 c on t.cid = c.cid where c.cname='java';如果發現有using join buffer說明mysql內部做了優化。
索引加在哪里?a、小表驅動大表。b、索引建立在經常使用的字段上。(上述t.cid字段使用頻繁,因此左外連接加到t.cid上,右外連接的話加到右表上)
小表 10, 大表 500
select ... where 小表.x * 10 = 大表.y * 500 。兩次循環次數結果一樣,小表循環較少。
alter table teacher2 add index index_teacher2_cid(cid); alter table course2 add index index_course2_cname(cname);避免索引優化失效
a.復合索引,不要跨列或者無序使用。
b.盡量使用全索引匹配。盡量索引全用上。
不要在索引上進行任何操作。(計算,函數,類型轉換)索引會失效。
select ... where a.x * 2 = xxx
對于復合索引,左邊的一個索引失效,那么右邊索引都失效。
索引不能使用(!= <>)或者is null (is not null),否則索引失效,右側跟著失效。> 概率失效
獨立的索引沒有影響。
有的時候底層優化不生效,是概率的。需要explain 推測。盡量使用覆蓋索引。
like盡量以常量開頭,不要以%開頭,否則索引失效。
盡量不要使用類型轉換,顯式轉換和隱式轉換都不要,否則索引失效。
使用or,索引失效,可以將左側的索引都失效。
其他優化方法:
exist 和 in,如果主查詢的數據集大,則用in,如果子查詢的數據集大,則用exist。
select tname from teacher exist(select * from teacher);總結
以上是生活随笔為你收集整理的left join 索引失效无条件_从零开始学数据分析-mysql索引优化方案的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Leetcode】几种简单的排序算法
- 下一篇: mysql5.6.28安装_mysql5