最近,開發人員須要按期的刪除表裏必定時間之前的數據,SQL以下: mysql > delete from testtable WHERE biz_date <= '2017-08-21 00:00:00' AND status = 2 limit 500\G 前段時間在優化的時候,咱們已經在相應的查詢條件上加上了索引,以下: KEY idx_bizdate_st
(biz_date
,status
) 可是實際執行的SQL依然很是慢,爲何呢,咱們來一步步分析驗證下。mysql
表上的字段既然都有索引,那麼按照以前的文章分析,是兩個字段均可以走上索引的。sql
既然可以利用索引,表的總大小也就是200M左右,那麼爲何造成了慢查呢?bash
咱們查看執行計劃,去掉limit 後,發現他選擇了走全表掃描。優化
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)
-- 只查詢biz_date
-- 關鍵點: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)
-- 查詢biz_date + status
-- 關鍵點: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)
-- 查詢biz_date + status+ limit
-- 關鍵點: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 |
+------------+
複製代碼
經過以上查詢,咱們能夠發現以下幾點問題:ui
經過 biz_date 預估出來的行數 和 biz_date + status=2 預估出來的行數幾乎同樣,爲98w。 實際查詢表 biz_date + status=2 一條記錄都沒有。 整表數據量達到了99萬,MySQL發現經過索引掃描須要98w行(預估)spa
所以,MySQL經過統計信息預估的時候,發現須要掃描的索引行數幾乎佔到了整個表,放棄了使用索引,選擇了走全表掃描。code
那是否是他的統計信息有問題呢?咱們從新收集了下表統計信息,發現執行計劃的預估行數仍是同樣,猜想只能根據組合索引的第一個字段進行預估(待肯定)。索引
那咱們試下直接強制讓他走索引呢?開發
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)
複製代碼
咱們發現,強制指定索引後,查詢耗時和沒有強制索引比較,的確執行速度快了不少,由於沒有強制索引是全表掃描嘛!可是!依然很是慢!string
那麼還有什麼辦法去優化這個原本應該很快的查詢呢?
你們應該都據說過要選擇性好的字段放在組合索引的最前面?
選擇性好的索引在前面並非對全部的場景都通用的,這個場景能夠將status放前面,sql速度會更快。
那,能不能讓他不要掃描索引的那麼多範圍呢?以前的索引模型中也說過,MySQL是經過索引去肯定一個掃描範圍,若是可以定位到儘量小的範圍,那是否是速度上會快不少呢?
而且業務邏輯上是按期刪除必定日期以前的數據。因此邏輯上來講,每次刪除都是隻刪除一天的數據,直接讓SQL掃描一天的範圍。那麼咱們就能夠改寫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)
-- 即便沒有status,也是確定走索引啦
複製代碼
這個問題,我本來打算用hint,強制讓他走索引,可是實際上強制走索引的執行時間並不能帶來滿意的效果。結合業務邏輯,來優化SQL,是最好的方式,也是終極法寶,必定要好好利用。不瞭解業務的DBA,不是一個好DBA...