帶你學(xué)MySQL系列 | “數(shù)據(jù)分析師”面試最怕被問到的SQL優(yōu)化問題(下)

      網(wǎng)友投稿 680 2025-04-01

      本文大綱

      前面我已經(jīng)帶著大家學(xué)習(xí)了本文的第1-4個部分,今天就帶大家學(xué)習(xí)這剩下的5-8個部分。MySQL優(yōu)化問題對于新手學(xué)習(xí),一般是個難題!我的教程自認為已經(jīng)是很通俗易懂的。如果你學(xué)習(xí)了這個教程后,仍然不太理解,可以去B站找到一個視頻瀏覽一遍,然后再回頭看我的文章。

      講解使用的數(shù)據(jù)源

      在上篇最后,我們已經(jīng)給出了本文需要使用到的數(shù)據(jù)代碼,這里我直接給出這3張表的圖示。

      5. explain執(zhí)行計劃常用關(guān)鍵字詳解

      # 查看執(zhí)行計劃 explain select t.* from teacher t,course c,teacherCard tc where t.tid = c.tid and t.tcid = tc.tcid and (c.cid = 2 or tc.tcid = 3);

      1

      2

      3

      4

      5

      結(jié)果如下:

      接著,在往teacher表中增加幾條數(shù)據(jù)。

      insert into teacher values(4,'ta',4); insert into teacher values(5,'tb',5); insert into teacher values(6,'tc',6);

      1

      2

      3

      再次查看執(zhí)行計劃。

      # 查看執(zhí)行計劃 explain select t.* from teacher t,course c,teacherCard tc where t.tid = c.tid and t.tcid = tc.tcid and (c.cid = 2 or tc.tcid = 3);

      1

      2

      3

      4

      5

      6

      7

      8

      9

      結(jié)果如下:

      這里先記住一句話:表的執(zhí)行順序 ,因表數(shù)量改變而改變的原因:笛卡爾積。怎么回事呢?看看下面這個例子。

      # 下面舉一個例子 a b c 2 3 4 最終:2 * 3 * 4 = 6 * 4 = 24 c b a 4 3 2 最終:4 * 3 * 2 = 12 * 2 = 24

      1

      2

      3

      4

      帶你學(xué)MySQL系列 | “數(shù)據(jù)分析師”面試最怕被問到的SQL優(yōu)化問題(下)

      5

      6

      7

      分析:最終執(zhí)行的條數(shù),雖然是一致的。但是中間過程,有一張臨時表是6,一張臨時表是12,很明顯6 < 12,對于內(nèi)存來說,數(shù)據(jù)量越小越好,因此優(yōu)化器肯定會選擇第一種執(zhí)行順序。

      結(jié)論:id值相同,從上往下順序執(zhí)行。表的執(zhí)行順序因表數(shù)量的改變而改變,數(shù)量越小,越在前面執(zhí)行。

      # 查看執(zhí)行計劃 explain select tc.tcdesc from teacherCard tc where tc.tcid = ( select t.tcid from teacher t where t.tid = (select c.tid from course c where c.cname = 'sql') );

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      結(jié)果如下:

      結(jié)論:id值不同,id值越大越優(yōu)先查詢。這是由于在進行嵌套子查詢時,先查內(nèi)層,再查外層。

      # 查看執(zhí)行計劃 explain select t.tname ,tc.tcdesc from teacher t,teacherCard tc where t.tcid= tc.tcid and t.tid = (select c.tid from course c where cname = 'sql') ;

      1

      2

      3

      4

      5

      6

      7

      8

      9

      結(jié)果如下:

      結(jié)論:id值有相同,又有不同。id值越大越優(yōu)先;id值相同,從上往下順序執(zhí)行。

      select_type關(guān)鍵字共有如下常用的6種類型,下面我分別帶著大家梳理一下,它們各自的含義。

      不包含子查詢,不包含union查詢。

      explain select * from teacher;

      1

      結(jié)果如下:

      關(guān)于primary和subquery,我們就拿下面的這個例子進行演示。從代碼中可以看到這個SQL語句是存在子查詢的,換句話說,這個SQL語句包含子查詢。where內(nèi)層(非最外層)使用到的c表屬于非最外層,因此是subquery關(guān)鍵字。where外層使用到了t表 和tc表,因此是primary關(guān)鍵字。

      # 查看執(zhí)行計劃 explain select t.tname ,tc.tcdesc from teacher t,teacherCard tc where t.tcid= tc.tcid and t.tid = (select c.tid from course c where cname = 'sql') ;

      1

      2

      3

      4

      5

      6

      7

      8

      9

      結(jié)果如下:

      a.在from子查詢中,只有一張表;

      b.在from子查詢中,如果table1 union table2,則table1就是derived表;

      explain select cr.cname from ( select * from course where tid = 1 union select * from course where tid = 2 ) cr ;

      1

      2

      3

      4

      5

      結(jié)果如下:

      type關(guān)鍵字可以很好的說明,你寫的SQL語句好不好。system、const是我們達不到的理想狀況,實際上只能優(yōu)化到index --> range --> ref這個級別,也可以看到ALL是最差的一個級別。

      對type進行優(yōu)化的前提,是你得創(chuàng)建索引。

      源表只有一條數(shù)據(jù)(實際中,基本不可能);

      衍生表只有一條數(shù)據(jù)的主查詢(偶爾可以達到)。

      僅僅能查到一條數(shù)據(jù)的SQL ,僅針對Primary key主鍵索引或unique索引類型有效。

      explain select tid from test01 where tid =1 ;

      1

      結(jié)果如下:

      刪除以前的主鍵索引后,此時我們添加一個其他的普通索引:

      create index test01_index on test01(tid) ; # 再次查看執(zhí)行計劃 explain select tid from test01 where tid =1 ;

      1

      2

      3

      結(jié)果如下:

      可以發(fā)現(xiàn):當(dāng)tid字段去掉主鍵索引,換為普通索引后,優(yōu)化級別就不是const了。

      唯一性索引,對于每個索引鍵的查詢,返回匹配唯一行數(shù)據(jù)(有且只有1個,不能多 、不能0),并且查詢結(jié)果和表中數(shù)據(jù)條數(shù)必須一致。

      此種情況常見于唯一索引和主鍵索引。

      delete from teacher where tcid >= 4; alter table teacherCard add constraint pk_tcid primary key(tcid); alter table teacher add constraint uk_tcid unique index(tcid) ; explain select t.tcid from teacher t,teacherCard tc where t.tcid = tc.tcid ;

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      結(jié)果如下:

      總結(jié):以上SQL,用到的索引是t.tcid,即teacher表中的tcid字段;如果teacher表的數(shù)據(jù)個數(shù)和連接查詢的數(shù)據(jù)個數(shù)一致(都是3條數(shù)據(jù)),則有可能滿足eq_ref級別;否則無法滿足。條件很苛刻,很難達到。

      非唯一性索引,對于每個索引鍵的查詢,返回匹配的所有行(可以0,可以1,可以多)

      準備數(shù)據(jù):

      創(chuàng)建索引,并查看執(zhí)行計劃:

      # 添加索引 alter table teacher add index index_name (tname) ; # 查看執(zhí)行計劃 explain select * from teacher where tname = 'tz';

      1

      2

      3

      4

      結(jié)果如下:

      檢索指定范圍的行 ,where后面是一個范圍查詢(between, >, <, >=, in)

      in有時候會失效,從而轉(zhuǎn)為無索引時候的ALL

      # 添加索引 alter table teacher add index tid_index (tid) ; # 查看執(zhí)行計劃:以下寫了一種等價SQL寫法,查看執(zhí)行計劃 explain select t.* from teacher t where t.tid in (1,2) ; explain select t.* from teacher t where t.tid <3 ;

      1

      2

      3

      4

      5

      結(jié)果如下:

      查詢?nèi)克饕械臄?shù)據(jù)(掃描整個索引)

      查詢?nèi)吭幢碇械臄?shù)據(jù)(暴力掃描全表)

      注意:cid是索引字段,因此查詢索引字段,只需要掃描索引表即可。但是tid不是索引字段,查詢非索引字段,需要暴力掃描整個源表,會消耗更多的資源。

      possible_keys可能用到的索引。是一種預(yù)測,不準。了解一下就好。

      key指的是實際使用的索引。

      # 先給course表的cname字段,添加一個索引 create index cname_index on course(cname); # 查看執(zhí)行計劃 explain select t.tname ,tc.tcdesc from teacher t,teacherCard tc where t.tcid= tc.tcid and t.tid = (select c.tid from course c where cname = 'sql') ;

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      結(jié)果如下:

      有一點需要注意的是:如果possible_key/key是NULL,則說明沒用索引。

      索引的長度:用于判斷復(fù)合索引是否被完全使用(a,b,c)。

      # 創(chuàng)建表 create table test_kl ( name char(20) not null default '' ); # 添加索引 alter table test_kl add index index_name(name) ; # 查看執(zhí)行計劃 explain select * from test_kl where name ='' ;

      1

      2

      3

      4

      5

      6

      7

      8

      9

      結(jié)果如下:

      結(jié)果分析:因為我沒有設(shè)置服務(wù)端的字符集,因此默認的字符集使用的是latin1,對于latin1一個字符代表一個字節(jié),因此這列的key_len的長度是20,表示使用了name這個索引。

      # 新增一個字段name1,name1可以為null alter table test_kl add column name1 char(20) ; # 給name1字段,設(shè)置為索引字段 alter table test_kl add index index_name1(name1) ; # 查看執(zhí)行計劃 explain select * from test_kl where name1 ='' ;

      1

      2

      3

      4

      5

      6

      結(jié)果如下:

      結(jié)果分析:如果索引字段可以為null,則mysql底層會使用1個字節(jié)用于標(biāo)識null。

      # 刪除原來的索引name和name1 drop index index_name on test_kl ; drop index index_name1 on test_kl ; # 增加一個復(fù)合索引 create index name_name1_index on test_kl(name,name1); # 查看執(zhí)行計劃 explain select * from test_kl where name1 = '' ; explain select * from test_kl where name = '' ;

      1

      2

      3

      4

      5

      6

      7

      8

      結(jié)果如下:

      結(jié)果分析:對于下面這個執(zhí)行計劃,可以看到我們只使用了復(fù)合索引的第一個索引字段name,因此key_len是20,這個很清楚。再看上面這個執(zhí)行計劃,我們雖然僅僅在where后面使用了復(fù)合索引字段中的name1字段,但是你要使用復(fù)合索引的第2個索引字段,會默認使用了復(fù)合索引的第1個索引字段name,由于name1可以是null,因此key_len = 20 + 20 + 1 = 41呀!

      # 新增一個字段name2,name2可以為null alter table test_kl add column name2 varchar(20) ; # 給name2字段,設(shè)置為索引字段 alter table test_kl add index name2_index(name2) ; # 查看執(zhí)行計劃 explain select * from test_kl where name2 = '' ;

      1

      2

      3

      4

      5

      6

      結(jié)果如下:

      結(jié)果分析:key_len = 20 + 1 + 2,這個20 + 1我們知道,這個2又代表什么呢?原來varchar屬于可變長度,在mysql底層中,用2個字節(jié)標(biāo)識可變長度。

      這里的ref的作用,指明當(dāng)前表所參照的字段。

      注意與type中的ref值區(qū)分。在type中,ref只是type類型的一種選項值。

      # 給course表的tid字段,添加一個索引 create index tid_index on course(tid); # 查看執(zhí)行計劃 explain select * from course c,teacher t where c.tid = t.tid and t.tname = 'tw';

      1

      2

      3

      4

      5

      6

      結(jié)果如下:

      結(jié)果分析:有兩個索引,c表的c.tid引用的是t表的tid字段,因此可以看到顯示結(jié)果為【數(shù)據(jù)庫名.t.tid】,t表的t.name引用的是一個常量"tw",因此可以看到結(jié)果顯示為const,表示一個常量。

      被索引優(yōu)化查詢的數(shù)據(jù)個數(shù) (實際通過索引而查詢到的數(shù)據(jù)個數(shù))

      explain select * from course c,teacher t where c.tid = t.tid and t.tname = 'tz' ;

      1

      2

      3

      4

      結(jié)果如下:

      表示其他的一些說明,也非常有用,通過這個關(guān)鍵字也可以很好的說明,你寫的SQL語句到底好不好。

      當(dāng)出現(xiàn)了這個詞,表示你當(dāng)前的SQL性能消耗較大。表示進行了一次額外的排序。常見于order by語句中。

      Ⅰ 什么是“額外”的排序?

      為了講清楚這個,我們首先要知道什么是排序。我們?yōu)榱私o某一個字段進行排序的時候,首先你得先查詢到這個字段,然后在將這個字段進行排序。

      緊接著,我們查看如下兩個SQL語句的執(zhí)行計劃。

      # 新建一張表,建表同時創(chuàng)建索引 create table test02 ( a1 char(3), a2 char(3), a3 char(3), index idx_a1(a1), index idx_a2(a2), index idx_a3(a3) ); # 查看執(zhí)行計劃 explain select * from test02 where a1 ='' order by a1 ; explain select * from test02 where a1 ='' order by a2 ;

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      結(jié)果如下:

      結(jié)果分析:對于第一個執(zhí)行計劃,where后面我們先查詢了a1字段,然后再利用a1做了依次排序,這個很輕松。但是對于第二個執(zhí)行計劃,where后面我們查詢了a1字段,然而利用的卻是a2字段進行排序,此時myql底層會進行一次查詢,進行“額外”的排序。

      總結(jié):對于單索引,如果排序和查找是同一個字段,則不會出現(xiàn)using filesort;如果排序和查找不是同一個字段,則會出現(xiàn)using filesort;

      因此where哪些字段,就order by哪些些字段。

      不能跨列(官方術(shù)語:最佳左前綴),這句話一定要牢記?。?!

      # 刪除test02的索引 drop index idx_a1 on test02; drop index idx_a2 on test02; drop index idx_a3 on test02; # 創(chuàng)建一個復(fù)合索引 alter table test02 add index idx_a1_a2_a3 (a1,a2,a3) ; # 查看下面SQL語句的執(zhí)行計劃 explain select *from test02 where a1='' order by a3 ; --using filesort explain select *from test02 where a2='' order by a3 ; --using filesort explain select *from test02 where a1='' order by a2 ;

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      結(jié)果如下:

      結(jié)果分析:復(fù)合索引的順序是(a1,a2,a3),可以看到a1在最左邊,因此a1就叫做最佳左前綴,

      如果要使用后面的索引字段,必須先使用到這個a1字段。

      對于explain1,where后面我們使用a1字段,但是后面的排序使用了a3,直接跳過了a2,屬于跨列;對于explain2,where后面我們使用了a2字段,直接跳過了a1字段,也屬于跨列;對于explain3,where后面我們使用a1字段,后面使用的是a2字段,因此沒有出現(xiàn)【using filesort】。

      當(dāng)出現(xiàn)了這個詞,也表示你當(dāng)前的SQL性能消耗較大。這是由于當(dāng)前SQL用到了臨時表,一般出現(xiàn)在group by中。

      explain select a1 from test02 where a1 in ('1','2','3') group by a1 ; explain select a1 from test02 where a1 in ('1','2','3') group by a2 ; --using temporary

      1

      2

      結(jié)果如下:

      結(jié)果分析:當(dāng)你查詢哪個字段,就按照那個字段分組,否則就會出現(xiàn)using temporary。

      ---------------------------------------------------------------------------------------------------------------

      針對using temporary,我們在看一個例子:

      using temporary表示需要額外再使用一張表,一般出現(xiàn)在group by語句中。雖然已經(jīng)有表了,但是不適用,必須再來一張表。

      再次來看mysql的編寫過程和解析過程。

      Ⅰ 編寫過程

      select dinstinct ..from ..join ..on ..where ..group by ..having ..order by ..limit ..

      1

      Ⅱ 解析過程

      from .. on.. join ..where ..group by ..having ..select dinstinct ..order by ..limit ..

      1

      很顯然,where后是group by,然后才是select?;诖?,我們再查看如下兩個SQL語句的執(zhí)行計劃。

      explain select * from test03 where a2=2 and a4=4 group by a2,a4; explain select * from test03 where a2=2 and a4=4 group by a3;

      1

      2

      分析如下:對于第一個執(zhí)行計劃,where后面是a2和a4,接著我們按照a2和a4分組,很明顯這兩張表已經(jīng)有了,直接在a2和a4上分組就行了。但是對于第二個執(zhí)行計劃,where后面是a2和a4,接著我們卻按照a3分組,很明顯我們沒有a3這張表,因此有需要再來一張臨時表a3,因此就會出現(xiàn)using temporary。

      當(dāng)你看到這個關(guān)鍵詞,恭喜你,表示你的SQL性能提升了。

      using index稱之為

      索引覆蓋。

      當(dāng)出現(xiàn)了using index,就表示不用讀取源表,而只利用索引獲取數(shù)據(jù),不需要回源表查詢。

      只要使用到的列,全部出現(xiàn)在索引中,就是索引覆蓋。

      # 刪除test02中的復(fù)合索引idx_a1_a2_a3 drop index idx_a1_a2_a3 on test02; # 重新創(chuàng)建一個復(fù)合索引idx_a1_a2 create index idx_a1_a2 on test02(a1,a2); # 查看執(zhí)行計劃 explain select a1,a3 from test02 where a1='' or a3= '' ; explain select a1,a2 from test02 where a1='' and a2= '' ;

      1

      2

      3

      4

      5

      6

      7

      結(jié)果如下:

      結(jié)果分析:這里我們創(chuàng)建的是a1和a2的復(fù)合索引,對于第一個執(zhí)行計劃,我們卻出現(xiàn)了a3,該字段并沒有創(chuàng)建索引,因此沒有出現(xiàn)using index,而是using where,表示我們需要回表查詢。對于第二個執(zhí)行計劃,屬于完全的索引覆蓋,因此出現(xiàn)了using index。

      ----------------------------------------------------------------------------------------------------------------

      針對using index,我們在查看一個案例:

      explain select a1,a2 from test02 where a1='' or a2= '' ; explain select a1,a2 from test02 ;

      1

      2

      結(jié)果如下:

      如果用到了索引覆蓋(using index時),會對possible_keys和key造成影響:

      a.如果沒有where,則索引只出現(xiàn)在key中;

      b.如果有where,則索引 出現(xiàn)在key和possible_keys中。

      表示需要【回表查詢】,表示既在索引中進行了查詢,又回到了源表進行了查詢。

      # 刪除test02中的復(fù)合索引idx_a1_a2 drop index idx_a1_a2 on test02; # 將a1字段,新增為一個索引 create index a1_index on test02(a1); # 查看執(zhí)行計劃 explain select a1,a3 from test02 where a1="" and a3="" ;

      1

      2

      3

      4

      5

      6

      結(jié)果如下:

      結(jié)果分析:我們使用了索引a1,表示我們使用了索引進行查詢。但是又對于a3字段,我們并沒有使用索引,因此對于a3字段,需要回源表查詢,這個時候出現(xiàn)了using where。

      當(dāng)where子句永遠為False的時候,會出現(xiàn)impossible where。

      # 查看執(zhí)行計劃 explain select a1 from test02 where a1="a" and a1="b" ;

      1

      2

      結(jié)果如下:

      # 創(chuàng)建新表 create table test03 ( a1 int(4) not null, a2 int(4) not null, a3 int(4) not null, a4 int(4) not null ); # 創(chuàng)建一個復(fù)合索引 create index a1_a2_a3_test03 on test03(a1,a2,a3); # 查看執(zhí)行計劃 explain select a3 from test03 where a1=1 and a2=2 and a3=3;

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      結(jié)果如下:

      【推薦寫法】:復(fù)合索引順序和使用順序一致。

      【不推薦寫法】:復(fù)合索引順序和使用順序不一致。

      # 查看執(zhí)行計劃 explain select a3 from test03 where a3=1 and a2=2 and a1=3;

      1

      2

      結(jié)果如下:

      結(jié)果分析:雖然結(jié)果和上述結(jié)果一致,但是不推薦這樣寫。但是這樣寫怎么又沒有問題呢?這是由于SQL優(yōu)化器的功勞,它幫我們調(diào)整了順序。

      最后再補充一點:對于復(fù)合索引,不要跨列使用。

      # 查看執(zhí)行計劃 explain select a3 from test03 where a1=1 and a3=2 group by a3;

      1

      2

      結(jié)果如下:

      結(jié)果分析:a1_a2_a3是一個復(fù)合索引,我們使用a1索引后,直接跨列使用了a3,直接跳過索引a2,因此索引a3失效了。當(dāng)再使用a3進行分組的時候,就會出現(xiàn)using where。

      # 創(chuàng)建新表 create table book ( bid int(4) primary key, name varchar(20) not null, authorid int(4) not null, publicid int(4) not null, typeid int(4) not null ); # 插入數(shù)據(jù) insert into book values(1,'tjava',1,1,2) ; insert into book values(2,'tc',2,1,2) ; insert into book values(3,'wx',3,2,1) ; insert into book values(4,'math',4,2,3) ;

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      結(jié)果如下:

      案例:查詢authorid=1且typeid為2或3的bid,并根據(jù)typeid降序排列。

      explain select bid from book where typeid in(2,3) and authorid=1 order by typeid desc ;

      1

      2

      3

      4

      結(jié)果如下:

      這是沒有進行任何優(yōu)化的SQL,可以看到typ為ALL類型,extra為using filesort,可以想象這個SQL有多恐怖。

      優(yōu)化:添加索引的時候,要根據(jù)MySQL解析順序添加索引,又回到了MySQL的解析順序,下面我們再來看看MySQL的解析順序。

      from .. on.. join ..where ..group by ..having ..select dinstinct ..order by ..limit ..

      1

      # 添加索引 create index typeid_authorid_bid on book(typeid,authorid,bid); # 再次查看執(zhí)行計劃 explain select bid from book where typeid in(2,3) and authorid=1 order by typeid desc ;

      1

      2

      3

      4

      5

      6

      7

      結(jié)果如下:

      結(jié)果分析:結(jié)果并不是和我們想象的一樣,還是出現(xiàn)了using where,查看索引長度key_len=8,表示我們只使用了2個索引,有一個索引失效了。

      將in字段放在最后面。

      需要注意一點:每次創(chuàng)建新的索引的時候,最好是刪除以前的廢棄索引,否則有時候會產(chǎn)生干擾(索引之間)。

      # 刪除以前的索引 drop index typeid_authorid_bid on book; # 再次創(chuàng)建索引 create index authorid_typeid_bid on book(authorid,typeid,bid); # 再次查看執(zhí)行計劃 explain select bid from book where authorid=1 and typeid in(2,3) order by typeid desc ;

      1

      2

      3

      4

      5

      6

      7

      8

      9

      結(jié)果如下:

      結(jié)果分析:這里雖然沒有變化,但是這是一種優(yōu)化思路。

      總結(jié)如下:

      a.最佳做前綴,保持索引的定義和使用的順序一致性

      b.索引需要逐步優(yōu)化(每次創(chuàng)建新索引,根據(jù)情況需要刪除以前的廢棄索引)。

      c.將含in的范圍查詢,放到where條件的最后,防止失效。

      本例中同時出現(xiàn)了Using where(需要回原表); Using index(不需要回原表):原因,where authorid=1 and typeid in(2,3)中authorid在索引(authorid,typeid,bid)中,因此不需要回原表(直接在索引表中能查到);而typeid雖然也在索引(authorid,typeid,bid)中,但是含in的范圍查詢已經(jīng)使該typeid索引失效,因此相當(dāng)于沒有typeid這個索引,所以需要回原表(using where);

      下面這個例子,沒有了in,則不會出現(xiàn)using where:

      explain select bid from book where authorid=1 and typeid =3 order by typeid desc ;

      1

      2

      3

      結(jié)果如下:

      # 創(chuàng)建teacher2新表 create table teacher2 ( tid int(4) primary key, cid int(4) not null ); # 插入數(shù)據(jù) insert into teacher2 values(1,2); insert into teacher2 values(2,1); insert into teacher2 values(3,3); # 創(chuàng)建course2新表 create table course2 ( cid int(4) , cname varchar(20) ); # 插入數(shù)據(jù) insert into course2 values(1,'java'); insert into course2 values(2,'python'); insert into course2 values(3,'kotlin');

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      結(jié)果如下:

      案例:使用一個左連接,查找教java課程的所有信息。

      explain select * from teacher2 t left outer join course2 c on t.cid=c.cid where c.cname='java';

      1

      2

      3

      4

      5

      6

      結(jié)果如下:

      對于兩張表,索引往哪里加?答:對于表連接,小表驅(qū)動大表。索引建立在經(jīng)常使用的字段上。

      為什么小表驅(qū)動大表好一些呢?

      小表:10 大表:300 # 小表驅(qū)動大表 select ...where 小表.x10=大表.x300 ; for(int i=0;i<小表.length10;i++) { for(int j=0;j<大表.length300;j++) { ... } } # 大表驅(qū)動小表 select ...where 大表.x300=小表.x10 ; for(int i=0;i<大表.length300;i++) { for(int j=0;j<小表.length10;j++) { ... } }

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      分析:以上2個FOR循環(huán),最終都會循環(huán)3000次;但是對于雙層循環(huán)來說:一般建議,將數(shù)據(jù)小的循環(huán),放外層。數(shù)據(jù)大的循環(huán),放內(nèi)層。

      不用管這是為什么,這是編程語言的一個原則,對于雙重循環(huán),外層循環(huán)少,內(nèi)存循環(huán)大,程序的性能越高。

      結(jié)論:當(dāng)編寫【…on t.cid=c.cid】時,將數(shù)據(jù)量小的表放左邊(假設(shè)此時t表數(shù)據(jù)量小,c表數(shù)據(jù)量大。)

      我們已經(jīng)知道了,對于兩表連接,需要利用小表驅(qū)動大表。例如【…on t.cid=c.cid】,t如果是小表(10條),c如果是大表(300條),那么t每循環(huán)1次,就需要循環(huán)300次,即t表的t.cid字段屬于經(jīng)常使用的字段,因此需要給cid字段添加索引。

      更深入的說明:一般情況下,左連接給左表加索引。右連接給右表加索引。其他表需不需要加索引,我們逐步嘗試。

      # 給左表的字段加索引 create index cid_teacher2 on teacher2(cid); # 查看執(zhí)行計劃 explain select * from teacher2 t left outer join course2 c on t.cid=c.cid where c.cname='java';

      1

      2

      3

      4

      5

      6

      7

      8

      9

      結(jié)果如下:

      當(dāng)然你可以下去接著優(yōu)化,給cname添加一個索引。

      索引優(yōu)化是一個逐步的過程,需要一點點嘗試。

      # 給cname的字段加索引 create index cname_course2 on course2(cname); # 查看執(zhí)行計劃 explain select t.cid,c.cname from teacher2 t left outer join course2 c on t.cid=c.cid where c.cname='java';

      1

      2

      3

      4

      5

      6

      7

      8

      9

      結(jié)果如下:

      最后補充一個:Using join buffer是extra中的一個選項,表示Mysql引擎使用了連接緩存,即MySQL底層動了你的SQL,你寫的太差了。

      大于等于2張表,優(yōu)化原則一樣;

      小表驅(qū)動大表 ;

      索引建立在經(jīng)常查詢的字段上;

      a.復(fù)合索引,不要跨列或無序使用(最佳左前綴);

      b.復(fù)合索引,盡量使用全索引匹配,也就是說,你建立幾個索引,就使用幾個索引;

      explain select * from book where authorid = 1 and typeid = 2; explain select * from book where authorid*2 = 1 and typeid = 2 ;

      1

      2

      結(jié)果如下:

      # 針對不是復(fù)合索引的情況 explain select * from book where authorid != 1 and typeid =2 ; explain select * from book where authorid != 1 and typeid !=2 ;

      1

      2

      3

      結(jié)果如下:

      再觀看下面這個案例:

      # 刪除單獨的索引 drop index authorid_index on book; drop index typeid_index on book; # 創(chuàng)建一個復(fù)合索引 alter table book add index idx_book_at (authorid,typeid); # 查看執(zhí)行計劃 explain select * from book where authorid > 1 and typeid = 2 ; explain select * from book where authorid = 1 and typeid > 2 ;

      1

      2

      3

      4

      5

      6

      7

      8

      結(jié)果如下:

      結(jié)論:復(fù)合索引中如果有【>】,則自身和右側(cè)索引全部失效。

      在看看復(fù)合索引中有【<】的情況:

      我們學(xué)習(xí)索引優(yōu)化 ,是一個大部分情況適用的結(jié)論,但由于SQL優(yōu)化器等原因 該結(jié)論不是100%正確。一般而言, 范圍查詢(> < in)之后的索引失效。

      # 刪除復(fù)合索引 drop index authorid_typeid_bid on book; # 為authorid和typeid,分別創(chuàng)建索引 create index authorid_index on book(authorid); create index typeid_index on book(typeid); # 查看執(zhí)行計劃 explain select * from book where authorid = 1 and typeid =2 ;

      1

      2

      3

      4

      5

      6

      7

      結(jié)果如下:

      結(jié)果分析:我們創(chuàng)建了兩個索引,但是實際上只使用了一個索引。因為對于兩個單獨的索引,程序覺得只用一個索引就夠了,不需要使用兩個。

      當(dāng)我們創(chuàng)建一個復(fù)合索引,再次執(zhí)行上面的SQL:

      # 查看執(zhí)行計劃 explain select * from book where authorid = 1 and typeid =2 ;

      1

      2

      結(jié)果如下:

      explain select * from teacher where tname like "%x%" ; explain select * from teacher where tname like 'x%'; explain select tname from teacher where tname like '%x%';

      1

      2

      3

      結(jié)果如下:

      結(jié)論如下:like盡量不要使用類似"%x%"情況,但是可以使用"x%"情況。如果非使用 "%x%"情況,需要使用索引覆蓋。

      explain select * from teacher where tname = 'abc' ; explain select * from teacher where tname = 123 ;

      1

      2

      結(jié)果如下:

      explain select * from teacher where tname ='' and tcid >1 ; explain select * from teacher where tname ='' or tcid >1 ;

      1

      2

      結(jié)果如下:

      注意:or很猛,會讓自身索引和左右兩側(cè)的索引都失效。

      如果主查詢的數(shù)據(jù)集大,則使用i關(guān)鍵字,效率高。

      如果子查詢的數(shù)據(jù)集大,則使用exist關(guān)鍵字,效率高。

      select ..from table where exist (子查詢) ; select ..from table where 字段 in (子查詢) ;

      1

      2

      IO就是訪問硬盤文件的次數(shù)。

      using filesort 有兩種算法:雙路排序、單路排序(根據(jù)IO的次數(shù))

      MySQL4.1之前默認使用雙路排序;雙路:掃描2次磁盤(1:從磁盤讀取排序字段

      ,對排序字段進行排序(在buffer中進行的排序)2:掃描其他字段)

      MySQL4.1之后默認使用單路排序:只讀取一次(全部字段),在buffer中進行排序。但種單路排序會有一定的隱患(不一定真的是“單路/1次IO”,有可能多次IO)。原因:如果數(shù)據(jù)量特別大,則無法將所有字段的數(shù)據(jù)一次性讀取完畢,因此會進行“分片讀取、多次讀取”。

      注意:單路排序 比雙路排序 會占用更多的buffer。

      單路排序在使用時,如果數(shù)據(jù)大,可以考慮調(diào)大buffer的容量大?。?/p>

      # 不一定真的是“單路/1次IO”,有可能多次IO set max_length_for_sort_data = 1024

      1

      2

      如果max_length_for_sort_data值太低,則mysql會自動從 單路->雙路(太低:需要排序的列的總大小超過了max_length_for_sort_data定義的字節(jié)數(shù))

      a.選擇使用單路、雙路 ;調(diào)整buffer的容量大??;

      b.避免使用select * …(select后面寫所有字段,也比寫*效率高)

      c.復(fù)合索引,不要跨列使用 ,避免using filesort

      d.保證全部的排序字段,排序的一致性(都是升序或降序)

      MySQL SQL 數(shù)據(jù)挖掘

      版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。

      版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。

      上一篇:在線文檔(4分鐘之前已更新)
      下一篇:提升企業(yè)效率的關(guān)鍵,企業(yè)流程管控
      相關(guān)文章
      亚洲综合久久成人69| 亚洲av日韩av无码黑人| 亚洲精品456在线播放| 久久久久久久综合日本亚洲| 国产福利电影一区二区三区,亚洲国模精品一区 | 亚洲伊人色欲综合网| 在线观看国产区亚洲一区成人 | 亚洲色偷偷偷网站色偷一区| 亚洲成色999久久网站| 亚洲国产成人久久精品影视| 亚洲人成网站影音先锋播放| 亚洲天天做日日做天天欢毛片| 亚洲国产天堂久久综合网站| 亚洲一区二区影院| 亚洲日韩在线视频| 亚洲av无码一区二区三区天堂古代| 亚洲乱码一二三四五六区| 在线综合亚洲中文精品| 亚洲日本一线产区和二线产区对比| 亚洲精华液一二三产区| 亚洲AV成人精品日韩一区| 亚洲精品97久久中文字幕无码| 亚洲麻豆精品国偷自产在线91| 区久久AAA片69亚洲| 国产AV无码专区亚洲AVJULIA| 久久亚洲国产欧洲精品一| 日韩亚洲Av人人夜夜澡人人爽| 亚洲黄色一级毛片| 亚洲影视自拍揄拍愉拍| 亚洲欧美一区二区三区日产| mm1313亚洲国产精品无码试看 | 亚洲毛片免费观看| 国产成人精品日本亚洲11| 亚洲精品无码少妇30P| 国产亚洲福利一区二区免费看| 国产福利电影一区二区三区,亚洲国模精品一区 | 亚洲午夜久久久久久噜噜噜| 久久久久亚洲av无码专区蜜芽| 91亚洲国产成人久久精品网站| 亚洲av永久无码精品三区在线4| 亚洲色大情网站www|