论证select count(*)和select count(1)
今天看到同事转载的一篇《select count(*)和select count(1)的区别》的博文,兴致所致,就想对文中提出的结论进行验证一下:
从内容来看,主要是有主键和没有主键的影响,那么
第一步:创建test1(有主键),test2(无主键)两张表
CREATE TABLE `test1` (`id` int(12) NOT NULL AUTO_INCREMENT,`value` int(12) NOT NULL,PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=400000 DEFAULT CHARSET=utf8;
CREATE TABLE `test2` (`id` int(12) NOT NULL,`value` int(12) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
第二步:插入40万条数据
DROP PROCEDURE inserttest; CREATE PROCEDURE inserttest() BEGINDECLARE i INT DEFAULT 1;WHILE i < 400000 DO -- test1表为test1,test2表为test2INSERT INTO test1 VALUES(i, i);SET i = i+1;END WHILE; ENDCALL inserttest;
第三步:论证第一个观点“表没有主键的情况下,count(1)比count(*)快”
SELECT SQL_NO_CACHE COUNT(1) as ooo FROM test2; SELECT SQL_NO_CACHE COUNT(*) as aaa FROM test2;
结果数据
次数 | count(1) | count(*) |
1 | 时间: 0.107s | 时间: 0.105s |
2 | 时间: 0.106s | 时间: 0.105s |
3 | 时间: 0.105s | 时间: 0.106s |
4 | 时间: 0.106s | 时间: 0.106s |
5 | 时间: 0.106s | 时间: 0.108s |
6 | 时间: 0.106s | 时间: 0.103s |
7 | 时间: 0.107s | 时间: 0.106s |
8 | 时间: 0.104s | 时间: 0.103s |
9 | 时间: 0.105s | 时间: 0.105s |
10 | 时间: 0.105s | 时间: 0.110s |
结论:该观点不值得相信,从结果数据来看,count(1)比count(*)平均时间可能稍短一点,但是并不总是这样。
第四步:论证第二个观点“表在有主键的情况下,主键作为count条件时最快”
SELECT SQL_NO_CACHE COUNT(1) as ooo FROM test1; SELECT SQL_NO_CACHE COUNT(*) as aaa FROM test1; SELECT SQL_NO_CACHE COUNT(id) as aaa FROM test1;
结果数据(忍不住多列了3个)
次数 | count(1) | count(*) | count(id) |
1 | 时间: 0.052s | 时间: 0.053s | 时间: 0.059s |
2 | 时间: 0.053s | 时间: 0.052s | 时间: 0.060s |
3 | 时间: 0.052s | 时间: 0.052s | 时间: 0.059s |
4 | 时间: 0.052s | 时间: 0.053s | 时间: 0.060s |
5 | 时间: 0.054s | 时间: 0.052s | 时间: 0.059s |
6 | 时间: 0.052s | 时间: 0.054s | 时间: 0.059s |
7 | 时间: 0.051s | 时间: 0.053s | 时间: 0.060s |
8 | 时间: 0.052s | 时间: 0.053s | 时间: 0.061s |
9 | 时间: 0.054s | 时间: 0.053s | 时间: 0.059s |
10 | 时间: 0.071s | 时间: 0.052s | 时间: 0.062s |
11 | 时间: 0.051s | 时间: 0.052s | 时间: 0.059s |
12 | 时间: 0.051s | 时间: 0.052s | 时间: 0.059s |
13 | 时间: 0.052s | 时间: 0.053s | 时间: 0.059s |
第五步:本来想论证“表在只有一个字段的时候count(*)最快”,但是很少情况下表只有一个字段,我就不论证了。
第六步:论证“count(*)和count(1)时统计包括null的,而count(列名)不包含null值”
ALTER TABLE `test1` MODIFY COLUMN `value` int(12) NULL AFTER `id`;
再将其中13行数据修改为null
UPDATE `test1` SET `value`=NULL WHERE (`id`='13')
通过以下语句
SELECT SQL_NO_CACHE COUNT(1) as ooo FROM test1; SELECT SQL_NO_CACHE COUNT(*) as aaa FROM test1; SELECT SQL_NO_CACHE COUNT(id) as aaa FROM test1; SELECT SQL_NO_CACHE COUNT(value) as aaa FROM test1;
数据证据
count(1) | count(*) | count(id) | count(value) |
399999 | 399999 | 399999 | 399998 |
第七步:对于select sum(1)的使用介绍,主要是说sum方法的计算结果是参数值1或者其他和满足条件行数的乘积
次数 | count(1) | count(*) | count(id) | count(value) | SUM(1) |
1 | 时间: 0.052s | 时间: 0.052s | 时间: 0.060s | 时间: 0.091s | 时间: 0.104s |
2 | 时间: 0.054s | 时间: 0.051s | 时间: 0.060s | 时间: 0.090s | 时间: 0.105s |
3 | 时间: 0.053s | 时间: 0.051s | 时间: 0.059s | 时间: 0.093s | 时间: 0.100s |
4 | 时间: 0.052s | 时间: 0.053s | 时间: 0.060s | 时间: 0.094s | 时间: 0.102s |
5 | 时间: 0.051s | 时间: 0.052s | 时间: 0.059s | 时间: 0.091s | 时间: 0.102s |
论证select count(*)和select count(1)相关推荐
- select count(*)和select count(1)
一般情况下,Select Count (*)和Select Count(1)两着返回结果是一样的 假如表沒有主键(Primary key), 那么count(1)比count(*)快, 如果有主键的話 ...
- select count(*)和select count(1)的区别
一般情况下,Select Count (*)和Select Count(1)两着返回结果是一样的 假如表沒有主键(Primary key), 那么count(1)比count(*)快, 如果有主键的話 ...
- mysql select count() count(1)_select count()和select count(1)的区别和执行方式讲解
select count()和select count(1)的区别和执行方式讲解 发布时间:2020-09-06 13:26:14 来源:脚本之家 阅读:227 作者:CODETC 在SQL Serv ...
- select count(*) 和 select count(1) 以及 select count(column) 的区别
考试,目的在于让自己明白,自己天天写的都是垃圾 select count(*) 和 select count(1) 以及 select count(column) 的区别 1.如果表沒有主键, 那么c ...
- mysql count里select_select count()和select count(1)的区别和执行方式讲解
在SQL Server中Count(*)或者Count(1)或者Count([列])或许是最常用的聚合函数.很多人其实对这三者之间是区分不清的.本文会阐述这三者的作用,关系以及背后的原理. 往常我经常 ...
- select * 和select 1,select count(*)和select count(1)
目录 1.select * 和select 1 2.select count(*)和select count(1) 1.select * 和select 1 语法:select * from 表名称: ...
- select 1,select *,select count(),select sum(),select null
select 1 from dual where a >20 查询后返回1列,所返回的列值均为1,返回的行数等于满足条件的行数: 同理, select 常量 from - where -,查询后 ...
- select count(1) 、select count(*) 、select count(字段)的区别、及性能
select count(*) from teacher; //11 select count(1) from teacher; //11 select count(id) from teacher; ...
- mysql中对一个表的id建立了唯一索引,那么查询的select count(*) ,select count(1) ,select count(id),select count(列名)
1.mysql中对一个表的id建立了唯一索引,那么查询的select count(*) ,select count(1) ,select count(id),select count(列名) 的查询结 ...
最新文章
- Virtual Machine--Vmware(2)
- 通过MyBatis查找一张表的数据,某些字段的值为空
- .h file not found
- Centos下mysql数据库备份与恢复的方法
- getcwd函数_PHP getcwd()函数与示例
- LeetCode15:三数之和(转载思路)
- C#LeetCode刷题之#507-完美数(Perfect Number)
- spring-beans下的BeanUtils.copyProperties
- 80% 的学校还在给新生上 C 语言,它们 OUT 了吗?
- vb还是python强大-VB已死,Python当立 | 忆云竹
- 栈--进栈,出栈指针修改的顺序问题
- 机器学习第六回——无监督学习
- rabbitmq 一个生产者多个消费者_RabbitMQ入门学习系列(二),单生产者消费者
- Chrome安装Axure RP插件
- 柳州铁一中机器人_柳州铁路第一中学的办学成果
- Python网络爬取科目一题库(1685道)2021.1.3
- OpenCV鼠标修改图片透明度
- 记录,Cents 7 更改home和root的中文路径
- iPhone 手机存储空间没有了
- 【C语言】位段(详解)