您好,欢迎访问一九零五行业门户网

MySQL COUNT(*)性能原理是什么

1.count(1)、count(*)与count(字段)哪个更快?执行效果:
count(*)mysql 对count(*)进行了优化,count(*)直接扫描主键索引记录,并不会把全部字段取出来,直接按行累加。
count(1)innodb引擎遍历整张表,但不取值,server 层对于返回的每一行,放一个数字“1”进去,按行累加。
count(字段)如果这个“字段”是定义为not null,那么innodb 引擎会一行行地从记录里面读出这个字段,server 层判断不能为null,按行累加;如果这个“字段”定义允许为null,那么innodb 引擎会一行行地从记录里面读出这个字段,然后把值取出来再判断一下,不是 null才累加。
实验分析本文测试使用的环境:
[root@zhyno1 ~]# cat /etc/system-releasecentos linux release 7.9.2009 (core)[root@zhyno1 ~]# uname -alinux 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
测试数据库采用的是(存储引擎采用innodb,其它参数默认):
(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();
接下来我们分别来实验一下:
count(1)花费了4.19秒
(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)
count(*)花费了4.16秒
(sat jul 23 22:57:41 2022)[root@greatsql][test]>select count(*) from test_count;+----------+| count(*) |+----------+| 10000000 |+----------+1 row in set (4.16 sec)
count(字段)花费了4.23秒
(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)
我们可以再来测试一下执行计划
count(*)
(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)
count(1)
(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)
count(字段)
(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)
实验结果1.从上面的实验我们可以得出,count(*)和count(1)是最快的,其次是count(id)。
2.count(*)被mysql查询优化器改写成了count(0),并选择了idx_salary索引。
3.count(1)和count(id)都选择了idx_salary索引。
实验结论总结: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定义的标准统计行数的语法。
2.count(*)与tables_rows在innodb中,mysql数据库每个表占用的空间、表记录的行数可以打开mysql的information_schema数据库。在该库中有一个tables表,这个表主要字段分别是:
table_schema : 数据库名
table_name:表名
engine:所使用的存储引擎
tables_rows:记录数
data_length:数据大小
index_length:索引大小
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行计数仅是大概估计值。
3.count(*)是怎么样执行的?首先要明确的是,mysql有多种不同引擎,在不同的引擎中,count(*)有不同的实现方式,本文主要介绍的是在innodb引擎上的执行流程
在innodb存储引擎中,count(*)函数是先从内存中读取表中的数据到内存缓冲区,然后扫描全表获得行记录数的。简单来说就是全表扫描,一个循环解决问题,循环内: 先读取一行,再决定该行是否计入count循环内是一行一行进行计数处理的。
在myisam引擎中是把一个表的总行数存在了磁盘上,因此执行count(*)的时候会直接返回这个数,效率很高。
之所以innodb 不跟 myisam一样把数字存起来,是因为即使是在同一个时刻的多个查询,由于多版本并发控制(mvcc)的原因,innodb表应该返回多少行也是不确定的。无论在事务支持、并发能力还是数据安全方面,innodb都比myisam表现更优。
虽然如此,innodb对于count(*)操作还是做了优化的。innodb是索引组织表,主键索引树的叶子节点是数据,而普通索引树的叶子节点是主键值。所以,普通索引树比主键索引树小很多。对于count(*)这样的操作,遍历哪个索引树得到的结果逻辑上都是一样的。因此,mysql 优化器会找到最小的那棵树来遍历。
需要注意的是我们在这篇文章里讨论的是没有过滤条件的count(*),如果加了where条件的话,myisam引擎的表也是不能返回得这么快的。
4.总结1.count(*)=count(1)>count(id)
2.count函数的用法,主要用于统计表行数。主要用法有count(*)、count(字段)和count(1)
3.因为count(*)是sql92定义的标准统计行数的语法,所以mysql对他进行了很多优化,myisam中会直接把表的总行数单独记录下来供count(*)查询,而innodb则会在扫表的时候选择最小的索引来降低成本。这些优化的前提是没有进行where和group的条件查询。
4.在innodb中count(*)和count(1)实现上没有区别,而且效率一样,但是count(字段)需要进行字段的非null判断,所以效率会低一些。
5.因为count(*)是sql92定义的标准统计行数的语法,并且效率高,所以还是建议使用count(*)查询表的行数。
6.正如前面count(name)的用例那样,在建表过程中需要根据业务需求建立性能较高的索引,同时也要注意避免建立不必要的索引。
以上就是mysql count(*)性能原理是什么的详细内容。
其它类似信息

推荐信息