一次 SQL 查詢優(yōu)化原理分析(900W+ 數(shù)據(jù),從 17s 到 300ms)
點擊關(guān)注上方“SQL數(shù)據(jù)庫開發(fā)”,
設(shè)為“置頂或星標(biāo)”,第一時間送達(dá)干貨
來源:Muscleape
jianshu.com/p/0768ebc4e28d
操作:?查詢條件放到子查詢中,子查詢只查主鍵ID,然后使用子查詢中確定的主鍵關(guān)聯(lián)查詢其他的屬性字段;
原理:?減少回表操作;
-- 優(yōu)化前SQL
SELECT??各種字段
FROM`table_name`
WHERE?各種條件
LIMIT0,10;-- 優(yōu)化后SQL
SELECT??各種字段
FROM`table_name`?main_tale
RIGHTJOIN
(
SELECT??子查詢只查主鍵
FROM`table_name`
WHERE?各種條件
LIMIT0,10;
) temp_table ON temp_table.主鍵 = main_table.主鍵找到的原理分析:MySQL 用 limit 為什么會影響性能?
一,前言
首先說明一下MySQL的版本:
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.17????|
+-----------+
1?row?in?set?(0.00?sec)表結(jié)構(gòu):
mysql> desc test;
+--------+---------------------+------+-----+---------+----------------+
| Field |?Type | Null |?Key | Default |?Extra |
+--------+---------------------+------+-----+---------+----------------+
|?id | bigint(20) unsigned |?NO | PRI |?NULL | auto_increment |
| val |?int(10) unsigned | NO |?MUL | 0 |????????????????|
|?source | int(10) unsigned |?NO | |?0???????| |
+--------+---------------------+------+-----+---------+----------------+
3?rows in?set (0.00?sec)id為自增主鍵,val為非唯一索引。
灌入大量數(shù)據(jù),共500萬:
mysql> selectcount(*) from test;
+----------+
| count(*) |
+----------+
| 5242882?|
+----------+
1?row?in?set?(4.25?sec)我們知道,當(dāng)limit offset rows中的offset很大時,會出現(xiàn)效率問題:
mysql> select * from test where val=4?limit 300000,5;
+---------+-----+--------+
| id |?val | source |
+---------+-----+--------+
| 3327622 |???4?| 4 |
| 3327632 |???4?| 4 |
| 3327642 |???4?| 4 |
| 3327652 |???4?| 4 |
| 3327662 |???4?| 4 |
+---------+-----+--------+
5?rows in?set (15.98?sec)為了達(dá)到相同的目的,我們一般會改寫成如下語句:
mysql> select * from test a
inner join
(select id from test
where val=4?
limit 300000,5) b on a.id=b.id;
+---------+-----+--------+---------+
| id |?val | source |?id |
+---------+-----+--------+---------+
|?3327622?| 4 |?4?| 3327622 |
| 3327632 |?4?| 4 |?3327632?|
|?3327642?| 4 |?4?| 3327642 |
| 3327652 |?4?| 4 |?3327652?|
|?3327662?| 4 |?4?| 3327662 |
+---------+-----+--------+---------+
5?rows in?set (0.38?sec)時間相差很明顯。
為什么會出現(xiàn)上面的結(jié)果?我們看一下select * from test where val=4 limit 300000,5;的查詢過程:
查詢到索引葉子節(jié)點數(shù)據(jù)。根據(jù)葉子節(jié)點上的主鍵值去聚簇索引上查詢需要的全部字段值。
類似于下面這張圖:

像上面這樣,需要查詢300005次索引節(jié)點,查詢300005次聚簇索引的數(shù)據(jù),最后再將結(jié)果過濾掉前300000條,取出最后5條。MySQL耗費了大量隨機I/O在查詢聚簇索引的數(shù)據(jù)上,而有300000次隨機I/O查詢到的數(shù)據(jù)是不會出現(xiàn)在結(jié)果集當(dāng)中的。
肯定會有人問:既然一開始是利用索引的,為什么不先沿著索引葉子節(jié)點查詢到最后需要的5個節(jié)點,然后再去聚簇索引中查詢實際數(shù)據(jù)。這樣只需要5次隨機I/O,類似于下面圖片的過程:

其實我也想問這個問題。
證實
select * from test where val=4 limit 300000,5是掃描300005個索引節(jié)點和300005個聚簇索引上的數(shù)據(jù)節(jié)點,我們需要知道MySQL有沒有辦法統(tǒng)計在一個sql中通過索引節(jié)點查詢數(shù)據(jù)節(jié)點的次數(shù)。我先試了Handler_read_*系列,很遺憾沒有一個變量能滿足條件。select * from test a inner join (select id from test where val=4 limit 300000,5);?之后,buffer pool中的數(shù)據(jù)頁的數(shù)量遠(yuǎn)遠(yuǎn)少于select * from test where val=4 limit 300000,5;對應(yīng)的數(shù)量,因為前一個sql只訪問5次數(shù)據(jù)頁,而后一個sql訪問300005次數(shù)據(jù)頁。select?* from?test?
where?val=4?
limit?300000,5mysql> select index_name,count(*) from information_schema.INNODB_BUFFER_PAGE
where INDEX_NAME in('val','primary') and TABLE_NAME like'%test%'
groupby index_name;
Empty set?(0.04 sec)可以看出,目前buffer pool中沒有關(guān)于test表的數(shù)據(jù)頁。
mysql> select * fromtestwhere val=4limit300000,5;
+---------+-----+--------+
| id |?val | source |
+---------+-----+--------+
| 3327622 |???4?| 4 |
| 3327632 |???4?| 4 |
| 3327642 |???4?| 4 |
| 3327652 |???4?| 4 |
| 3327662 |???4?| 4 |
+---------+-----+--------+
5?rows in?set (26.19?sec)
mysql> select index_name,count(*) from information_schema.INNODB_BUFFER_PAGE where INDEX_NAME in('val','primary') and?TABLE_NAME like'%test%'groupby index_name;
+------------+----------+
| index_name |?count(*) |
+------------+----------+
|?PRIMARY | 4098 |
| val |??????208?|
+------------+----------+2 rows in?set (0.04 sec)可以看出,此時buffer pool中關(guān)于test表有4098個數(shù)據(jù)頁,208個索引頁。
select * from test a inner join (select id from test where val=4 limit 300000,5) ;為了防止上次試驗的影響,我們需要清空buffer pool,重啟mysql。
mysqladmin shutdown
/usr/local/bin/mysqld_safe &mysql> select index_name,count(*) from information_schema.INNODB_BUFFER_PAGE where?INDEX_NAME in('val','primary') and TABLE_NAME like'%test%'groupby index_name;
Empty set?(0.03 sec)運行sql:
mysql> select * fromtest a innerjoin (selectidfromtestwhere val=4limit300000,5) b on a.id=b.id;
+---------+-----+--------+---------+
| id |?val | source |?id |
+---------+-----+--------+---------+
|?3327622?| 4 |?4?| 3327622 |
| 3327632 |?4?| 4 |?3327632?|
|?3327642?| 4 |?4?| 3327642 |
| 3327652 |?4?| 4 |?3327652?|
|?3327662?| 4 |?4?| 3327662 |
+---------+-----+--------+---------+
5?rows in?set (0.09 sec)
mysql> select index_name,count(*) from information_schema.INNODB_BUFFER_PAGE where INDEX_NAME in('val','primary') and?TABLE_NAME like'%test%'groupby index_name;
+------------+----------+
| index_name |?count(*) |
+------------+----------+
|?PRIMARY | 5 |
| val |????390?|
+------------+----------+
2 rows in?set (0.03 sec)參考資料
1.https://explainextended.com/2009/10/23/mysql-order-by-limit-performance-late-row-lookups/
2.https://dev.mysql.com/doc/refman/5.7/en/innodb-information-schema-buffer-pool-tables.html
我是岳哥,最后給大家分享我寫的SQL兩件套:《SQL基礎(chǔ)知識第二版》和《SQL高級知識第二版》的PDF電子版。里面有各個語法的解釋、大量的實例講解和批注等等,非常通俗易懂,方便大家跟著一起來實操。
有需要的讀者可以下載學(xué)習(xí),在下面的公眾號「數(shù)據(jù)前線」(非本號)后臺回復(fù)關(guān)鍵字:SQL,就行
數(shù)據(jù)前線 ——End——
后臺回復(fù)關(guān)鍵字:1024,獲取一份精心整理的技術(shù)干貨
后臺回復(fù)關(guān)鍵字:進(jìn)群,帶你進(jìn)入高手如云的交流群。
推薦閱讀

