mysql常見錯誤詳解
MySQL 在近幾年仍然保持強勁的(de)數據庫流行度增長(cháng)趨勢。越來(lái)越多(duō)的(de)客戶将自己的(de)小程序開發應用(yòng)建立在 MySQL 數據庫之上,甚至是從 Oracle 遷移到 MySQL上來(lái)。但也(yě)存在部分(fēn)客戶在使用(yòng) MySQL 數據庫的(de)過程中遇到一些比如響應時(shí)間慢(màn),CPU 打滿等情況。
流量限制(rate-limiting),是Nginx中一個(gè)非常實用(yòng),卻經常被錯誤理(lǐ)解和(hé)錯誤配置的(de)功能。我們可(kě)以用(yòng)來(lái)限制用(yòng)戶在給定時(shí)間内HTTP請求的(de)數量。請求,可(kě)以是一個(gè)簡單網站首
1. LIMIT 語句
分(fēn)頁查詢是最常用(yòng)的(de)場(chǎng)景之一,但也(yě)通(tōng)常也(yě)是最容易出問題的(de)地方。比如對(duì)于下(xià)面簡單的(de)語句,一般DBA想到的(de)辦法是在type, name, create_time字段上加組合索引。這(zhè)樣條件排序都能有效的(de)利用(yòng)到索引,性能迅速提升。
SELECT *
FROM operation
WHERE type = 'SQLStats'
AND name = 'SlowLog'
ORDER BY create_time
LIMIT 1000, 10;
好吧,可(kě)能90%以上的(de)DBA解決該問題就到此爲止。但當 LIMIT 子句變成 “LIMIT 1000000,10” 時(shí),程序員(yuán)仍然會抱怨:我隻取10條記錄爲什(shén)麽還(hái)是慢(màn)?
要知道數據庫也(yě)并不知道第1000000條記錄從什(shén)麽地方開始,即使有索引也(yě)需要從頭計算(suàn)一次。出現這(zhè)種性能問題,多(duō)數情形下(xià)是程序員(yuán)偷懶了(le)。在前端數據浏覽翻頁,或者大(dà)數據分(fēn)批導出等場(chǎng)景下(xià),是可(kě)以将上一頁的(de)最大(dà)值當成參數作爲查詢條件的(de)。SQL重新設計如下(xià):
SELECT *
FROM operation
WHERE type = 'SQLStats'
AND name = 'SlowLog'
AND create_time > '2017-03-16 14:00:00'
ORDER BY create_time limit 10;
在新設計下(xià)查詢時(shí)間基本固定,不會随著(zhe)數據量的(de)增長(cháng)而發生變化(huà)。
2. 隐式轉換
SQL語句中查詢變量和(hé)字段定義類型不匹配是另一個(gè)常見的(de)錯誤。比如下(xià)面的(de)語句:
mysql> explain extended SELECT *
> FROM my_balance b
> WHERE b.bpn = 14000000123
> AND b.isverified IS NULL ;
mysql> show warnings;
| Warning | 1739 | Cannot use ref access on index 'bpn' due to type or collation conversion on field 'bpn'
其中字段bpn的(de)定義爲varchar(20),MySQL的(de)策略是将字符串轉換爲數字之後再比較。函數作用(yòng)于表字段,索引失效。
上述情況可(kě)能是應用(yòng)程序框架自動填入的(de)參數,而不是程序員(yuán)的(de)原意。現在應用(yòng)框架很多(duō)很繁雜(zá),使用(yòng)方便的(de)同時(shí)也(yě)小心它可(kě)能給自己挖坑。
3. 關聯更新、删除
雖然MySQL5.6引入了(le)物(wù)化(huà)特性,但需要特别注意它目前僅僅針對(duì)查詢語句的(de)優化(huà)。對(duì)于更新或删除需要手工重寫成JOIN。
比如下(xià)面UPDATE語句,MySQL實際執行的(de)是循環/嵌套子查詢(DEPENDENT SUBQUERY),其執行時(shí)間可(kě)想而知。
UPDATE operation o
SET status = 'applying'
WHERE o.id IN (SELECT id
FROM (SELECT o.id,
o.status
FROM operation o
WHERE o.group = 123
AND o.status NOT IN ( 'done' )
ORDER BY o.parent,
o.id
LIMIT 1) t);
執行計劃:
+----+--------------------+-------+-------+---------------+---------+---------+-------+------+-----------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+-------+-------+---------------+---------+---------+-------+------+-----------------------------------------------------+
| 1 | PRIMARY | o | index | | PRIMARY | 8 | | 24 | Using where; Using temporary |
| 2 | DEPENDENT SUBQUERY | | | | | | | | Impossible WHERE noticed after reading const tables |
| 3 | DERIVED | o | ref | idx_2,idx_5 | idx_5 | 8 | const | 1 | Using where; Using filesort |
+----+--------------------+-------+-------+---------------+---------+---------+-------+------+-----------------------------------------------------+
重寫爲JOIN之後,子查詢的(de)選擇模式從DEPENDENT SUBQUERY變成DERIVED,執行速度大(dà)大(dà)加快(kuài),從7秒降低到2毫秒。
UPDATE operation o
JOIN (SELECT o.id,
o.status
FROM operation o
WHERE o.group = 123
AND o.status NOT IN ( 'done' )
ORDER BY o.parent,
o.id
LIMIT 1) t
ON o.id = t.id
SET status = 'applying'
執行計劃簡化(huà)爲:
+----+-------------+-------+------+---------------+-------+---------+-------+------+-----------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+-------+---------+-------+------+-----------------------------------------------------+
| 1 | PRIMARY | | | | | | | | Impossible WHERE noticed after reading const tables |
| 2 | DERIVED | o | ref | idx_2,idx_5 | idx_5 | 8 | const | 1 | Using where; Using filesort |
+----+-------------+-------+------+---------------+-------+---------+-------+------+-----------------------------------------------------+
4. 混合排序
MySQL不能利用(yòng)索引進行混合排序。但在某些場(chǎng)景,還(hái)是有機會使用(yòng)特殊方法提升性能的(de)。
SELECT *
FROM my_order o
INNER JOIN my_appraise a ON a.orderid = o.id
ORDER BY a.is_reply ASC,
a.appraise_time DESC
LIMIT 0, 20
執行計劃顯示爲全表掃描:
+----+-------------+-------+--------+-------------+---------+---------+---------------+---------+-+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra
+----+-------------+-------+--------+-------------+---------+---------+---------------+---------+-+
| 1 | SIMPLE | a | ALL | idx_orderid | NULL | NULL | NULL | 1967647 | Using filesort |
| 1 | SIMPLE | o | eq_ref | PRIMARY | PRIMARY | 122 | a.orderid | 1 | NULL |
+----+-------------+-------+--------+---------+---------+---------+-----------------+---------+-+
由于is_reply隻有0和(hé)1兩種狀态,我們按照(zhào)下(xià)面的(de)方法重寫後,執行時(shí)間從1.58秒降低到2毫秒。
SELECT *
FROM ((SELECT *
FROM my_order o
INNER JOIN my_appraise a
ON a.orderid = o.id
AND is_reply = 0
ORDER BY appraise_time DESC
LIMIT 0, 20)
UNION ALL
(SELECT *
FROM my_order o
INNER JOIN my_appraise a
ON a.orderid = o.id
AND is_reply = 1
ORDER BY appraise_time DESC
LIMIT 0, 20)) t
ORDER BY is_reply ASC,
appraisetime DESC
LIMIT 20;
5. EXISTS語句
MySQL對(duì)待EXISTS子句時(shí),仍然采用(yòng)嵌套子查詢的(de)執行方式。如下(xià)面的(de)SQL語句:
SELECT *
FROM my_neighbor n
LEFT JOIN my_neighbor_apply sra
ON n.id = sra.neighbor_id
AND sra.user_id = 'xxx'
WHERE n.topic_status < 4
AND EXISTS(SELECT 1
FROM message_info m
WHERE n.id = m.neighbor_id
AND m.inuser = 'xxx')
AND n.topic_type <> 5
執行計劃爲:
+----+--------------------+-------+------+-----+------------------------------------------+---------+-------+---------+ -----+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+-------+------+ -----+------------------------------------------+---------+-------+---------+ -----+
| 1 | PRIMARY | n | ALL | | NULL | NULL | NULL | 1086041 | Using where |
| 1 | PRIMARY | sra | ref | | idx_user_id | 123 | const | 1 | Using where |
| 2 | DEPENDENT SUBQUERY | m | ref | | idx_message_info | 122 | const | 1 | Using index condition; Using where |
+----+--------------------+-------+------+ -----+------------------------------------------+---------+-------+---------+ -----+
去掉exists更改爲join,能夠避免嵌套子查詢,将執行時(shí)間從1.93秒降低爲1毫秒。
SELECT *
FROM my_neighbor n
INNER JOIN message_info m
ON n.id = m.neighbor_id
AND m.inuser = 'xxx'
LEFT JOIN my_neighbor_apply sra
ON n.id = sra.neighbor_id
AND sra.user_id = 'xxx'
WHERE n.topic_status < 4
AND n.topic_type <> 5
新的(de)執行計劃:
+----+-------------+-------+--------+ -----+------------------------------------------+---------+ -----+------+ -----+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+--------+ -----+------------------------------------------+---------+ -----+------+ -----+
| 1 | SIMPLE | m | ref | | idx_message_info | 122 | const | 1 | Using index condition |
| 1 | SIMPLE | n | eq_ref | | PRIMARY | 122 | ighbor_id | 1 | Using where |
| 1 | SIMPLE | sra | ref | | idx_user_id | 123 | const | 1 | Using where |
+----+-------------+-------+--------+ -----+------------------------------------------+---------+ -----+------+ -----+
6. 條件下(xià)推
外部查詢條件不能夠下(xià)推到複雜(zá)的(de)視圖或子查詢的(de)情況有:
聚合子查詢; 含有LIMIT的(de)子查詢; UNION 或UNION ALL子查詢; 輸出字段中的(de)子查詢;
如下(xià)面的(de)語句,從執行計劃可(kě)以看出其條件作用(yòng)于聚合子查詢之後:
SELECT *
FROM (SELECT target,
Count(*)
FROM operation
GROUP BY target) t
WHERE target = 'rm-xxxx'
+----+-------------+------------+-------+---------------+-------------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+-------+---------------+-------------+---------+-------+------+-------------+
| 1 | PRIMARY | <derived2> | ref | <auto_key0> | <auto_key0> | 514 | const | 2 | Using where |
| 2 | DERIVED | operation | index | idx_4 | idx_4 | 519 | NULL | 20 | Using index |
+----+-------------+------------+-------+---------------+-------------+---------+-------+------+-------------+
确定從語義上查詢條件可(kě)以直接下(xià)推後,重寫如下(xià):
SELECT target,
Count(*)
FROM operation
WHERE target = 'rm-xxxx'
GROUP BY target
執行計劃變爲:
+----+-------------+-----------+------+---------------+-------+---------+-------+------+--------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+---------------+-------+---------+-------+------+--------------------+
| 1 | SIMPLE | operation | ref | idx_4 | idx_4 | 514 | const | 1 | Using where; Using index |
+----+-------------+-----------+------+---------------+-------+---------+-------+------+--------------------+
關于MySQL外部條件不能下(xià)推的(de)詳細解釋說明(míng)請參考以前文章(zhāng):MySQL · 性能優化(huà) · 條件下(xià)推到物(wù)化(huà)表
7. 提前縮小範圍
先上初始SQL語句:
SELECT *
FROM my_order o
LEFT JOIN my_userinfo u
ON o.uid = u.uid
LEFT JOIN my_productinfo p
ON o.pid = p.pid
WHERE ( o.display = 0 )
AND ( o.ostaus = 1 )
ORDER BY o.selltime DESC
LIMIT 0, 15
該SQL語句原意是:先做(zuò)一系列的(de)左連接,然後排序取前15條記錄。從執行計劃也(yě)可(kě)以看出,最後一步估算(suàn)排序記錄數爲90萬,時(shí)間消耗爲12秒。
+----+-------------+-------+--------+---------------+---------+---------+-----------------+--------+----------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+--------+---------------+---------+---------+-----------------+--------+----------------------------------------------------+
| 1 | SIMPLE | o | ALL | NULL | NULL | NULL | NULL | 909119 | Using where; Using temporary; Using filesort |
| 1 | SIMPLE | u | eq_ref | PRIMARY | PRIMARY | 4 | o.uid | 1 | NULL |
| 1 | SIMPLE | p | ALL | PRIMARY | NULL | NULL | NULL | 6 | Using where; Using join buffer (Block Nested Loop) |
+----+-------------+-------+--------+---------------+---------+---------+-----------------+--------+----------------------------------------------------+
由于最後WHERE條件以及排序均針對(duì)最左主表,因此可(kě)以先對(duì)my_order排序提前縮小數據量再做(zuò)左連接。SQL重寫後如下(xià),執行時(shí)間縮小爲1毫秒左右。
SELECT *
FROM (
SELECT *
FROM my_order o
WHERE ( o.display = 0 )
AND ( o.ostaus = 1 )
ORDER BY o.selltime DESC
LIMIT 0, 15
) o
LEFT JOIN my_userinfo u
ON o.uid = u.uid
LEFT JOIN my_productinfo p
ON o.pid = p.pid
ORDER BY o.selltime DESC
limit 0, 15
再檢查執行計劃:子查詢物(wù)化(huà)後(select_type=DERIVED)參與JOIN。雖然估算(suàn)行掃描仍然爲90萬,但是利用(yòng)了(le)索引以及LIMIT 子句後,實際執行時(shí)間變得(de)很小。
+----+-------------+------------+--------+---------------+---------+---------+-------+--------+----------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+--------+---------------+---------+---------+-------+--------+----------------------------------------------------+
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 15 | Using temporary; Using filesort |
| 1 | PRIMARY | u | eq_ref | PRIMARY | PRIMARY | 4 | o.uid | 1 | NULL |
| 1 | PRIMARY | p | ALL | PRIMARY | NULL | NULL | NULL | 6 | Using where; Using join buffer (Block Nested Loop) |
| 2 | DERIVED | o | index | NULL | idx_1 | 5 | NULL | 909112 | Using where |
+----+-------------+------------+--------+---------------+---------+---------+-------+--------+----------------------------------------------------+
8. 中間結果集下(xià)推
再來(lái)看下(xià)面這(zhè)個(gè)已經初步優化(huà)過的(de)例子(左連接中的(de)主表優先作用(yòng)查詢條件):
SELECT a.*,
c.allocated
FROM (
SELECT resourceid
FROM my_distribute d
WHERE isdelete = 0
AND cusmanagercode = '1234567'
ORDER BY salecode limit 20) a
LEFT JOIN
(
SELECT resourcesid, sum(ifnull(allocation, 0) * 12345) allocated
FROM my_resources
GROUP BY resourcesid) c
ON a.resourceid = c.resourcesid
那麽該語句還(hái)存在其它問題嗎?不難看出子查詢 c 是全表聚合查詢,在表數量特别大(dà)的(de)情況下(xià)會導緻整個(gè)語句的(de)性能下(xià)降。
其實對(duì)于子查詢 c,左連接最後結果集隻關心能和(hé)主表resourceid能匹配的(de)數據。因此我們可(kě)以重寫語句如下(xià),執行時(shí)間從原來(lái)的(de)2秒下(xià)降到2毫秒。
SELECT a.*,
c.allocated
FROM (
SELECT resourceid
FROM my_distribute d
WHERE isdelete = 0
AND cusmanagercode = '1234567'
ORDER BY salecode limit 20) a
LEFT JOIN
(
SELECT resourcesid, sum(ifnull(allocation, 0) * 12345) allocated
FROM my_resources r,
(
SELECT resourceid
FROM my_distribute d
WHERE isdelete = 0
AND cusmanagercode = '1234567'
ORDER BY salecode limit 20) a
WHERE r.resourcesid = a.resourcesid
GROUP BY resourcesid) c
ON a.resourceid = c.resourcesid
但是子查詢 a 在我們的(de)SQL語句中出現了(le)多(duō)次。這(zhè)種寫法不僅存在額外的(de)開銷,還(hái)使得(de)整個(gè)語句顯的(de)繁雜(zá)。使用(yòng)WITH語句再次重寫:
WITH a AS
(
SELECT resourceid
FROM my_distribute d
WHERE isdelete = 0
AND cusmanagercode = '1234567'
ORDER BY salecode limit 20)
SELECT a.*,
c.allocated
FROM a
LEFT JOIN
(
SELECT resourcesid, sum(ifnull(allocation, 0) * 12345) allocated
FROM my_resources r,
a
WHERE r.resourcesid = a.resourcesid
GROUP BY resourcesid) c
ON a.resourceid = c.resourcesid
總結
數據庫編譯器産生執行計劃,決定著(zhe)SQL的(de)實際執行方式。但是編譯器隻是盡力服務,所有數據庫的(de)編譯器都不是盡善盡美(měi)的(de)。上述提到的(de)多(duō)數場(chǎng)景,在其它數據庫中也(yě)存在性能問題。了(le)解數據庫編譯器的(de)特性,才能避規其短處,寫出高(gāo)性能的(de)SQL語句。 程序員(yuán)在設計數據模型以及編寫SQL語句時(shí),要把算(suàn)法的(de)思想或意識帶進來(lái)。 編寫複雜(zá)SQL語句要養成使用(yòng)WITH語句的(de)習(xí)慣。簡潔且思路清晰的(de)SQL語句也(yě)能減小數據庫的(de)負擔 ^^。 使用(yòng)雲上數據庫遇到難點(不局限于SQL問題),随時(shí)尋求阿裏雲原廠專家服務的(de)幫助。
掃一掃,關注我們
相關新聞
- JAVA開發之hashMap時(shí)間複雜(zá)度分(fēn)析
- 微信小程序開發偶發性獲取手機号失敗解決方案
- 使用(yòng)JAVA開發小程序時(shí),如何防止接口被頻(pín)繁請求
- app開發制作過程中,使用(yòng)JAVA注解方式,實現權限功能開發
- springBoot小程序開發的(de)項目,後台如何優雅的(de)停止進程
- JAVA知識十連問
- JAVA語言小程序開發之hashMap原理(lǐ)詳解
- mysql常見錯誤詳解
- 小程序open-data組件将于2022年2月(yuè)21日24時(shí)起···
- 爲什(shén)麽重寫了(le)equals方法,就必須重寫hashCode