您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“MySQL中覆蓋索引怎么用”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“MySQL中覆蓋索引怎么用”這篇文章吧。
查看測(cè)試表結(jié)構(gòu):
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執(zhí)行效果:
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)
查看執(zhí)行計(jì)劃:
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)
創(chuàng)建覆蓋索引:
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執(zhí)行效果:
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)
查看執(zhí)行計(jì)劃:
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)
發(fā)現(xiàn)優(yōu)化器選擇了新建的兩個(gè)覆蓋索引。
創(chuàng)建覆蓋索引之后,利用索引的有序性, select max(id)可以快速的取到最大id。
以上是“MySQL中覆蓋索引怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。