MySQL優化之覆蓋索引的使用
檢視測試表結構:
mysql> show create table im_message \G *************************** 1. row *************************** Table: im_message Create Table: CREATE TABLE `im_message` ( `id` int(11) NOT NULL AUTO_INCREMENT, `from_id` varchar(40) COLLATE utf8_bin NOT NULL, `from_type` tinyint(1) NOT NULL DEFAULT '0', `to_id` varchar(40) COLLATE utf8_bin NOT NULL, `to_type` tinyint(1) NOT NULL DEFAULT '0', `content` varchar(2048) COLLATE utf8_bin DEFAULT '', `create_date` bigint(20) NOT NULL, `update_date` bigint(20) NOT NULL, `message_id` varchar(40) COLLATE utf8_bin NOT NULL, `is_sync` tinyint(1) DEFAULT '0' COMMENT '是否同步 0 未同步 1 已同步', `is_read` tinyint(1) DEFAULT '0' COMMENT '是否已讀 0 未讀 1 已讀', `is_withdraw` tinyint(1) DEFAULT '0' COMMENT '是否撤回 0 未撤 1 已撤', `is_lastest` tinyint(1) DEFAULT '0' COMMENT '是否是最新回話訊息 0 不是 1是', PRIMARY KEY (`id`), UNIQUE KEY `uidx_message_id` (`message_id`), KEY `idx_date` (`create_date`), KEY `idx_from_id` (`from_id`), KEY `idx_to_id` (`to_id`), KEY `idx_is_sync` (`is_sync`), KEY `idx_update_date` (`update_date`), KEY `idx_fid_tid` (`from_id`,`to_id`) ) ENGINE=InnoDB AUTO_INCREMENT=13264365 DEFAULT CHARSET=utf8 COLLATE=utf8_bin ROW_FORMAT=DYNAMIC 1 row in set (0.00 sec)
檢視SQL執行效果:
select max(id) as "id" from im_message WHERE from_id='bd29879279c44672a2fdffcb8428b4d1' or to_id='bd29879279c44672a2fdffcb8428b4d1' group by from_id, to_id; | 8547247 | | 7152294 | | 6897129 | | 12874034 | | 10011290 | | 8027198 | | 7852741 | | 9960496 | | 6059399 | | 10860981 | | 9963172 | | 13253445 | +----------+ 27827 rows in set (0.91 sec)
檢視執行計劃:
mysql> explain select max(id) as "id" from im_message WHERE from_id='bd29879279c44672a2fdffcb8428b4d1' or to_id='bd29879279c44672a2fdffcb8428b4d1' group by from_id, to_id;
+----+-------------+------------+------------+-------------+-----------------------------------+-----------------------+---------+------+--------+----------+---------------------------------------------------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+------------+------------+-------------+-----------------------------------+-----------------------+---------+------+--------+----------+---------------------------------------------------------------------------------------+
| 1 | SIMPLE | im_message | NULL | index_merge | idx_from_id,idx_to_id,idx_fid_tid | idx_fid_tid,idx_to_id | 122,122 | NULL | 168680 | 100.00 | Using sort_union(idx_fid_tid,idx_to_id); Using where; Using temporary; Using filesort |
+----+-------------+------------+------------+-------------+-----------------------------------+-----------------------+---------+------+--------+----------+---------------------------------------------------------------------------------------+
1 row in set, 1 warning (0.00 sec)
建立覆蓋索引:
mysql> alter table im_message add index idx_from_id_id(from_id,id); Query OK, 0 rows affected (1 min 1.94 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> alter table im_message add index idx_to_id_id(to_id,id); Query OK, 0 rows affected (1 min 9.79 sec) Records: 0 Duplicates: 0 Warnings: 0
重新檢視SQL執行效果:
select max(id) as "id" from im_message WHERE from_id='bd29879279c44672a2fdffcb8428b4d1' or to_id='bd29879279c44672a2fdffcb8428b4d1' group by from_id, to_id; | 8547247 | | 7152294 | | 6897129 | | 12874034 | | 10011290 | | 8027198 | | 7852741 | | 9960496 | | 6059399 | | 10860981 | | 9963172 | | 13253445 | +----------+ 27827 rows in set (0.63 sec)
檢視執行計劃:
mysql> explain select max(id) as "id" from im_message WHERE from_id='bd29879279c44672a2fdffcb8428b4d1' or to_id='bd29879279c44672a2fdffcb8428b4d1' group by from_id, to_id; +----+-------------+------------+------------+-------------+---------------------------------------------------------------+-----------------------------+---------+------+--------+----------+----------------------------------------------------------------------------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+------------+------------+-------------+---------------------------------------------------------------+-----------------------------+---------+------+--------+----------+----------------------------------------------------------------------------------------+ | 1 | SIMPLE | im_message | NULL | index_merge | idx_from_id,idx_to_id,idx_fid_tid,idx_from_id_id,idx_to_id_id | idx_from_id_id,idx_to_id_id | 122,122 | NULL | 162106 | 100.00 | Using union(idx_from_id_id,idx_to_id_id); Using where; Using temporary; Using filesort | +----+-------------+------------+------------+-------------+---------------------------------------------------------------+-----------------------------+---------+------+--------+----------+----------------------------------------------------------------------------------------+ 1 row in set, 1 warning (0.00 sec)
發現優化器選擇了新建的兩個覆蓋索引。
建立覆蓋索引之後,利用索引的有序性, select max(id)可以快速的取到最大id。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/30135314/viewspace-2654606/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 【MySQL】三、效能優化之 覆蓋索引MySql優化索引
- Mysql索引覆蓋MySql索引
- MySQL 索引覆蓋(Covering Index)MySql索引Index
- MySQL 聚簇索引 和覆蓋索引MySql索引
- mysql覆蓋索引之看山還是山MySql索引
- MySQL SQL最佳化 - 覆蓋索引(covering index)MySql索引Index
- MySQL 的覆蓋索引與回表MySql索引
- mysql覆蓋索引高效能的探究MySql索引
- Mysql索引優化之索引的分類MySql索引優化
- MySQL 覆蓋索引、回表查詢MySql索引
- MySQL調優之索引優化MySql索引優化
- MySQL優化之索引解析MySql優化索引
- mysql索引的使用和優化MySql索引優化
- 技術分享 | MySQL 覆蓋索引最佳化案例一則MySql索引
- MySQL-覆蓋索引總結筆記MySql索引筆記
- MySQL 索引使用策略及優化MySql索引優化
- MySQL效能優化之索引設計MySql優化索引
- 什麼是覆蓋索引?索引
- MySQL索引優化MySql索引優化
- mysql優化索引MySql優化索引
- 【離散優化】覆蓋問題優化
- MySQL優化學習筆記之索引MySql優化筆記索引
- 測試覆蓋率 之 Cobertura的使用
- Mysql索引優化(一)MySql索引優化
- 還傻傻分不清MySQL回表查詢與索引覆蓋?MySql索引
- 《MySQL慢查詢優化》之SQL語句及索引優化MySql優化索引
- MySQL 優化之 index_merge (索引合併)MySql優化Index索引
- MySQL 筆記 - 索引優化MySql筆記索引優化
- MySQL——索引優化實戰MySql索引優化
- MySQL 字串索引優化方案MySql字串索引優化
- MySQL 索引原理以及優化MySql索引優化
- mysql索引原理及優化MySql索引優化
- 淺談MySQL的B樹索引與索引優化MySql索引優化
- MySQL 調優之如何正確使用聯合索引MySql索引
- 一篇文章講清楚MySQL的聚簇/聯合/覆蓋索引、回表、索引下推MySql索引
- 一文總結分析聚集索引、非聚集索引、覆蓋索引的工作原理!索引
- 【MySQL】MySQL的執行計劃及索引優化MySql索引優化
- 使用Elasticsearch的動態索引和索引優化Elasticsearch索引優化