欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
一個(gè)奇怪的MySQL慢查詢(xún),打懵了一群不懂業(yè)務(wù)的DBA!

前言

最近,開(kāi)發(fā)人員需要定期的刪除表里一定時(shí)間以前的數據,SQL如下:

mysql > delete from testtable WHERE biz_date <= '2017-08-21 00:00:00' AND status = 2 limit 500\G

前段時(shí)間在優(yōu)化的時(shí)候,我們已經(jīng)在相應的查詢(xún)條件上加上了索引,如下:

KEY `idx_bizdate_st` (`biz_date`,`status`)
但是實(shí)際執行的SQL依然非常慢,為什么呢,我們來(lái)一步步分析驗證下。

分析

表上的字段既然都有索引,那么按照之前的文章分析,是兩個(gè)字段都可以走上索引的。

既然能夠利用索引,表的總大小也就是200M左右,那么為什么形成了慢查呢?

我們查看執行計劃,去掉limit 后,發(fā)現他選擇了走全表掃描。
mysql > desc select * from testtable WHERE biz_date <= '2017-08-21 00:00:00'; ---- ------------- ----------- ------ ---------------- ------ --------- ------ -------- ------------- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | ---- ------------- ----------- ------ ---------------- ------ --------- ------ -------- ------------- | 1 | SIMPLE | testtable | ALL | idx_bizdate_st | NULL | NULL | NULL | 980626 | Using where | ---- ------------- ----------- ------ ---------------- ------ --------- ------ -------- ------------- 1 row in set (0.00 sec)
-- 只查詢(xún)biz_date-- 關(guān)鍵點(diǎn):rows:980626;type:ALL
mysql > desc select * from testtable WHERE biz_date <= '2017-08-21 00:00:00' and status = 2; ---- ------------- ----------- ------ ---------------- ------ --------- ------ -------- ------------- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | ---- ------------- ----------- ------ ---------------- ------ --------- ------ -------- ------------- | 1 | SIMPLE | testtable | ALL | idx_bizdate_st | NULL | NULL | NULL | 980632 | Using where | ---- ------------- ----------- ------ ---------------- ------ --------- ------ -------- ------------- 1 row in set (0.00 sec)
-- 查詢(xún)biz_date status -- 關(guān)鍵點(diǎn):rows:980632;type:ALL
mysql > desc select * from testtable WHERE biz_date <= '2017-08-21 00:00:00' and status = 2 limit 100; ---- ------------- ----------- ------- ---------------- ---------------- --------- ------ -------- ----------------------- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | ---- ------------- ----------- ------- ---------------- ---------------- --------- ------ -------- ----------------------- | 1 | SIMPLE | testtable | range | idx_bizdate_st | idx_bizdate_st | 6 | NULL | 490319 | Using index condition | ---- ------------- ----------- ------- ---------------- ---------------- --------- ------ -------- ----------------------- 1 row in set (0.00 sec)
-- 查詢(xún)biz_date status limit -- 關(guān)鍵點(diǎn):rows:490319;
mysql > select count(*) from testtable WHERE biz_date <= '2017-08-21 00:00:00' and status = 2; ---------- | count(*) | ---------- | 0 | ---------- 1 row in set (0.34 sec)
mysql > select count(*) from testtable WHERE biz_date <= '2017-08-21 00:00:00'; ---------- | count(*) | ---------- | 970183 | ---------- 1 row in set (0.33 sec)
mysql > select count(*) from testtable; ---------- | count(*) | ---------- | 991421 | ---------- 1 row in set (0.19 sec)
mysql > select distinct biz_status from testtable; ------------ | biz_status | ------------ | 1 || 2 || 4 | ------------
通過(guò)以上查詢(xún),我們可以發(fā)現如下幾點(diǎn)問(wèn)題:

  • 通過(guò) biz_date 預估出來(lái)的行數 和 biz_date status=2 預估出來(lái)的行數幾乎一樣,為98w。
  • 實(shí)際查詢(xún)表 biz_date status=2 一條記錄都沒(méi)有。
  • 整表數據量達到了99萬(wàn),MySQL發(fā)現通過(guò)索引掃描需要98w行(預估)

因此,MySQL通過(guò)統計信息預估的時(shí)候,發(fā)現需要掃描的索引行數幾乎占到了整個(gè)表,放棄了使用索引,選擇了走全表掃描。

那是不是他的統計信息有問(wèn)題呢?我們重新收集了下表統計信息,發(fā)現執行計劃的預估行數還是一樣,猜測只能根據組合索引的第一個(gè)字段進(jìn)行預估(待確定)。
那我們試下直接強制讓他走索引呢?
mysql > select * from testtable   WHERE biz_date <= '2017-08-21 00:00:00' and status = 2;Empty set (0.79 sec)

mysql > select * from testtable force index(idx_bizdate_st) WHERE biz_date <= '2017-08-21 00:00:00' and status = 2;Empty set (0.16 sec)

我們發(fā)現,強制指定索引后,查詢(xún)耗時(shí)和沒(méi)有強制索引比較,的確執行速度快了很多,因為沒(méi)有強制索引是全表掃描嘛!但是!依然非常慢!


那么還有什么辦法去優(yōu)化這個(gè)本來(lái)應該很快的查詢(xún)呢?

大家應該都聽(tīng)說(shuō)過(guò)要選擇性好的字段放在組合索引的最前面?

選擇性好的索引在前面并不是對所有的場(chǎng)景都通用的,這個(gè)場(chǎng)景可以將status放前面,sql速度會(huì )更快。

那,能不能讓他不要掃描索引的那么多范圍呢?之前的索引模型中也說(shuō)過(guò),MySQL是通過(guò)索引去確定一個(gè)掃描范圍,如果能夠定位到盡可能小的范圍,那是不是速度上會(huì )快很多呢?

并且業(yè)務(wù)邏輯上是定期刪除一定日期之前的數據。所以邏輯上來(lái)說(shuō),每次刪除都是只刪除一天的數據,直接讓SQL掃描一天的范圍。那么我們就可以改寫(xiě)SQL啦!

mysql > select * from testtable WHERE biz_date >= '2017-08-20 00:00:00' and biz_date <= '2017-08-21 00:00:00' and status = 2;Empty set (0.00 sec)
mysql > desc select * from testtable WHERE biz_date >= '2017-08-20 00:00:00' and biz_date <= '2017-08-21 00:00:00' and status = 2; ---- ------------- ------------------ ------- ---------------- ---------------- --------- ------ ------ ----------------------- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | ---- ------------- ------------------ ------- ---------------- ---------------- --------- ------ ------ ----------------------- | 1 | SIMPLE | testtable | range | idx_bizdate_st | idx_bizdate_st | 6 | NULL | 789 | Using index condition | ---- ------------- ------------------ ------- ---------------- ---------------- --------- ------ ------ ----------------------- 1 row in set (0.00 sec)
-- rows降低了很多,乖乖的走了索引
mysql > desc select * from testtable WHERE biz_date >= '2017-08-20 00:00:00' and biz_date <= '2017-08-21 00:00:00' ; ---- ------------- ------------------ ------- ---------------- ---------------- --------- ------ ------ ----------------------- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | ---- ------------- ------------------ ------- ---------------- ---------------- --------- ------ ------ ----------------------- | 1 | SIMPLE | testtable | range | idx_bizdate_st | idx_bizdate_st | 5 | NULL | 1318 | Using index condition | ---- ------------- ------------------ ------- ---------------- ---------------- --------- ------ ------ ----------------------- 1 row in set (0.00 sec)
-- 即使沒(méi)有status,也是肯定走索引啦

小結

這個(gè)問(wèn)題,我原本打算用hint,強制讓他走索引,但是實(shí)際上強制走索引的執行時(shí)間并不能帶來(lái)滿(mǎn)意的效果。結合業(yè)務(wù)邏輯,來(lái)優(yōu)化SQL,是最好的方式,也是終極法寶,一定要好好利用。不了解業(yè)務(wù)的DBA,不是一個(gè)好DBA... 

來(lái)源:http://www.fordba.com/optimize-an-amazing-mysql-slowlog.html
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
MySQL SHOW INDEX語(yǔ)法 查看索引狀態(tài)
ORDER BY導致索引使用不理想
mysql優(yōu)化筆記
Mysql下優(yōu)化SQL的一般步驟
mysql – 使用復雜過(guò)濾優(yōu)化SQL查詢(xún)
MySQL索引原理及慢查詢(xún)優(yōu)化(好文)
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久