十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
这篇文章主要为大家展示了“MySQL中覆盖索引怎么用”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“MySQL中覆盖索引怎么用”这篇文章吧。
成都创新互联公司主要从事网站设计、网站建设、网页设计、企业做网站、公司建网站等业务。立足成都服务镇江,10余年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:028-86922220
查看测试表结构:
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。
以上是“MySQL中覆盖索引怎么用”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!