mysql中innodb表的count

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群版 2核4GB 100GB
推荐场景:
搭建个人博客
云数据库 RDS MySQL,高可用版 2核4GB 50GB
简介:

其实就一个问题:count(1),count(*),count(主键),count(非主键字段)哪个更快?

于是建了一张测试表,插入了600多万条记录。表结构如下:

 
 
  1. CREATE TABLE `test` ( 
  2.   `id` int(10) unsigned NOT NULL AUTO_INCREMENT, 
  3.   `c1` varchar(10) NOT NULL, 
  4.   `c2` varchar(10) NOT NULL, 
  5.   `c3` varchar(10) NOT NULL, 
  6.   PRIMARY KEY (`id`) 
  7. ENGINE=InnoDB AUTO_INCREMENT=6003060 DEFAULT CHARSET=utf8 

看看下面三条sql:

  • 1.select count(1) from test;
  • 2.select count(*) from test;
  • 3.select count(id) from test;
  • 4.select count(c1) from test;

下面是命令行下的执行情况:

 
 
  1. mysql> select count(1) from test; 
  2. +----------+ 
  3. | count(1) | 
  4. +----------+ 
  5. |  6003058 | 
  6. +----------+ 
  7. 1 row in set (1.74 sec) 
 
 
  1. mysql> select count(*) from test; 
  2. +----------+ 
  3. | count(*) | 
  4. +----------+ 
  5. |  6003058 | 
  6. +----------+ 
  7. 1 row in set (1.75 sec) 
 
 
  1. mysql> select count(id) from test; 
  2. +-----------+ 
  3. | count(id) | 
  4. +-----------+ 
  5. |   6003058 | 
  6. +-----------+ 
  7. 1 row in set (1.87 sec) 
 
 
  1. mysql> select count(c1) from test; 
  2. +-----------+ 
  3. | count(c1) | 
  4. +-----------+ 
  5. |   6003058 | 
  6. +-----------+ 
  7. 1 row in set (2.02 sec) 

从上面的执行时间看,前两条相差不多,第三条次之,最差的是第四条,看看explain的结果:

 
 
  1. mysql> explain select count(1) from test;  
  2. +----+-------------+-------+-------+---------------+---------+---------+------+---------+-------------+ 
  3. | id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows    | Extra       | 
  4. +----+-------------+-------+-------+---------------+---------+---------+------+---------+-------------+ 
  5. |  1 | SIMPLE      | test  | index | NULL          | PRIMARY | 4       | NULL | 6003369 | Using index | 
  6. +----+-------------+-------+-------+---------------+---------+---------+------+---------+-------------+ 
  7. 1 row in set (0.00 sec) 
 
 
  1. mysql> explain select count(*) from test; 
  2. +----+-------------+-------+-------+---------------+---------+---------+------+---------+-------------+ 
  3. | id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows    | Extra       | 
  4. +----+-------------+-------+-------+---------------+---------+---------+------+---------+-------------+ 
  5. |  1 | SIMPLE      | test  | index | NULL          | PRIMARY | 4       | NULL | 6003369 | Using index | 
  6. +----+-------------+-------+-------+---------------+---------+---------+------+---------+-------------+ 
  7. 1 row in set (0.00 sec) 
 
 
  1. mysql> explain select count(id) from test; 
  2. +----+-------------+-------+-------+---------------+---------+---------+------+---------+-------------+ 
  3. | id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows    | Extra       | 
  4. +----+-------------+-------+-------+---------------+---------+---------+------+---------+-------------+ 
  5. |  1 | SIMPLE      | test  | index | NULL          | PRIMARY | 4       | NULL | 6003369 | Using index | 
  6. +----+-------------+-------+-------+---------------+---------+---------+------+---------+-------------+ 
  7. 1 row in set (0.00 sec) 
 
 
  1. mysql> explain select count(c1) from test; 
  2. +----+-------------+-------+------+---------------+------+---------+------+---------+-------+ 
  3. | id | select_type | table | type | possible_keys | key  | key_len | ref  | rows    | Extra | 
  4. +----+-------------+-------+------+---------------+------+---------+------+---------+-------+ 
  5. |  1 | SIMPLE      | test  | ALL  | NULL          | NULL | NULL    | NULL | 6003369 |       | 
  6. +----+-------------+-------+------+---------------+------+---------+------+---------+-------+ 
  7. 1 row in set (0.00 sec) 

由上可以看出,前三者的执行计划是一样的,第四条与前三者不同,没有使用覆盖索引,从执行时间上排序为:

count(1)=count(*) < count(主键) < count(非主键字段)

 

上面是未指定条件的情况,如果加上条件呢,看看下面三条语句:

  • 1.select count(1) from test where id>0;
  • 2.select count(*) from test where id>0;
  • 3.select count(id) from test where id>0;
  • 4.select count(c1) from test where id>0;

下面是命令行下的执行情况:

 
 
  1. mysql> select count(1) from test where id>0; 
  2. +----------+ 
  3. | count(1) | 
  4. +----------+ 
  5. |  6003058 | 
  6. +----------+ 
  7. 1 row in set (2.07 sec) 
 
 
  1. mysql> select count(*) from test where id>0; 
  2. +----------+ 
  3. | count(*) | 
  4. +----------+ 
  5. |  6003058 | 
  6. +----------+ 
  7. 1 row in set (2.07 sec) 
 
 
  1. mysql> select count(id) from test where id>0; 
  2. +-----------+ 
  3. | count(id) | 
  4. +-----------+ 
  5. |   6003058 | 
  6. +-----------+ 
  7. 1 row in set (2.07 sec) 
 
 
  1. mysql> select count(c1) from test where id>0; 
  2. +-----------+ 
  3. | count(c1) | 
  4. +-----------+ 
  5. |   6003058 | 
  6. +-----------+ 
  7. 1 row in set (2.28 sec) 

从上面的执行时间可以看到:

  • 1.count(1),count(*),count(主键)在执行时间上是差不多的,没有明显的差别
  • 2.count(非主键字段)的执行时间明显要比前三者长。

再看一下explain的结果:

 
 
  1. mysql> explain select count(1) from test where id>0; 
  2. +----+-------------+-------+-------+---------------+---------+---------+------+---------+--------------------------+ 
  3. | id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows    | Extra                    | 
  4. +----+-------------+-------+-------+---------------+---------+---------+------+---------+--------------------------+ 
  5. |  1 | SIMPLE      | test  | range | PRIMARY       | PRIMARY | 4       | NULL | 3001684 | Using where; Using index | 
  6. +----+-------------+-------+-------+---------------+---------+---------+------+---------+--------------------------+ 
  7. 1 row in set (0.00 sec) 
 
 
  1. mysql> explain select count(*) from test where id>0; 
  2. +----+-------------+-------+-------+---------------+---------+---------+------+---------+--------------------------+ 
  3. | id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows    | Extra                    | 
  4. +----+-------------+-------+-------+---------------+---------+---------+------+---------+--------------------------+ 
  5. |  1 | SIMPLE      | test  | range | PRIMARY       | PRIMARY | 4       | NULL | 3001684 | Using where; Using index | 
  6. +----+-------------+-------+-------+---------------+---------+---------+------+---------+--------------------------+ 
  7. 1 row in set (0.00 sec) 
 
 
  1. mysql> explain select count(id) from test where id>0; 
  2. +----+-------------+-------+-------+---------------+---------+---------+------+---------+--------------------------+ 
  3. | id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows    | Extra                    | 
  4. +----+-------------+-------+-------+---------------+---------+---------+------+---------+--------------------------+ 
  5. |  1 | SIMPLE      | test  | range | PRIMARY       | PRIMARY | 4       | NULL | 3001684 | Using where; Using index | 
  6. +----+-------------+-------+-------+---------------+---------+---------+------+---------+--------------------------+ 
  7. 1 row in set (0.00 sec) 
 
 
  1. mysql> explain select count(c1) from test where id>0; 
  2. +----+-------------+-------+-------+---------------+---------+---------+------+---------+-------------+ 
  3. | id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows    | Extra       | 
  4. +----+-------------+-------+-------+---------------+---------+---------+------+---------+-------------+ 
  5. |  1 | SIMPLE      | test  | range | PRIMARY       | PRIMARY | 4       | NULL | 3001684 | Using where | 
  6. +----+-------------+-------+-------+---------------+---------+---------+------+---------+-------------+ 
  7. 1 row in set (0.00 sec) 

从上面的explain可以看到,前三种情况都会发生using index,即覆盖索引。

  • Using index:列数据是从仅仅使用了索引中的信息而没有读取实际的行动的表返回的,这发生在对表的全部的请求列都是同一个索引的部分的时候

而最后的情况不会覆盖索引,会进行表读取,故速度会比前三者要慢,耗时更长就能理解了。

结论:

  • 1.在mysql中,对于表类型innodb的情况,如果无条件,count(1),count(*)没有区别。
  • 2.如果有条件,则count(1),count(*),count(主键)没有区别
  • 3.对于count(非主键字段)的情况因为没有使用覆盖索引,耗时会比前三者更长,应避免这种写法。
  • 4.为了安全起见,建议只使用count(1)或count(*)进行统计,两者完全等价。









本文转自 ustb80 51CTO博客,原文链接:http://blog.51cto.com/ustb80/1049001,如需转载请自行联系原作者
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
3天前
|
关系型数据库 MySQL 数据库
数据迁移脚本优化过程:从 MySQL 到 Django 模型表
在大规模的数据迁移过程中,性能问题往往是开发者面临的主要挑战之一。本文将分析一个数据迁移脚本的优化过程,展示如何从 MySQL 数据库迁移数据到 Django 模型表,并探讨优化前后的性能差异。
|
2天前
|
存储 关系型数据库 分布式数据库
PolarDB产品使用问题之如何用InnoDB引擎创建Federated表
PolarDB产品使用合集涵盖了从创建与管理、数据管理、性能优化与诊断、安全与合规到生态与集成、运维与支持等全方位的功能和服务,旨在帮助企业轻松构建高可用、高性能且易于管理的数据库环境,满足不同业务场景的需求。用户可以通过阿里云控制台、API、SDK等方式便捷地使用这些功能,实现数据库的高效运维与持续优化。
7 1
|
7天前
|
存储 关系型数据库 MySQL
关系型数据库mysql的InnoDB
【6月更文挑战第17天】
16 3
|
2天前
|
SQL 关系型数据库 MySQL
ClickHouse(23)ClickHouse集成Mysql表引擎详细解析
ClickHouse的MySQL引擎允许执行`SELECT`查询从远程MySQL服务器。使用`MySQL(&#39;host:port&#39;, &#39;database&#39;, &#39;table&#39;, &#39;user&#39;, &#39;password&#39;[,...])`格式连接,支持简单`WHERE`子句在MySQL端处理,复杂条件和`LIMIT`在ClickHouse端执行。不支持`NULL`值,用默认值替换。系列文章涵盖ClickHouse安装、集群搭建、表引擎解析等主题。[链接](https://zhangfeidezhu.com/?p=468)有更多
8 0
|
4天前
|
关系型数据库 MySQL 调度
深入理解MySQL InnoDB线程模型
深入理解MySQL InnoDB线程模型
|
4天前
|
存储 关系型数据库 MySQL
mysql的InnoDB引擎实现ACID特性的原理
mysql的InnoDB引擎实现ACID特性的原理
|
4天前
|
SQL 缓存 关系型数据库
MySQL操作全攻略:库、表、数据、事务全面指南
MySQL操作全攻略:库、表、数据、事务全面指南
|
5天前
|
SQL 关系型数据库 MySQL
经验大分享:MySQL(三)数据库表的查询操作【重要】
经验大分享:MySQL(三)数据库表的查询操作【重要】
17 0
|
3天前
|
存储 关系型数据库 MySQL
|
3天前
|
存储 SQL 关系型数据库