一般,咱們會採用ORDER BY LIMIT start, offset 的方式來進行分頁查詢。例以下面這個SQL:mysql
SELECT * FROM `t1` WHERE ftype=1 ORDER BY id DESC LIMIT 100, 10;
或者像下面這個不帶任何條件的分頁SQL:算法
SELECT * FROM `t1` ORDER BY id DESC LIMIT 100, 10;
通常而言,分頁SQL的耗時隨着 start 值的增長而急劇增長,咱們來看下面這2個不一樣起始值的分頁SQL執行耗時:sql
yejr@imysql.com> SELECT * FROM `t1` WHERE ftype=1 ORDER BY id DESC LIMIT 500, 10; … 10 rows in set (0.05 sec) yejr@imysql.com> SELECT * FROM `t1` WHERE ftype=6 ORDER BY id DESC LIMIT 935500, 10; … 10 rows in set (2.39 sec)
能夠看到,隨着分頁數量的增長,SQL查詢耗時也有數十倍增長,顯然不科學。今天咱們就來分析下,如何能優化這個分頁方案。 通常滴,想要優化分頁的終極方案就是:沒有分頁,哈哈哈~~~,不要說我講廢話,確實如此,能夠把分頁算法交給Sphinx、Lucence等第三方解決方案,不必讓MySQL來作它不擅長的事情。 固然了,有小夥伴說,用第三方太麻煩了,咱們就想用MySQL來作這個分頁,咋辦呢?莫急,且待咱們慢慢分析,先看下錶DDL、數據量、查詢SQL的執行計劃等信息:測試
yejr@imysql.com> SHOW CREATE TABLE `t1`; CREATE TABLE `t1` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, ... `ftype` tinyint(3) unsigned NOT NULL, ... PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; yejr@imysql.com> select count(*) from t1; +----------+ | count(*) | +----------+ | 994584 | +----------+ yejr@imysql.com> EXPLAIN SELECT * FROM `t1` WHERE ftype=1 ORDER BY id DESC LIMIT 500, 10\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: t1 type: index possible_keys: NULL key: PRIMARY key_len: 4 ref: NULL rows: 510 Extra: Using where yejr@imysql.com> EXPLAIN SELECT * FROM `t1` WHERE ftype=1 ORDER BY id DESC LIMIT 935500, 10\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: t1 type: index possible_keys: NULL key: PRIMARY key_len: 4 ref: NULL rows: 935510 Extra: Using where
能夠看到,雖然經過主鍵索引進行掃描了,但第二個SQL須要掃描的記錄數太大了,並且須要先掃描約935510條記錄,而後再根據排序結果取10條記錄,這確定是很是慢了。 針對這種狀況,咱們的優化思路就比較清晰了,有兩點:優化
一、儘量從索引中直接獲取數據,避免或減小直接掃描行數據的頻率 二、儘量減小掃描的記錄數,也就是先肯定起始的範圍,再日後取N條記錄便可
據此,咱們有兩種相應的改寫方法:子查詢、錶鏈接,即下面這樣的:排序
#採用子查詢的方式優化,在子查詢裏先從索引獲取到最大id,而後倒序排,再取10行結果集 #注意這裏採用了2次倒序排,所以在取LIMIT的start值時,比原來的值加了10,即935510,不然結果將和原來的不一致 yejr@imysql.com> EXPLAIN SELECT * FROM (SELECT * FROM `t1` WHERE id > ( SELECT id FROM `t1` WHERE ftype=1 ORDER BY id DESC LIMIT 935510, 1) LIMIT 10) t ORDER BY id DESC\G *************************** 1. row *************************** id: 1 select_type: PRIMARY table: <derived2> type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 10 Extra: Using filesort *************************** 2. row *************************** id: 2 select_type: DERIVED table: t1 type: ALL possible_keys: PRIMARY key: NULL key_len: NULL ref: NULL rows: 973192 Extra: Using where *************************** 3. row *************************** id: 3 select_type: SUBQUERY table: t1 type: index possible_keys: NULL key: PRIMARY key_len: 4 ref: NULL rows: 935511 Extra: Using where #採用INNER JOIN優化,JOIN子句裏也優先從索引獲取ID列表,而後直接關聯查詢得到最終結果,這裏不須要加10 yejr@imysql.com> EXPLAIN SELECT * FROM `t1` INNER JOIN ( SELECT id FROM `t1` WHERE ftype=1 ORDER BY id DESC LIMIT 935500,10) t2 USING (id)\G *************************** 1. row *************************** id: 1 select_type: PRIMARY table: <derived2> type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 935510 Extra: NULL *************************** 2. row *************************** id: 1 select_type: PRIMARY table: t1 type: eq_ref possible_keys: PRIMARY key: PRIMARY key_len: 4 ref: t2.id rows: 1 Extra: NULL *************************** 3. row *************************** id: 2 select_type: DERIVED table: t1 type: index possible_keys: NULL key: PRIMARY key_len: 4 ref: NULL rows: 973192 Extra: Using where
而後咱們來對比下這2個優化後的新SQL執行時間:索引
yejr@imysql.com> SELECT * FROM (SELECT * FROM `t1` WHERE id > ( SELECT id FROM `t1` WHERE ftype=1 ORDER BY id DESC LIMIT 935510, 1) LIMIT 10) T ORDER BY id DESC; ... rows in set (1.86 sec) #採用子查詢優化,從profiling的結果來看,相比原來的那個SQL快了:28.2% yejr@imysql.com> SELECT * FROM `t1` INNER JOIN ( SELECT id FROM `t1` WHERE ftype=1 ORDER BY id DESC LIMIT 935500,10) t2 USING (id); ... 10 rows in set (1.83 sec) #採用INNER JOIN優化,從profiling的結果來看,相比原來的那個SQL快了:30.8%
咱們再來看一個不帶過濾條件的分頁SQL對比:get
#原始SQL yejr@imysql.com> EXPLAIN SELECT * FROM `t1` ORDER BY id DESC LIMIT 935500, 10\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: t1 type: index possible_keys: NULL key: PRIMARY key_len: 4 ref: NULL rows: 935510 Extra: NULL yejr@imysql.com> SELECT * FROM `t1` ORDER BY id DESC LIMIT 935500, 10; ... 10 rows in set (2.22 sec) #採用子查詢優化 yejr@imysql.com> EXPLAIN SELECT * FROM (SELECT * FROM `t1` WHERE id > ( SELECT id FROM `t1` ORDER BY id DESC LIMIT 935510, 1) LIMIT 10) t ORDER BY id DESC; *************************** 1. row *************************** id: 1 select_type: PRIMARY table: <derived2> type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 10 Extra: Using filesort *************************** 2. row *************************** id: 2 select_type: DERIVED table: t1 type: ALL possible_keys: PRIMARY key: NULL key_len: NULL ref: NULL rows: 973192 Extra: Using where *************************** 3. row *************************** id: 3 select_type: SUBQUERY table: t1 type: index possible_keys: NULL key: PRIMARY key_len: 4 ref: NULL rows: 935511 Extra: Using index yejr@imysql.com> SELECT * FROM (SELECT * FROM `t1` WHERE id > ( SELECT id FROM `t1` ORDER BY id DESC LIMIT 935510, 1) LIMIT 10) t ORDER BY id DESC; … 10 rows in set (2.01 sec) #採用子查詢優化,從profiling的結果來看,相比原來的那個SQL快了:10.6% #採用INNER JOIN優化 yejr@imysql.com> EXPLAIN SELECT * FROM `t1` INNER JOIN ( SELECT id FROM `t1`ORDER BY id DESC LIMIT 935500,10) t2 USING (id)\G *************************** 1. row *************************** id: 1 select_type: PRIMARY table: type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 935510 Extra: NULL *************************** 2. row *************************** id: 1 select_type: PRIMARY table: t1 type: eq_ref possible_keys: PRIMARY key: PRIMARY key_len: 4 ref: t1.id rows: 1 Extra: NULL *************************** 3. row *************************** id: 2 select_type: DERIVED table: t1 type: index possible_keys: NULL key: PRIMARY key_len: 4 ref: NULL rows: 973192 Extra: Using index yejr@imysql.com> SELECT * FROM `t1` INNER JOIN ( SELECT id FROM `t1`ORDER BY id DESC LIMIT 935500,10) t2 USING (id); … 10 rows in set (1.70 sec) #採用INNER JOIN優化,從profiling的結果來看,相比原來的那個SQL快了:30.2%
至此,咱們看到採用子查詢或者INNER JOIN進行優化後,都有大幅度的提高,這個方法也一樣適用於較小的分頁,雖然LIMIT開始的 start 位置小了不少,SQL執行時間也快了不少,但採用這種方法後,帶WHERE條件的分頁分別能提升查詢效率:24.9%、156.5%,不帶WHERE條件的分頁分別提升查詢效率:554.5%、11.7%,各位能夠自行進行測試驗證。單從提高比例說,仍是挺可觀的,確保這些優化方法能夠適用於各類分頁模式,就能夠從一開始就是用。 咱們來看下各類場景相應的提高比例是多少:io
大分頁,帶WHERE | 大分頁,不帶WHERE | 大分頁平均提高比例 | 小分頁,帶WHERE | 小分頁,不帶WHERE | 整體平均提高比例 | |
子查詢優化 | 28.20% | 10.60% | 19.40% | 24.90% | 554.40% | 154.53% |
INNER JOIN優化 | 30.80% | 30.20% | 30.50% | 156.50% | 11.70% | 57.30% |
結論:這樣看就和明顯了,尤爲是針對大分頁的狀況,所以咱們優先推薦使用INNER JOIN方式優化分頁算法。table
上述每次測試都重啓mysqld實例,而且加了SQL_NO_CACHE,以保證每次都是直接數據文件或索引文件中讀取。若是數據通過預熱後,查詢效率會必定程度提高,但但上述相應的效率提高比例仍是基本一致的。
2014/07/28後記更新:
其實若是是不帶任何條件的分頁,就不必用這麼麻煩的方法了,能夠採用對主鍵採用範圍檢索的方法,例如參考這篇:Advance for MySQL Pagination