Mysql基礎(二十):mysql效能優化(五)explain 使用
先來一張表:
CREATE TABLE IF NOT EXISTS `article` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT, `author_id` int(10) unsigned NOT NULL, `category_id` int(10) unsigned NOT NULL, `views` int(10) unsigned NOT NULL, `comments` int(10) unsigned NOT NULL, `title` varbinary(255) NOT NULL, `content` text NOT NULL, PRIMARYKEY (`id`) );
再插幾條資料:
INSERT INTO `article` (`author_id`, `category_id`, `views`, `comments`, `title`, `content`) VALUES (1, 1, 1, 1, '1', '1'), (2, 2, 2, 2, '2', '2'), (1, 1, 3, 3, '3', '3');
需求:
查詢 category_id 為 1 且 comments 大於 1 的情況下,views 最多的 article_id。
先查查試試看:
EXPLAIN SELECT author_id FROM `article`WHERE category_id = 1 AND comments > 1 ORDER BY views DESC LIMIT 1\G
看看部分輸出結果:
*************************** 1. row *************************** id: 1 select_type: SIMPLE table: article type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 3 Extra: Using where; Using filesort1 row in set (0.00 sec)
很顯然,type 是 ALL,即最壞的情況。Extra 裡還出現了 Using filesort,也是最壞的情況。優化是必須的。
嗯,那麼最簡單的解決方案就是加索引了。好,我們來試一試。查詢的條件裡即 where 之後共使用了 category_id,comments,views 三個欄位。那麼來一個聯合索引是最簡單的了。
ALTER TABLE `article` ADD INDEX x ( `category_id` , `comments`, `views` );
結果有了一定好轉,但仍然很糟糕:
*************************** 1. row *************************** id: 1 select_type: SIMPLE table: article type: range possible_keys: x key: x key_len: 8 ref: NULL rows: 1 Extra: Using where; Using filesort 1 row in set (0.00 sec)
type 變成了 range,這是可以忍受的。但是 extra 裡使用 Using filesort 仍是無法接受的。但是我們已經建立了索引,為啥沒用呢?這是因為按照 BTree 索引的工作原理,先排序 category_id,如果遇到相同的 category_id 則再排序 comments,如果遇到相同的 comments 則再排序 views。當 comments 欄位在聯合索引裡處於中間位置時,因comments > 1 條件是一個範圍值(所謂 range),MySQL 無法利用索引再對後面的 views 部分進行檢索,即 range 型別查詢欄位後面的索引無效。
那麼我們需要拋棄 comments,刪除舊索引:
DROP INDEX x ON article;
然後建立新索引:
ALTER TABLE `article` ADD INDEX y ( `category_id` , `views` ) ;
接著再執行查詢:
*************************** 1. row *************************** id: 1 select_type: SIMPLE table: article type: ref possible_keys: y key: y key_len: 4 ref: const rows: 1 Extra: Using where 1 row in set (0.00 sec)
可以看到,type 變為了 ref,Extra 中的 Using filesort 也消失了,結果非常理想。
再來看一個多表查詢的例子。
首先定義 3個表 class 和 room。
CREATE TABLE IF NOT EXISTS `class` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `card` int(10) unsigned NOT NULL, PRIMARY KEY (`id`) ); CREATE TABLE IF NOT EXISTS `book` ( `bookid` int(10) unsigned NOT NULL AUTO_INCREMENT, `card` int(10) unsigned NOT NULL, PRIMARY KEY (`bookid`) ); CREATE TABLE IF NOT EXISTS `phone` ( `phoneid` int(10) unsigned NOT NULL AUTO_INCREMENT, `card` int(10) unsigned NOT NULL, PRIMARY KEY (`phoneid`) ) engine = innodb;
然後再分別插入大量資料。插入資料的php指令碼:
<?php $link = mysql_connect("localhost","root","870516"); mysql_select_db("test",$link); for($i=0;$i<10000;$i++) { $j = rand(1,20); $sql = " insert into class(card) values({$j})"; mysql_query($sql); } for($i=0;$i<10000;$i++) { $j = rand(1,20); $sql = " insert into book(card) values({$j})"; mysql_query($sql); } for($i=0;$i<10000;$i++) { $j = rand(1,20); $sql = " insert into phone(card) values({$j})"; mysql_query($sql); } mysql_query("COMMIT"); ?>
然後來看一個左連線查詢:
explain select * from class left join book on class.card = book.card\G
分析結果是:
*************************** 1. row *************************** id: 1 select_type: SIMPLE table: class type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 20000 Extra: *************************** 2. row *************************** id: 1 select_type: SIMPLE table: book type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 20000 Extra: 2 rows in set (0.00 sec)
顯然第二個 ALL 是需要我們進行優化的。
建立個索引試試看:
ALTER TABLE `book` ADD INDEX y ( `card`);
*************************** 1. row *************************** id: 1 select_type: SIMPLE table: class type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 20000 Extra: *************************** 2. row *************************** id: 1 select_type: SIMPLE table: book type: ref possible_keys: y key: y key_len: 4 ref: test.class.card rows: 1000 Extra: 2 rows in set (0.00 sec)
可以看到第二行的 type 變為了 ref,rows 也變成了 1741*18,優化比較明顯。這是由左連線特性決定的。LEFT JOIN 條件用於確定如何從右表搜尋行,左邊一定都有,所以右邊是我們的關鍵點,一定需要建立索引。
刪除舊索引:
DROP INDEX y ON book;
建立新索引。
ALTER TABLE `class` ADD INDEX x ( `card`);
結果
*************************** 1. row *************************** id: 1 select_type: SIMPLE table: class type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 20000 Extra: *************************** 2. row *************************** id: 1 select_type: SIMPLE table: book type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 20000 Extra: 2 rows in set (0.00 sec)
基本無變化。
然後來看一個右連線查詢:
explain select * from class right join book on class.card = book.card;
分析結果是:
*************************** 1. row *************************** id: 1 select_type: SIMPLE table: book type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 20000 Extra: *************************** 2. row *************************** id: 1 select_type: SIMPLE table: class type: ref possible_keys: x key: x key_len: 4 ref: test.book.card rows: 1000 Extra: 2 rows in set (0.00 sec)
優化較明顯。這是因為 RIGHT JOIN 條件用於確定如何從左表搜尋行,右邊一定都有,所以左邊是我們的關鍵點,一定需要建立索引。
刪除舊索引:
DROP INDEX x ON class;
建立新索引。
ALTER TABLE `book` ADD INDEX y ( `card`);
結果
*************************** 1. row *************************** id: 1 select_type: SIMPLE table: class type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 20000 Extra: *************************** 2. row *************************** id: 1 select_type: SIMPLE table: book type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 20000 Extra: 2 rows in set (0.00 sec)
基本無變化。
最後來看看 inner join 的情況:
複製程式碼程式碼如下:explain select * from class inner join book on class.card = book.card;
結果:
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: book
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 20000
Extra:
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: class
type: ref
possible_keys: x
key: x
key_len: 4
ref: test.book.card
rows: 1000
Extra:
2 rows in set (0.00 sec)
刪除舊索引:
DROP INDEX y ON book;
結果
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: class
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 20000
Extra:
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: book
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 20000
Extra:
2 rows in set (0.00 sec)
建立新索引。
ALTER TABLE `class` ADD INDEX x ( `card`);
結果
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: class
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 20000
Extra:
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: book
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 20000
Extra:
2 rows in set (0.00 sec)
綜上所述,inner join 和 left join 差不多,都需要優化右表。而 right join 需要優化左表。
我們再來看看三表查詢的例子
新增一個新索引:
複製程式碼程式碼如下:ALTER TABLE `phone` ADD INDEX z ( `card`);
ALTER TABLE `book` ADD INDEX y ( `card`); 複製程式碼程式碼如下:
explain select * from class left join book on class.card=book.card left join phone on book.card = phone.card; 複製程式碼程式碼如下:
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: class
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 20000
Extra:
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: book
type: ref
possible_keys: y
key: y
key_len: 4
ref: test.class.card
rows: 1000
Extra:
*************************** 3. row ***************************
id: 1
select_type: SIMPLE
table: phone
type: ref
possible_keys: z
key: z
key_len: 4
ref: test.book.card
rows: 260
Extra: Using index
3 rows in set (0.00 sec)
後 2 行的 type 都是 ref 且總 rows 優化很好,效果不錯。
MySql 中的 explain 語法可以幫助我們改寫查詢,優化表的結構和索引的設定,從而最大地提高查詢效率。當然,在大規模資料量時,索引的建立和維護的代價也是很高的,往往需要較長的時間和較大的空間,如果在不同的列組合上建立索引,空間的開銷會更大。因此索引最好設定在需要經常查詢的欄位中。