前言:今天在生產(chǎn)環(huán)境抓到慢SQL一例亏娜,單獨拿出來執(zhí)行速度非常快蹬挺,我們拿出該SQL分析一下维贺。
原始SQL:
select distinct metadatalogid,opertime from xwcmmetadatalog where metadataid='4395597' order by opertime desc;
執(zhí)行計劃:
MariaDB [test]> explain select distinct metadatalogid,opertime from xwcmmetadatalog where metadataid='4395597' order by opertime desc;
+------+-------------+-----------------+------+-------------------------------+-------------------------------+---------+-------+------+-----------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-----------------+------+-------------------------------+-------------------------------+---------+-------+------+-----------------------------+
| 1 | SIMPLE | xwcmmetadatalog | ref | IX_xwcmmetadatalog_METADATAID | IX_xwcmmetadatalog_METADATAID | 5 | const | 1 | Using where; Using filesort |
+------+-------------+-----------------+------+-------------------------------+-------------------------------+---------+-------+------+-----------------------------+
1 row in set (0.00 sec)
我們可以看到該執(zhí)行計劃其實問題也不是很大,唯一需要注意的就是用到了文件排序巴帮,當MySQL SERVER io比較吃緊的時候大量執(zhí)行該SQL就會有問題了溯泣,所以我們優(yōu)化思路就是消除這個文件排序即可。
查看索引
MariaDB [test]> show index from xwcmmetadatalog;
+-----------------+------------+----------------------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-----------------+------------+----------------------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| xwcmmetadatalog | 0 | PRIMARY | 1 | METADATALOGID | A | 0 | NULL | NULL | | BTREE | | |
| xwcmmetadatalog | 1 | IX_xwcmmetadatalog_SRCMETADATAID | 1 | SRCMETADATAID | A | 0 | NULL | NULL | YES | BTREE | | |
| xwcmmetadatalog | 1 | IX_xwcmmetadatalog_METADATAID | 1 | METADATAID | A | 0 | NULL | NULL | YES | BTREE | | |
+-----------------+------------+----------------------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.00 sec)
我們看到執(zhí)行計劃用到的索引只包含METADATAID列榕茧,而我們的sql篩選條件不僅包含metadataid='4395597' 還用到了opertime來排序垃沦,所以我們針對這兩個字段建立索引即可。
MariaDB [test]> create index idx_medataid_opertime on xwcmmetadatalog(metadataid,opertime);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
再次查看執(zhí)行計劃:
MariaDB [test]> explain select distinct metadatalogid,opertime from xwcmmetadatalog where metadataid='4395597' order by opertime desc;
+------+-------------+-----------------+------+-----------------------------------------------------+-----------------------+---------+-------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-----------------+------+-----------------------------------------------------+-----------------------+---------+-------+------+--------------------------+
| 1 | SIMPLE | xwcmmetadatalog | ref | IX_xwcmmetadatalog_METADATAID,idx_medataid_opertime | idx_medataid_opertime | 5 | const | 1 | Using where; Using index |
+------+-------------+-----------------+------+-----------------------------------------------------+-----------------------+---------+-------+------+--------------------------+
1 row in set (0.00 sec)
完美