SQL 書寫規(guī)范與各關(guān)鍵字詳解
《SQL 書寫規(guī)范與各關(guān)鍵字詳解》由會(huì)員分享,可在線閱讀,更多相關(guān)《SQL 書寫規(guī)范與各關(guān)鍵字詳解(17頁(yè)珍藏版)》請(qǐng)?jiān)谘b配圖網(wǎng)上搜索。
1、SQL 書寫規(guī)范與各關(guān)鍵字詳解
l 1、LIMIT 語(yǔ)句
l 2、隱式轉(zhuǎn)換
l 3、關(guān)聯(lián)更新、刪除
l 4、混合排序
l 5、EXISTS語(yǔ)句
l 6、條件下推
l 7、提前縮小范圍
l 8、中間結(jié)果集下推
l 總結(jié)
sql語(yǔ)句的執(zhí)行順序:
FROM
2、ndition>
SELECT
DISTINCT
3、 name = 'SlowLog'
ORDER BY create_time
LIMIT 1000, 10;
好吧,可能90%以上的 DBA 解決該問題就到此為止。但當(dāng) LIMIT 子句變成 “LIMIT 1000000,10” 時(shí),程序員仍然會(huì)抱怨:我只取10條記錄為什么還是慢?
要知道數(shù)據(jù)庫(kù)也并不知道第1000000條記錄從什么地方開始,即使有索引也需要從頭計(jì)算一次。出現(xiàn)這種性能問題,多數(shù)情形下是程序員偷懶了。
在前端數(shù)據(jù)瀏覽翻頁(yè),或者大數(shù)據(jù)分批導(dǎo)出等場(chǎng)景下,是可以將上一頁(yè)的最大值當(dāng)成參數(shù)作為查詢條件的。SQL 重新設(shè)計(jì)如下:
SELECT *
FROM 4、operation
WHERE type = 'SQLStats'
AND name = 'SlowLog'
AND create_time > '2017-03-16 14:00:00'
ORDER BY create_time limit 10;
在新設(shè)計(jì)下查詢時(shí)間基本固定,不會(huì)隨著數(shù)據(jù)量的增長(zhǎng)而發(fā)生變化。
2、隱式轉(zhuǎn)換
SQL語(yǔ)句中查詢變量和字段定義類型不匹配是另一個(gè)常見的錯(cuò)誤。比如下面的語(yǔ)句:
mysql> explain extended SELECT *
> FROM my_balance b
> WHERE 5、 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 的定義為 varchar(20),MySQL 的策略是將字符串轉(zhuǎn)換為數(shù)字之后再比較。函數(shù)作用于表字段,索引失效。
上述情況可能是應(yīng)用程序框架自動(dòng)填入的參數(shù),而不是程序員的原意?,F(xiàn)在應(yīng)用框 6、架很多很繁雜,使用方便的同時(shí)也小心它可能給自己挖坑。
3、關(guān)聯(lián)更新、刪除
雖然 MySQL5.6 引入了物化特性,但需要特別注意它目前僅僅針對(duì)查詢語(yǔ)句的優(yōu)化。對(duì)于更新或刪除需要手工重寫成 JOIN。
比如下面 UPDATE 語(yǔ)句,MySQL 實(shí)際執(zhí)行的是循環(huán)/嵌套子查詢(DEPENDENT SUBQUERY),其執(zhí)行時(shí)間可想而知。
UPDATE operation o
SET status = 'applying'
WHERE o.id IN (SELECT id
FROM (SELECT o.id,
7、 o.status
FROM operation o
WHERE o.group = 123
AND o.status NOT IN ( 'done' )
ORDER BY o.parent,
o.id
LIMIT 1) t); 8、
執(zhí)行計(jì)劃:
+----+--------------------+-------+-------+---------------+---------+---------+-------+------+-----------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra 9、 |
+----+--------------------+-------+-------+---------------+---------+---------+-------+------+-----------------------------------------------------+
| 1 | PRIMARY | o | index | | PRIMARY | 8 | | 24 | Using where; Using temporary 10、 |
| 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 11、 |
+----+--------------------+-------+-------+---------------+---------+---------+-------+------+-----------------------------------------------------+
重寫為 JOIN 之后,子查詢的選擇模式從 DEPENDENT SUBQUERY 變成 DERIVED,執(zhí)行速度大大加快,從7秒降低到2毫秒。
UPDATE operation o
JOIN (SELECT o.id,
12、 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. 13、id
SET status = 'applying'
執(zhí)行計(jì)劃簡(jiǎn)化為:
+----+-------------+-------+------+---------------+-------+---------+-------+------+-----------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra 14、 |
+----+-------------+-------+------+---------------+-------+---------+-------+------+-----------------------------------------------------+
| 1 | PRIMARY | | | | | | | | Impossible WHERE noticed after reading const tables 15、 |
| 2 | DERIVED | o | ref | idx_2,idx_5 | idx_5 | 8 | const | 1 | Using where; Using filesort |
+----+-------------+-------+------+---------------+-------+---------+-------+------+-----------------------------------------------------+
4、混合排序
MySQL 16、不能利用索引進(jìn)行混合排序。但在某些場(chǎng)景,還是有機(jī)會(huì)使用特殊方法提升性能的。
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
執(zhí)行計(jì)劃顯示為全表掃描:
+----+-------------+-------+--------+-------------+---------+---------+---------------+- 17、--------+-+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra
+----+-------------+-------+--------+-------------+---------+---------+---------------+---------+-+
| 1 | SIMPLE | a | ALL | idx_orderid | NULL | NULL | NULL 18、| 1967647 | Using filesort |
| 1 | SIMPLE | o | eq_ref | PRIMARY | PRIMARY | 122 | a.orderid | 1 | NULL |
+----+-------------+-------+--------+---------+---------+---------+-----------------+---------+-+
由于 is_reply 只有0和1兩種狀態(tài),我們按照下面的方法重寫后,執(zhí)行時(shí)間從1.58秒降低到2毫秒。
S 19、ELECT *
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 *
FR 20、OM 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語(yǔ)句
My 21、SQL 對(duì)待 EXISTS 子句時(shí),仍然采用嵌套子查詢的執(zhí)行方式。如下面的 SQL 語(yǔ)句:
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
22、 WHERE n.id = m.neighbor_id
AND m.inuser = 'xxx')
AND n.topic_type <> 5
執(zhí)行計(jì)劃為:
+----+--------------------+-------+------+-----+------------------------------------------+---------+-------+---------+ -----+
| id | select_type | table | t 23、ype | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+-------+------+ -----+------------------------------------------+---------+-------+---------+ -----+
| 1 | PRIMARY | n | ALL | | NULL | NULL | NULL | 1086041 | Using where 24、 |
| 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 |
+----+--------------------+-------+ 25、------+ -----+------------------------------------------+---------+-------+---------+ -----+
去掉 exists 更改為 join,能夠避免嵌套子查詢,將執(zhí)行時(shí)間從1.93秒降低為1毫秒。
SELECT *
FROM my_neighbor n
INNER JOIN message_info m
ON n.id = m.neighbor_id
AND m.inuser = 'xxx'
26、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
新的執(zhí)行計(jì)劃:
+----+-------------+-------+--------+ -----+------------------------------------------+---------+ -----+------+ -- 27、---+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+--------+ -----+------------------------------------------+---------+ -----+------+ -----+
| 1 | SIMPLE | m | ref | | idx_message_in 28、fo | 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 |
+----+-------------+-------+--- 29、-----+ -----+------------------------------------------+---------+ -----+------+ -----+
6、條件下推
外部查詢條件不能夠下推到復(fù)雜的視圖或子查詢的情況有:
1、聚合子查詢;2、含有 LIMIT 的子查詢;3、UNION 或 UNION ALL 子查詢;4、輸出字段中的子查詢;
如下面的語(yǔ)句,從執(zhí)行計(jì)劃可以看出其條件作用于聚合子查詢之后:
SELECT *
FROM (SELECT target,
Count(*)
FROM ope 30、ration
GROUP BY target) t
WHERE target = 'rm-xxxx'
+----+-------------+------------+-------+---------------+-------------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-- 31、-----------+------------+-------+---------------+-------------+---------+-------+------+-------------+
| 1 | PRIMARY | 32、LL | 20 | Using index |
+----+-------------+------------+-------+---------------+-------------+---------+-------+------+-------------+
確定從語(yǔ)義上查詢條件可以直接下推后,重寫如下:
SELECT target,
Count(*)
FROM operation
WHERE target = 'rm-xxxx'
GROUP BY target
執(zhí)行計(jì)劃變?yōu)椋?
+----+-------------+-- 33、---------+------+---------------+-------+---------+-------+------+--------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+---------------+-------+---------+-------+------+--------------------+
| 1 | SIMP 34、LE | operation | ref | idx_4 | idx_4 | 514 | const | 1 | Using where; Using index |
+----+-------------+-----------+------+---------------+-------+---------+-------+------+--------------------+
關(guān)于 MySQL 外部條件不能下推的詳細(xì)解釋說明請(qǐng)參考以前文章:MySQL · 性能優(yōu)化 · 條件下推到物化表 http://mysql.taobao.org/monthly/2016/07/08
35、7、提前縮小范圍
先上初始 SQL 語(yǔ)句:
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語(yǔ)句原意是:先做一系列的 36、左連接,然后排序取前15條記錄。從執(zhí)行計(jì)劃也可以看出,最后一步估算排序記錄數(shù)為90萬,時(shí)間消耗為12秒。
+----+-------------+-------+--------+---------------+---------+---------+-----------------+--------+----------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | 37、 rows | Extra |
+----+-------------+-------+--------+---------------+---------+---------+-----------------+--------+----------------------------------------------------+
| 1 | SIMPLE | o | ALL | NULL | NULL | NULL | NULL 38、 | 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 39、 | 6 | Using where; Using join buffer (Block Nested Loop) |
+----+-------------+-------+--------+---------------+---------+---------+-----------------+--------+----------------------------------------------------+
由于最后 WHERE 條件以及排序均針對(duì)最左主表,因此可以先對(duì) my_order 排序提前縮小數(shù)據(jù)量再做左連接。SQL 重寫后如下,執(zhí) 40、行時(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 41、.selltime DESC
limit 0, 15
再檢查執(zhí)行計(jì)劃:子查詢物化后(select_type=DERIVED)參與 JOIN。雖然估算行掃描仍然為90萬,但是利用了索引以及 LIMIT 子句后,實(shí)際執(zhí)行時(shí)間變得很小。
+----+-------------+------------+--------+---------------+---------+---------+-------+--------+----------------------------------------------------+
| id | select_type | table 42、 | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+--------+---------------+---------+---------+-------+--------+----------------------------------------------------+
| 1 | PRIMARY | 43、ed2> | 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 44、 | 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 |
+----+-------------+------- 45、-----+--------+---------------+---------+---------+-------+--------+----------------------------------------------------+
8、中間結(jié)果集下推
再來看下面這個(gè)已經(jīng)初步優(yōu)化過的例子(左連接中的主表優(yōu)先作用查詢條件):
SELECT a.*,
c.allocated
FROM (
SELECT resourceid
FROM my_distribute d 46、
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
47、 GROUP BY resourcesid) c
ON a.resourceid = c.resourcesid
那么該語(yǔ)句還存在其它問題嗎?不難看出子查詢 c 是全表聚合查詢,在表數(shù)量特別大的情況下會(huì)導(dǎo)致整個(gè)語(yǔ)句的性能下降。
其實(shí)對(duì)于子查詢 c,左連接最后結(jié)果集只關(guān)心能和主表 resourceid 能匹配的數(shù)據(jù)。因此我們可以重寫語(yǔ)句如下,執(zhí)行時(shí)間從原來的2秒下降到2毫秒。
SELECT a.*,
c.allocated
FROM (
SELECT resou 48、rceid
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) 49、allocated
FROM my_resources r,
(
SELECT resourceid
FROM my_distribute d
WHERE isdelete = 0
50、 AND cusmanagercode = '1234567'
ORDER BY salecode limit 20) a
WHERE r.resourcesid = a.resourcesid
GROUP BY resourcesid) c
ON a.resourceid = c.resourcesid
但是子查詢 a 在我們的SQL語(yǔ)句中出現(xiàn)了多次。這種寫法不僅存在額外的開銷,還使得整個(gè)語(yǔ)句 51、顯的繁雜。使用 WITH 語(yǔ)句再次重寫:
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
(
52、 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
總結(jié)
數(shù)據(jù)庫(kù)編譯器產(chǎn)生執(zhí)行計(jì)劃,決定著SQL的實(shí)際執(zhí)行方式。但是編譯器只是盡力服務(wù),所有數(shù)據(jù)庫(kù)的編譯器都不是盡善盡美的。
上述提到的多數(shù)場(chǎng)景,在其它數(shù)據(jù)庫(kù)中也存在性能問題。了解數(shù)據(jù)庫(kù)編譯器的特性,才能避規(guī)其短處,寫出高性能的SQL語(yǔ)句。
程序員在設(shè)計(jì)數(shù)據(jù)模型以及編寫SQL語(yǔ)句時(shí),要把算法的思想或意識(shí)帶進(jìn)來。
編寫復(fù)雜SQL語(yǔ)句要養(yǎng)成使用 WITH 語(yǔ)句的習(xí)慣。簡(jiǎn)潔且思路清晰的SQL語(yǔ)句也能減小數(shù)據(jù)庫(kù)的負(fù)擔(dān) 。
- 溫馨提示:
1: 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
2: 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
3.本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
5. 裝配圖網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 6.煤礦安全生產(chǎn)科普知識(shí)競(jìng)賽題含答案
- 2.煤礦爆破工技能鑒定試題含答案
- 3.爆破工培訓(xùn)考試試題含答案
- 2.煤礦安全監(jiān)察人員模擬考試題庫(kù)試卷含答案
- 3.金屬非金屬礦山安全管理人員(地下礦山)安全生產(chǎn)模擬考試題庫(kù)試卷含答案
- 4.煤礦特種作業(yè)人員井下電鉗工模擬考試題庫(kù)試卷含答案
- 1 煤礦安全生產(chǎn)及管理知識(shí)測(cè)試題庫(kù)及答案
- 2 各種煤礦安全考試試題含答案
- 1 煤礦安全檢查考試題
- 1 井下放炮員練習(xí)題含答案
- 2煤礦安全監(jiān)測(cè)工種技術(shù)比武題庫(kù)含解析
- 1 礦山應(yīng)急救援安全知識(shí)競(jìng)賽試題
- 1 礦井泵工考試練習(xí)題含答案
- 2煤礦爆破工考試復(fù)習(xí)題含答案
- 1 各種煤礦安全考試試題含答案