MySQL中l(wèi)imit對查詢語句性能的影響
一,前言
首先說明一下MySQL的版本:
mysql> select version(); +-----------+ | version() | +-----------+ | 5.7.17 | +-----------+ 1 row in set (.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 | | | | source | int(10) unsigned | NO | | | | +--------+---------------------+------+-----+---------+----------------+ 3 rows in set (.00 sec)
id為自增主鍵,val為非唯一索引。
灌入大量數(shù)據(jù),共500萬:
mysql> select count(*) 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 (.38 sec)
時間相差很明顯。
為什么會出現(xiàn)上面的結(jié)果?我們看一下select * from test where val=4 limit 300000,5;的查詢過程:
查詢到索引葉子節(jié)點(diǎn)數(shù)據(jù)。
根據(jù)葉子節(jié)點(diǎn)上的主鍵值去聚簇索引上查詢需要的全部字段值。
類似于下面這張圖:
像上面這樣,需要查詢300005次索引節(jié)點(diǎn),查詢300005次聚簇索引的數(shù)據(jù),最后再將結(jié)果過濾掉前300000條,取出最后5條。MySQL耗費(fèi)了大量隨機(jī)I/O在查詢聚簇索引的數(shù)據(jù)上,而有300000次隨機(jī)I/O查詢到的數(shù)據(jù)是不會出現(xiàn)在結(jié)果集當(dāng)中的。
肯定會有人問:既然一開始是利用索引的,為什么不先沿著索引葉子節(jié)點(diǎn)查詢到最后需要的5個節(jié)點(diǎn),然后再去聚簇索引中查詢實(shí)際數(shù)據(jù)。這樣只需要5次隨機(jī)I/O,類似于下面圖片的過程:
其實(shí)我也想問這個問題。
證實(shí)
下面我們實(shí)際操作一下來證實(shí)上述的推論:
為了證實(shí)select * from test where val=4 limit 300000,5
是掃描300005個索引節(jié)點(diǎn)和300005個聚簇索引上的數(shù)據(jù)節(jié)點(diǎn),我們需要知道MySQL有沒有辦法統(tǒng)計在一個sql中通過索引節(jié)點(diǎn)查詢數(shù)據(jù)節(jié)點(diǎn)的次數(shù)。我先試了Handler_read_*系列,很遺憾沒有一個變量能滿足條件。
我只能通過間接的方式來證實(shí):
InnoDB中有buffer pool。里面存有最近訪問過的數(shù)據(jù)頁,包括數(shù)據(jù)頁和索引頁。所以我們需要運(yùn)行兩個sql,來比較buffer pool中的數(shù)據(jù)頁的數(shù)量。預(yù)測結(jié)果是運(yùn)行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ù)量,因?yàn)榍耙粋€sql只訪問5次數(shù)據(jù)頁,而后一個sql訪問300005次數(shù)據(jù)頁。
mysql> select index_name,count(*) from information_schema.INNODB_BUFFER_PAGE where INDEX_NAME in('val','primary') and TABLE_NAME like '%test%' group by index_name; Empty set (.04 sec)
可以看出,目前buffer pool中沒有關(guān)于test表的數(shù)據(jù)頁。
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 (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%' group by index_name; +------------+----------+ | index_name | count(*) | +------------+----------+ | PRIMARY | 4098 | | val | 208 | +------------+----------+ 2 rows in set (.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)
為了防止上次試驗(yàn)的影響,我們需要清空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%' group by index_name; Empty set (0.03 sec)
運(yùn)行SQL:
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.09 sec) mysql> select index_name,count(*) from information_schema.INNODB_BUFFER_PAGE where INDEX_NAME in('val','primary') and TABLE_NAME like '%test%' group by index_name; +------------+----------+ | index_name | count(*) | +------------+----------+ | PRIMARY | 5 | | val | 390 | +------------+----------+ 2 rows in set (0.03 sec)
我們可以看明顯的看出兩者的差別:第一個sql加載了4098個數(shù)據(jù)頁到buffer pool,而第二個sql只加載了5個數(shù)據(jù)頁到buffer pool。符合我們的預(yù)測。也證實(shí)了為什么第一個sql會慢:讀取大量的無用數(shù)據(jù)行(300000),最后卻拋棄掉。
而且這會造成一個問題:加載了很多熱點(diǎn)不是很高的數(shù)據(jù)頁到buffer pool,會造成buffer pool的污染,占用buffer pool的空間。
遇到的問題
為了在每次重啟時確保清空buffer pool,我們需要關(guān)閉innodb_buffer_pool_dump_at_shutdown和innodb_buffer_pool_load_at_startup,這兩個選項(xiàng)能夠控制數(shù)據(jù)庫關(guān)閉時dump出buffer pool中的數(shù)據(jù)和在數(shù)據(jù)庫開啟時載入在磁盤上備份buffer pool的數(shù)據(jù)。
參考資料:
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
更多關(guān)于SQL執(zhí)行效率問題請查看下面的相關(guān)文章
相關(guān)文章
MySQL 5.7 mysql command line client 使用命令詳解
這篇文章主要介紹了MySQL 5.7 mysql command line client 使用命令,需要的朋友可以參考下2017-06-06myeclipse中連接mysql數(shù)據(jù)庫示例代碼
這篇文章主要為大家詳細(xì)介紹了MyEclipse連接MySQL數(shù)據(jù)庫圖文教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10解決MySQL因不能創(chuàng)建臨時文件而導(dǎo)致無法啟動的方法
這篇文章主要跟大家介紹了關(guān)于解決MySQL因不能創(chuàng)建臨時文件而導(dǎo)致無法啟動的方法,文中通過示例代碼介紹了詳細(xì)的解決方法,對大家具有一定的的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-06-06實(shí)例解析MySQL中的存儲過程及存儲過程的調(diào)用方法
存儲例程是存儲在數(shù)據(jù)庫服務(wù)器中的一組sql語句,通過在查詢中調(diào)用一個指定的名稱來執(zhí)行這些sql語句命令,下面就以實(shí)例解析MySQL中的存儲過程及存儲過程的調(diào)用方法:2016-05-05MySQL Index Condition Pushdown(ICP)性能優(yōu)化方法實(shí)例
這篇文章主要介紹了MySQL Index Condition Pushdown(ICP)性能優(yōu)化方法實(shí)例,本文講解了概念介紹、原理、實(shí)踐案例、案例分析、ICP的使用限制等內(nèi)容,需要的朋友可以參考下2015-05-05MySql 緩存查詢原理與緩存監(jiān)控和索引監(jiān)控介紹
這篇文章主要介紹了MySql 緩存查詢原理與緩存監(jiān)控和索引監(jiān)控,需要的朋友可以參考下2021-07-07