sql最佳化(mysql)
mysql版本:
今天早上看到一個sql執行慢,要8秒左右才能出結果,sql如下:
檢視此sql的執行計劃:
檢視錶上都有哪些索引:
檢視選擇性:
看下全表有多少資料:
選擇性最好就是INTO_TIME,因為這個列也有索引,強制走這個索引
用上這個索引了,實際執行不到0.3秒出結果,還是可以接受的。
最後給研發提了幾個建議:
1.儘量不要用select *
2.這個表200多萬資料,可以考慮分表。
點選(此處)摺疊或開啟
-
mysql> select version();
-
+------------+
-
| version() |
-
+------------+
-
| 5.7.12-log |
-
+------------+
- 1 row in set (0.00 sec)
點選(此處)摺疊或開啟
-
SELECT *
-
FROM CUSTOMERS
-
WHERE TYPE = 1
-
AND STATUS < 7
-
AND ISAREA = 6
-
AND INTO_TIME >= '2016-11-01'
-
AND INTO_TIME <= '2016-12-01'
- ORDER BY SCORE DESC LIMIT 1140, 20;
點選(此處)摺疊或開啟
-
mysql> explain SELECT *
-
-> FROM CUSTOMERS
-
-> WHERE TYPE = 1
-
-> AND STATUS < 7
-
-> AND ISAREA = 6
-
-> AND INTO_TIME >= '2016-11-01'
-
-> AND INTO_TIME <= '2016-12-01'
-
-> ORDER BY SCORE DESC LIMIT 1140, 20;
-
+----+-------------+-----------+------------+-------+-----------------------------------------------------------------+-------+---------+------+-------+----------+-------------+
-
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
-
+----+-------------+-----------+------------+-------+-----------------------------------------------------------------+-------+---------+------+-------+----------+-------------+
-
| 1 | SIMPLE | CUSTOMERS | NULL | index | type,into_time,isarea,status,idx_isarea_renew_owner,type_status | score | 2 | NULL | 11314 | 0.25 | Using where |
-
+----+-------------+-----------+------------+-------+-----------------------------------------------------------------+-------+---------+------+-------+----------+-------------+
- 1 row in set, 1 warning (0.00 sec)
檢視錶上都有哪些索引:
點選(此處)摺疊或開啟
-
PRIMARY KEY (`id`),
-
KEY `newdata` (`newdata`),
-
KEY `cusname` (`cusname`),
-
KEY `type` (`type`,`ownerid`),
-
KEY `operator` (`operator`),
-
KEY `into_time` (`into_time`),
-
KEY `isarea` (`isarea`),
-
KEY `linkcase` (`linkcase`),
-
KEY `status` (`status`),
-
KEY `operate_id` (`operate_id`),
-
KEY `isparticiple` (`isparticiple`),
-
KEY `idx_level_op` (`level_op`),
-
KEY `idx_status_op` (`status_op`),
-
KEY `renew_ownerid` (`renew_ownerid`),
-
KEY `renew` (`renew`),
-
KEY `idx_ownerid` (`ownerid`),
-
KEY `idx_isarea_renew_owner` (`isarea`,`renew_ownerid`),
-
KEY `idx_create_time` (`create_time`),
-
KEY `idx_source` (`source`),
-
KEY `type_status` (`type`,`status`),
-
KEY `end_month` (`end_month`),
-
KEY `score` (`score`),
-
FULLTEXT KEY `fdx_cusname` (`cusname_idx`)
- ) ENGINE=InnoDB AUTO_INCREMENT=2529287 DEFAULT CHARSET=utf8 |
點選(此處)摺疊或開啟
-
mysql> SELECT COUNT(*) FROM CUSTOMERS where TYPE = 1;
-
+----------+
-
| COUNT(*) |
-
+----------+
-
| 2347457 |
-
+----------+
-
1 row in set (4.66 sec)
-
-
mysql> select count(*) from customers where STATUS < 7;
-
+----------+
-
| count(*) |
-
+----------+
-
| 2468461 |
-
+----------+
-
1 row in set (3.51 sec)
-
-
mysql> select count(*) from customers where ISAREA = 6;
-
+----------+
-
| count(*) |
-
+----------+
-
| 134726 |
-
+----------+
-
1 row in set (0.17 sec)
-
-
mysql> SELECT COUNT(*) FROM CUSTOMERS WHERE INTO_TIME >= '2016-11-01' AND INTO_TIME <= '2016-12-01';
-
+----------+
-
| COUNT(*) |
-
+----------+
-
| 110859 |
-
+----------+
- 1 row in set (0.26 sec)
看下全表有多少資料:
點選(此處)摺疊或開啟
-
mysql> select count(*) from customers;
-
+----------+
-
| count(*) |
-
+----------+
-
| 2481386 |
-
+----------+
- 1 row in set (2.01 sec)
選擇性最好就是INTO_TIME,因為這個列也有索引,強制走這個索引
點選(此處)摺疊或開啟
-
mysql> explain SELECT *
-
-> FROM CUSTOMERS USE KEY(into_time)
-
-> WHERE TYPE = 1
-
-> AND STATUS < 7
-
-> AND ISAREA = 6
-
-> AND INTO_TIME >= '2016-11-01'
-
-> AND INTO_TIME <= '2016-12-01'
-
-> ORDER BY SCORE DESC LIMIT 1140, 20;
-
+----+-------------+-----------+------------+-------+---------------+-----------+---------+------+--------+----------+----------------------------------------------------+
-
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
-
+----+-------------+-----------+------------+-------+---------------+-----------+---------+------+--------+----------+----------------------------------------------------+
-
| 1 | SIMPLE | CUSTOMERS | NULL | range | into_time | into_time | 6 | NULL | 244448 | 0.05 | Using index condition; Using where; Using filesort |
-
+----+-------------+-----------+------------+-------+---------------+-----------+---------+------+--------+----------+----------------------------------------------------+
- 1 row in set, 1 warning (0.00 sec)
最後給研發提了幾個建議:
1.儘量不要用select *
2.這個表200多萬資料,可以考慮分表。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/20893244/viewspace-2131988/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- mysql資料庫SQL最佳化MySql資料庫
- mysql資料庫SQL最佳化2MySql資料庫
- mysql資料庫SQL最佳化3MySql資料庫
- MySQL SQL最佳化 - 覆蓋索引(covering index)MySql索引Index
- MySQL的SQL語句最佳化一例MySql
- Effective MySQL之SQL語句最佳化 小結MySql
- sql最佳化:使用sql profile最佳化sql語句SQL
- SQL最佳化工具(MYSQL)——SQLAdvisor安裝使用MySql
- MySQL最佳化之如何查詢SQL效率低的原因MySql
- SQL最佳化 | MySQL問題處理案例分享三則MySql
- MySQL 效能最佳化:8 種常見 SQL 錯誤用法!MySql
- SQL最佳化SQL
- MySQL分頁查詢offset過大,Sql最佳化經驗MySql
- 【SQL最佳化】SQL最佳化的10點注意事項SQL
- Mysql 最佳化MySql
- mysql最佳化MySql
- SQL最佳化1SQL
- PL/SQL最佳化SQL
- SQL最佳化方案SQL
- sql最佳化技巧SQL
- 【MySQL】MySQL語句最佳化MySql
- mysql分頁時offset過大的Sql最佳化經驗分享MySql
- mysql最佳化索引MySql索引
- MySQL最佳化方向MySql
- 小米 sql 最佳化工具SQL
- SQL最佳化問題SQL
- SQL 最佳化手冊SQL
- SQL最佳化(一) 索引SQL索引
- sql最佳化工具SQLTSQL
- Oracle SQL效能最佳化OracleSQL
- Oracle sql 效能最佳化OracleSQL
- 最佳化sql語句SQL
- SQL最佳化 之 -- joinSQL
- SQL的最佳化[轉]SQL
- 08SQL最佳化SQL
- SQL語句最佳化SQL
- 【Mysql】MySQL查詢最佳化-explainMySqlAI
- SQL效能最佳化之索引最佳化法SQL索引