亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

MySQL中SQL語句優化的示例分析

發布時間:2021-11-06 10:03:34 來源:億速云 閱讀:138 作者:小新 欄目:MySQL數據庫

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

sql如下:sql強制用了into_time索引


  1. # Time: 2017-02-14T11:35:01.594499+08:00

  2. # User@Host: oms_readonly[oms_readonly] @ [10.44.xxx.xxx] Id: 41636892

  3. # Query_time: 9.299612 Lock_time: 0.000124 Rows_sent: 20 Rows_examined: 2439330

  4. SET timestamp=1487043301;

  5. 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;

查看表的相關狀態:


  1. mysql> show table status like 'customers' \G;

  2. *************************** 1. row ***************************

  3.            Name: customers

  4.          Engine: InnoDB

  5.         Version: 10

  6.      Row_format: Dynamic

  7.            Rows: 2504609

  8.  Avg_row_length: 710

  9.     Data_length: 1780383744

  10. Max_data_length: 0

  11.    Index_length: 1253048320

  12.       Data_free: 6291456

  13.  Auto_increment: 2546101

  14.     Create_time: 2017-01-07 01:59:34

  15.     Update_time: 2017-02-14 13:58:17

  16.      Check_time: NULL

  17.       Collation: utf8_general_ci

  18.        Checksum: NULL

  19.  Create_options:

  20.         Comment:

  21. 1 row in set (0.00 sec)

表一共大約有250萬行記錄,查看下滿足into_time<='2017-01-31 23:59:59'這個條件的有多少行


  1. mysql> select count(*) from customers where `into_time`<='2017-01-31 23:59:59';

  2. +----------+

  3. | count(*) |

  4. +----------+

  5. | 2439147 |

  6. +----------+

  7. 1 row in set (0.95 sec)


顯然into_time這個列的索引已經不合適了,查看下表上都有那些索引


  1. Create Table: CREATE TABLE `customers` (

  2.   `id` int(11) NOT NULL AUTO_INCREMENT,

  3.   PRIMARY KEY (`id`),

  4.   KEY `newdata` (`newdata`),

  5.   KEY `cusname` (`cusname`),

  6.   KEY `type` (`type`,`ownerid`),

  7.   KEY `operator` (`operator`),

  8.   KEY `into_time` (`into_time`),

  9.   KEY `isarea` (`isarea`),

  10.   KEY `linkcase` (`linkcase`),

  11.   KEY `score` (`score`),

  12.   FULLTEXT KEY `fdx_cusname` (`cusname_idx`)

  13. ) ENGINE=InnoDB AUTO_INCREMENT=2546101 DEFAULT CHARSET=utf8

  14. 1 row in set (0.00 sec)

可以看到score列有索引,如果能采用這個列的索引是個比較好的選擇,去掉強制索引看下執行計劃





    1. 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;

    2. +----+-------------+-----------+------------+-------+-----------------------------------------------------------------+-------+---------+------+------+----------+-------------+

    3. | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |

    4. +----+-------------+-----------+------------+-------+-----------------------------------------------------------------+-------+---------+------+------+----------+-------------+

    5. | 1 | SIMPLE | customers | NULL | index | type,into_time,isarea,status,idx_isarea_renew_owner,type_status | score | 2 | NULL | 270 | 0.92 | Using where |

    6. +----+-------------+-----------+------------+-------+-----------------------------------------------------------------+-------+---------+------+------+----------+-------------+

    7. 1 row in set, 1 warning (0.00 sec)

可以看到用了score索引,執行時間從最9秒多,到優化后的0.0幾秒。

以上是“MySQL中SQL語句優化的示例分析”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

霸州市| 义乌市| 宁陵县| 广安市| 承德市| 六枝特区| 怀集县| 义乌市| 双峰县| 宕昌县| 县级市| 九龙县| 库伦旗| 五峰| 顺昌县| 温宿县| 土默特右旗| 洛扎县| 新巴尔虎右旗| 澄迈县| 桂阳县| 达州市| 枣强县| 冀州市| 天津市| 赣州市| 桃源县| 香格里拉县| 城固县| 含山县| 平泉县| 翁牛特旗| 宜宾县| 衡山县| 洞头县| 自贡市| 辰溪县| 庆安县| 登封市| 青龙| 揭东县|