查看测试表结构:

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。

Comments are closed.