溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

MySQL中覆蓋索引怎么用

發布時間:2021-11-02 17:23:18 來源:億速云 閱讀:221 作者:小新 欄目:MySQL數據庫

這篇文章主要為大家展示了“MySQL中覆蓋索引怎么用”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“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。

以上是“MySQL中覆蓋索引怎么用”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女