這篇文章主要介紹MySQL中SQL語句優化的示例分析,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

創新互聯主營廣靈網站建設的網絡公司,主營網站建設方案,重慶APP軟件開發,廣靈h5成都小程序開發搭建,廣靈網站營銷推廣歡迎廣靈等地區企業咨詢
sql如下:sql強制用了into_time索引
# Time: 2017-02-14T11:35:01.594499+08:00
# User@Host: oms_readonly[oms_readonly] @ [10.44.xxx.xxx] Id: 41636892
# Query_time: 9.299612 Lock_time: 0.000124 Rows_sent: 20 Rows_examined: 2439330
SET timestamp=1487043301;
select * from customers force index(`into_time`) where `type` = 1 AND `status` < 7 AND `isarea` = 8 AND `into_time`<='2017-01-31 23:59:59' order by score desc limit 0,20;
查看表的相關狀態:
mysql> show table status like 'customers' \G;
*************************** 1. row ***************************
Name: customers
Engine: InnoDB
Version: 10
Row_format: Dynamic
Rows: 2504609
Avg_row_length: 710
Data_length: 1780383744
Max_data_length: 0
Index_length: 1253048320
Data_free: 6291456
Auto_increment: 2546101
Create_time: 2017-01-07 01:59:34
Update_time: 2017-02-14 13:58:17
Check_time: NULL
Collation: utf8_general_ci
Checksum: NULL
Create_options:
Comment:
1 row in set (0.00 sec)
表一共大約有250萬行記錄,查看下滿足into_time<='2017-01-31 23:59:59'這個條件的有多少行
mysql> select count(*) from customers where `into_time`<='2017-01-31 23:59:59';
+----------+
| count(*) |
+----------+
| 2439147 |
+----------+
1 row in set (0.95 sec)
顯然into_time這個列的索引已經不合適了,查看下表上都有那些索引
Create Table: CREATE TABLE `customers` (
`id` int(11) NOT NULL AUTO_INCREMENT,
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 `score` (`score`),
FULLTEXT KEY `fdx_cusname` (`cusname_idx`)
) ENGINE=InnoDB AUTO_INCREMENT=2546101 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
可以看到score列有索引,如果能采用這個列的索引是個比較好的選擇,去掉強制索引看下執行計劃
mysql> explain select * from customers where `type` = 1 AND `status` < 7 AND `isarea` = 8 AND `into_time`<='2017-01-31 23:59:59' order by score desc limit 0,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 | 270 | 0.92 | Using where |
+----+-------------+-----------+------------+-------+-----------------------------------------------------------------+-------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)
可以看到用了score索引,執行時間從最9秒多,到優化后的0.0幾秒。
以上是“MySQL中SQL語句優化的示例分析”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注創新互聯行業資訊頻道!
文章名稱:MySQL中SQL語句優化的示例分析
網頁路徑:http://www.yijiale78.com/article34/pehdpe.html
成都網站建設公司_創新互聯,為您提供定制網站、網站內鏈、建站公司、域名注冊、全網營銷推廣、App設計
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯