在实际开发过程中,统计一个表的数据量是经常遇到的需求,用来统计数据库表的行数都会使用COUNT(*),COUNT(1)或者COUNT(字段),但是表中的记录越来越多,使用COUNT(*)也会变得越来越慢,本文我们就来分析一下COUNT的性能到底如何。
执行效果:
实验分析
[root@zhyno1 ~]# cat /etc/system-release CentOS Linux release 7.9.2009 (Core) [root@zhyno1 ~]# uname -a Linux zhyno1 3.10.0-1160.62.1.el7.x86_64 #1 SMP Tue Apr 5 16:57:59 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
(Mon Jul 25 09:41:39 2022)[root@GreatSQL][(none)]>select version(); +-----------+ | version() | +-----------+ | 8.0.25-16 | +-----------+ 1 row in set (0.00 sec)
实验开始:
#首先我们创建一个实验表 CREATE TABLE test_count ( `id` int(10) NOT NULL AUTO_INCREMENT PRIMARY KEY, `name` varchar(20) NOT NULL, `salary` int(1) NOT NULL, KEY `idx_salary` (`salary`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; #插入1000W条数据 DELIMITER // CREATE PROCEDURE insert_1000w() BEGIN DECLARE i INT; SET i=1; WHILE i<=10000000 DO INSERT INTO test_count(name,salary) VALUES('KAiTO',1); SET i=i+1; END WHILE; END// DELIMITER ; #执行存储过程 call insert_1000w();
接下来我们分别来实验一下:
(Sat Jul 23 22:56:04 2022)[root@GreatSQL][test]>select count(1) from test_count; +----------+ | count(1) | +----------+ | 10000000 | +----------+ 1 row in set (4.19 sec)
(Sat Jul 23 22:57:41 2022)[root@GreatSQL][test]>select count(*) from test_count; +----------+ | count(*) | +----------+ | 10000000 | +----------+ 1 row in set (4.16 sec)
(Sat Jul 23 22:58:56 2022)[root@GreatSQL][test]>select count(id) from test_count; +-----------+ | count(id) | +-----------+ | 10000000 | +-----------+ 1 row in set (4.23 sec)
我们可以再来测试一下执行计划
(Sat Jul 23 22:59:16 2022)[root@GreatSQL][test]>explain select count(*) from test_count; +----+-------------+------------+------------+-------+---------------+------------+---------+------+---------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+------------+------------+-------+---------------+------------+---------+------+---------+----------+-------------+ | 1 | SIMPLE | test_count | NULL | index | NULL | idx_salary | 4 | NULL | 9980612 | 100.00 | Using index | +----+-------------+------------+------------+-------+---------------+------------+---------+------+---------+----------+-------------+ 1 row in set, 1 warning (0.01 sec) (Sat Jul 23 22:59:48 2022)[root@GreatSQL][test]>show warnings; +-------+------+-----------------------------------------------------------------------+ | Level | Code | Message | +-------+------+-----------------------------------------------------------------------+ | Note | 1003 | /* select#1 */ select count(0) AS `count(*)` from `test`.`test_count` | +-------+------+-----------------------------------------------------------------------+ 1 row in set (0.00 sec)
(Sat Jul 23 23:12:45 2022)[root@GreatSQL][test]>explain select count(1) from test_count; +----+-------------+------------+------------+-------+---------------+------------+---------+------+---------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+------------+------------+-------+---------------+------------+---------+------+---------+----------+-------------+ | 1 | SIMPLE | test_count | NULL | index | NULL | idx_salary | 4 | NULL | 9980612 | 100.00 | Using index | +----+-------------+------------+------------+-------+---------------+------------+---------+------+---------+----------+-------------+ 1 row in set, 1 warning (0.00 sec) (Sat Jul 23 23:13:02 2022)[root@GreatSQL][test]>show warnings; +-------+------+-----------------------------------------------------------------------+ | Level | Code | Message | +-------+------+-----------------------------------------------------------------------+ | Note | 1003 | /* select#1 */ select count(1) AS `count(1)` from `test`.`test_count` | +-------+------+-----------------------------------------------------------------------+ 1 row in set (0.00 sec)
(Sat Jul 23 23:13:14 2022)[root@GreatSQL][test]>explain select count(id) from test_count; +----+-------------+------------+------------+-------+---------------+------------+---------+------+---------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+------------+------------+-------+---------------+------------+---------+------+---------+----------+-------------+ | 1 | SIMPLE | test_count | NULL | index | NULL | idx_salary | 4 | NULL | 9980612 | 100.00 | Using index | +----+-------------+------------+------------+-------+---------------+------------+---------+------+---------+----------+-------------+ 1 row in set, 1 warning (0.00 sec) (Sat Jul 23 23:13:29 2022)[root@GreatSQL][test]>show warnings; +-------+------+-----------------------------------------------------------------------------------------------+ | Level | Code | Message | +-------+------+-----------------------------------------------------------------------------------------------+ | Note | 1003 | /* select#1 */ select count(`test`.`test_count`.`id`) AS `count(id)` from `test`.`test_count` | +-------+------+-----------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
需要注意的是COUNT里如果是非主键字段的话
(Tue Jul 26 14:01:57 2022)[root@GreatSQL][test]>explain select count(name) from test_count where id <100 ; +----+-------------+------------+------------+-------+---------------+---------+---------+------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+------------+------------+-------+---------------+---------+---------+------+------+----------+-------------+ | 1 | SIMPLE | test_count | NULL | range | PRIMARY | PRIMARY | 4 | NULL | 99 | 100.00 | Using where | +----+-------------+------------+------------+-------+---------------+---------+---------+------+------+----------+-------------+ 1 row in set, 1 warning (0.00 sec)
实验结果
实验结论
总结:COUNT(*)=COUNT(1)>COUNT(id)
MySQL的官方文档也有说过:
InnoDB handles SELECT COUNT(*) and SELECT COUNT(1) operations in the same way. There is no performance difference
翻译: InnoDB以相同的方式处理SELECT COUNT(*)和SELECT COUNT(1)操作。没有性能差异
所以说明了对于COUNT(1)或者是COUNT(*),MySQL的优化其实是完全一样的,没有存在没有性能的差异。
但是建议使用COUNT(*),因为这是MySQL92定义的标准统计行数的语法。
在InnoDB中,MySQL数据库每个表占用的空间、表记录的行数可以打开MySQL的information_schema数据库。在该库中有一个TABLES表,这个表主要字段分别是:
TABLE_ROWS用于显示这个表当前有多少行,这个命令执行挺快的,那这个TABLE_ROWS能代替COUNT(*)吗?
我们用TABLES_ROWS查询一下表记录条数
(Sat Jul 23 23:15:14 2022)[root@GreatSQL][test]>SELECT TABLE_ROWS -> FROM INFORMATION_SCHEMA.TABLES -> WHERE TABLE_NAME = 'test_count'; +------------+ | TABLE_ROWS | +------------+ | 9980612 | +------------+ 1 row in set (0.03 sec)
可以看到,记录的条数并不准确,因为InnoDB引擎下TABLES_ROWS行计数仅是大概估计值。
首先要明确的是,MySQL有多种不同引擎,在不同的引擎中,COUNT(*)有不同的实现方式,本文主要介绍的是在InnoDB引擎上的执行流程
在InnoDB存储引擎中,COUNT(*)函数是先从内存中读取表中的数据到内存缓冲区,然后扫描全表获得行记录数的。简单来说就是全表扫描,一个循环解决问题,循环内: 先读取一行,再决定该行是否计入COUNT,循环内是一行一行进行计数处理的。
在MyISAM引擎中是把一个表的总行数存在了磁盘上,因此执行COUNT(*)的时候会直接返回这个数,效率很高。
之所以InnoDB 不跟 MyISAM一样把数字存起来,是因为即使是在同一个时刻的多个查询,由于多版本并发控制(MVCC)的原因,InnoDB表应该返回多少行也是不确定的。而且不论是在事务支持、并发能力还是在数据安全方面,InnoDB都优于MyISAM。
虽然如此,InnoDB对于COUNT(*)操作还是做了优化的。InnoDB是索引组织表,主键索引树的叶子节点是数据,而普通索引树的叶子节点是主键值。所以,普通索引树比主键索引树小很多。对于COUNT(*)这样的操作,遍历哪个索引树得到的结果逻辑上都是一样的。因此,MySQL 优化器会找到最小的那棵树来遍历。
需要注意的是我们在这篇文章里讨论的是没有过滤条件的COUNT(*),如果加了WHERE条件的话,MyISAM引擎的表也是不能返回得这么快的。