MySQL count(*)之索引选择

发布时间:2020-08-06 21:07:43 作者:StevenBeijing
阅读:360
mysql云数据库,弹性扩容,低至0.3元/天! 查看>>

覆盖索引对于一些统计问题,如下:

MySQL > show create table test1 \G
*************************** 1. row ***************************
       Table: test1
Create Table: CREATE TABLE `test1` (
  `id` bigint(16) NOT NULL AUTO_INCREMENT,
  `order_seq` bigint(16) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_id` (`id`),
  KEY `idx_id_ordseq` (`id`,`order_seq`)
) ENGINE=InnoDB AUTO_INCREMENT=15002212 DEFAULT CHARSET=utf8mb4
1 row in set (0.00 sec)
MySQL > explain select count(*) from test1 where id>10000 and id<20000;
+----+-------------+-------+------------+-------+------------------------------+--------+---------+------+------+----------+--------------------------+
| id | select_type | table | partitions | type  | possible_keys                | key    | key_len | ref  | rows | filtered | Extra                    |
+----+-------------+-------+------------+-------+------------------------------+--------+---------+------+------+----------+--------------------------+
|  1 | SIMPLE      | test1 | NULL       | range | PRIMARY,idx_id,idx_id_ordseq | idx_id | 8       | NULL | 9999 |   100.00 | Using where; Using index |
+----+-------------+-------+------------+-------+------------------------------+--------+---------+------+------+----------+--------------------------+
1 row in set, 1 warning (0.00 sec)

innodb存储引擎选择了id字段的辅助索引,而不是聚集索引来统计,更不是联合索引。原因是辅助索引远小于聚集索引,选择辅助索引可以减少IO资源消耗。

而另外一个统计场景:

select count(*) from test1 where order_seq > 1502131212577 and order_seq< 202007080947244761;

test1表建有id和 order_seq 字段的联合索引。

MySQL > show create table test1 \G
*************************** 1. row ***************************
       Table: test1
Create Table: CREATE TABLE `test1` (
  `id` bigint(16) NOT NULL AUTO_INCREMENT,
  `order_seq` bigint(16) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_id` (`id`),
  KEY `idx_id_ordseq` (`id`,`order_seq`)
) ENGINE=InnoDB AUTO_INCREMENT=15002212 DEFAULT CHARSET=utf8mb4
1 row in set (0.00 sec)
MySQL > explain select count(*) from test1 where order_seq > 1502131212577 and order_seq< 202007080947244761;
+----+-------------+-------+------------+-------+---------------+---------------+---------+------+----------+----------+--------------------------+
| id | select_type | table | partitions | type  | possible_keys | key           | key_len | ref  | rows     | filtered | Extra                    |
+----+-------------+-------+------------+-------+---------------+---------------+---------+------+----------+----------+--------------------------+
|  1 | SIMPLE      | test1 | NULL       | index | NULL          | idx_id_ordseq | 16      | NULL | 15068082 |    11.11 | Using where; Using index |
+----+-------------+-------+------------+-------+---------------+---------------+---------+------+----------+----------+--------------------------+
1 row in set, 1 warning (0.00 sec)

这里使用条件 order_seq 查询,一般情况下使用不了联合索引的,但是这个案例中的查询,利用到覆盖索引的信息。possible_keys依然为null,但是key是idx_id_ordseq,extra里出现Using index,表示为覆盖索引。

亿速云「云数据库 MySQL」免部署即开即用,比自行安装部署数据库高出1倍以上的性能,双节点冗余防止单节点故障,数据自动定期备份随时恢复。点击查看>>

推荐阅读:
  1. MySQL 5.7下InnoDB对COUNT(*)的优化
  2. MySQL中如何实现无过滤条件的count

开发者交流群:

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

原文链接:http://blog.itpub.net/30135314/viewspace-2707939/

mysql count 索引

上一篇:用js给frames赋值

下一篇:关于判断web是手机端还是电脑端访问以及复制删除按钮的实现

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》
开发者交流群×