這篇文章主要為大家展示了“MySQL中覆蓋索引怎么用”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“MySQL中覆蓋索引怎么用”這篇文章吧。
成都創(chuàng)新互聯(lián)公司主要從事網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)鎮(zhèn)江,10余年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來(lái)電咨詢建站服務(wù):028-86922220
查看測(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)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
當(dāng)前標(biāo)題:MySQL中覆蓋索引怎么用
瀏覽路徑:http://jinyejixie.com/article36/pshjsg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、網(wǎng)站導(dǎo)航、網(wǎng)頁(yè)設(shè)計(jì)公司、服務(wù)器托管、電子商務(wù)、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)