這篇文章主要介紹了通過MySQL優化Discuz!的熱帖翻頁的技巧,包括更新索引來降低服務器負載等方面,需要的朋友可以參考下
寫在前面:discuz!作為首屈一指的社區系統,為廣大站長提供了一站式網站解決方案,而且是開源的(雖然部分代碼是加密的),它為這個垂直領域的行業發展作出了巨大貢獻。盡管如此,discuz!系統源碼中,還是或多或少有些坑。其中最著名的就是默認采用MyISAM引擎,以及基于MyISAM引擎的搶樓功能,session表采用memory引擎等,可以參考后面幾篇歷史文章。本次我們要說說discuz!在應對熱們帖子翻頁邏輯功能中的另一個問題。
在我們的環境中,使用的是 MySQL-5.6.6 版本。
在查看帖子并翻頁過程中,會產生類似下面這樣的SQL:
- mysql> desc SELECT * FROM pre_forum_post WHERE
- tid=8201301 AND `invisible` IN('0','-2') ORDER BY dateline DESC LIMIT 15/G
- *************************** 1. row ***************************
- id: 1
- select_type: SIMPLE
- table: pre_forum_post
- type: ref
- possible_keys: tid,displayorder,first
- key: displayorder
- key_len: 3
- ref: const
- rows: 593371
- Extra: Using index condition; Using where; Using filesort
這個SQL執行的代價是:
-- 根據索引訪問行記錄次數,總體而言算是比較好的狀態
- | Handler_read_key | 16 |
-- 根據索引順序訪問下一行記錄的次數,通常是因為根據索引的范圍掃描,或者全索引掃描,總體而言也算是比較好的狀態
- | Handler_read_next | 329881 |
-- 按照一定順序讀取行記錄的總次數。如果需要對結果進行排序,該值通常會比較大。當發生全表掃描或者多表join無法使用索引時,該值也會比較大
- | Handler_read_rnd | 15 |
而當遇到熱帖需要往后翻很多頁時,例如:
- mysql> desc SELECT * FROM pre_forum_post WHERE
- tid=8201301 AND `invisible` IN('0','-2') ORDER BY dateline LIMIT 129860, 15/G
- *************************** 1. row ***************************
- id: 1
- select_type: SIMPLE
- table: pre_forum_post
- type: ref
- possible_keys: displayorder
- key: displayorder
- key_len: 3
- ref: const
- rows: 593371
- Extra: Using where; Using filesort
這個SQL執行的代價則變成了(可以看到Handler_read_key、Handler_read_rnd大了很多):
| Handler_read_key | 129876 | -- 因為前面需要跳過很多行記錄
| Handler_read_next | 329881 | -- 同上
| Handler_read_rnd | 129875 | -- 因為需要先對很大一個結果集進行排序
可見,遇到熱帖時,這個SQL的代價會非常高。如果該熱帖被大量的訪問歷史回復,或者被搜素引擎一直反復請求并且歷史回復頁時,很容易把數據庫服務器直接壓垮。
小結:這個SQL不能利用 `displayorder` 索引排序的原因是,索引的第二個列 `invisible` 采用范圍查詢(RANGE),導致沒辦法繼續利用聯合索引完成對 `dateline` 字段的排序需求(而如果是 WHERE tid =? AND invisible IN(?, ?) AND dateline =? 這種情況下是完全可以用到整個聯合索引的,注意下二者的區別)。
知道了這個原因,相應的優化解決辦法也就清晰了:
創建一個新的索引 idx_tid_dateline,它只包括 tid、dateline 兩個列即可(根據其他索引的統計信息,item_type 和 item_id 的基數太低,所以沒包含在聯合索引中。當然了,也可以考慮一并加上)。
我們再來看下采用新的索引后的執行計劃:
- mysql> desc SELECT * FROM pre_forum_post WHERE
- tid=8201301 AND `invisible` IN('0','-2') ORDER BY dateline LIMIT 15/G
- *************************** 1. row ***************************
- id: 1
- select_type: SIMPLE
- table: pre_forum_post
- type: ref
- possible_keys: tid,displayorder,first,idx_tid_dateline
- key: idx_tid_dateline
- key_len: 3
- ref: const
- rows: 703892
- Extra: Using where
可以看到,之前存在的 Using filesort 消失了,可以通過索引直接完成排序了。
不過,如果該熱帖翻到較舊的歷史回復時,相應的SQL還是不能使用新的索引:
- mysql> desc SELECT * FROM pre_forum_post WHERE
- tid=8201301 AND `invisible` IN('0','-2') ORDER BY dateline LIMIT 129860,15/G
- *************************** 1. row ***************************
- id: 1
- select_type: SIMPLE
- table: pre_forum_post
- type: ref
- possible_keys: tid,displayorder,first,idx_tid_dateline
- key: displayorder
- key_len: 3
- ref: const
- rows: 593371
- Extra: Using where; Using filesort
對比下如果建議優化器使用新索引的話,其執行計劃是怎樣的:
- mysql> desc SELECT * FROM pre_forum_post use index(idx_tid_dateline) WHERE
- tid=8201301 AND `invisible` IN('0','-2') ORDER BY dateline LIMIT 129860,15/G
- *************************** 1. row ***************************
- id: 1
- select_type: SIMPLE
- table: pre_forum_post
- type: ref
- possible_keys: idx_tid_dateline
- key: idx_tid_dateline
- key_len: 3
- ref: const
- rows: 703892
- Extra: Using where
可以看到,因為查詢優化器認為后者需要掃描的行數遠比前者多了11萬多,因此認為前者效率更高。
事實上,在這個例子里,排序的代價更高,因此我們要優先消除排序,所以應該強制使用新的索引,也就是采用后面的執行計劃,在相應的程序中指定索引。
最后,我們來看下熱帖翻到很老的歷史回復時,兩個執行計劃分別的profiling統計信息對比:
1、采用舊索引(displayorder):
- mysql> SELECT * FROM pre_forum_post WHERE
- tid=8201301 AND `invisible` IN('0','-2') ORDER BY dateline LIMIT 129860,15;
- #查看profiling結果
- | starting | 0.020203 |
- | checking permissions | 0.000026 |
- | Opening tables | 0.000036 |
- | init | 0.000099 |
- | System lock | 0.000092 |
- | optimizing | 0.000038 |
- | statistics | 0.000123 |
- | preparing | 0.000043 |
- | Sorting result | 0.000025 |
- | executing | 0.000023 |
- | Sending data | 0.000045 |
- | Creating sort index | 0.941434 |
- | end | 0.000077 |
- | query end | 0.000044 |
- | closing tables | 0.000038 |
- | freeing items | 0.000056 |
- | cleaning up | 0.000040 |
2、如果是采用新索引(idx_tid_dateline):
- mysql> SELECT * FROM pre_forum_post use index(idx_tid_dateline) WHERE
- tid=8201301 AND `invisible` IN('0','-2') ORDER BY dateline LIMIT 129860,15;
- #對比查看profiling結果
- | starting | 0.000151 |
- | checking permissions | 0.000033 |
- | Opening tables | 0.000040 |
- | init | 0.000105 |
- | System lock | 0.000044 |
- | optimizing | 0.000038 |
- | statistics | 0.000188 |
- | preparing | 0.000044 |
- | Sorting result | 0.000024 |
- | executing | 0.000023 |
- | Sending data | 0.917035 |
- | end | 0.000074 |
- | query end | 0.000030 |
- | closing tables | 0.000036 |
- | freeing items | 0.000049 |
- | cleaning up | 0.000032 |
可以看到,效率有了一定提高,不過不是很明顯,因為確實需要掃描的數據量更大,所以 Sending data 階段耗時更多。
這時候,我們可以再參考之前的一個優化方案:[MySQL優化案例]系列 — 分頁優化
然后可以將這個SQL改寫成下面這樣:
- mysql> EXPLAIN SELECT * FROM pre_forum_post t1 INNER JOIN (
- SELECT id FROM pre_forum_post use index(idx_tid_dateline) WHERE
- tid=8201301 AND `invisible` IN('0','-2') ORDER BY
- dateline LIMIT 129860,15) 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: 129875
- 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: pre_forum_post
- type: ref
- possible_keys: idx_tid_dateline
- key: idx_tid_dateline
- key_len: 3
- ref: const
- rows: 703892
- Extra: Using where
再看下這個SQL的 profiling 統計信息:
- | starting | 0.000209 |
- | checking permissions | 0.000026 |
- | checking permissions | 0.000026 |
- | Opening tables | 0.000101 |
- | init | 0.000062 |
- | System lock | 0.000049 |
- | optimizing | 0.000025 |
- | optimizing | 0.000037 |
- | statistics | 0.000106 |
- | preparing | 0.000059 |
- | Sorting result | 0.000039 |
- | statistics | 0.000048 |
- | preparing | 0.000032 |
- | executing | 0.000036 |
- | Sending data | 0.000045 |
- | executing | 0.000023 |
- | Sending data | 0.225356 |
- | end | 0.000067 |
- | query end | 0.000028 |
- | closing tables | 0.000023 |
- | removing tmp table | 0.000029 |
- | closing tables | 0.000044 |
- | freeing items | 0.000048 |
- | cleaning up | 0.000037 |
可以看到,效率提升了1倍以上,還是挺不錯的。
最后說明下,這個問題只會在熱帖翻頁時才會出現,一般只有1,2頁回復的帖子如果還采用原來的執行計劃,也沒什么問題。
因此,建議discuz!官方修改或增加下新索引,并且在代碼中判斷是否熱帖翻頁,是的話,就強制使用新的索引,以避免性能問題。
新聞熱點
疑難解答