千家信息网

MySQL中count(*)、count(1)、count(col)三者的区别是什么

发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,MySQL中count(*)、count(1)、count(col)三者的区别是什么?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。1
千家信息网最后更新 2026年11月01日MySQL中count(*)、count(1)、count(col)三者的区别是什么

MySQL中count(*)、count(1)、count(col)三者的区别是什么?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

1、表结构:

dba_jingjing@3306>[rds_test]>CREATE TABLE `test_count` ( -> `c1` varchar(10) DEFAULT NULL, -> `c2` varchar(10) DEFAULT NULL, -> KEY `idx_c1` (`c1`) -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8;Query OK, 0 rows affected (0.11 sec)

2、插入测试数据:

dba_jingjing@3306>[rds_test]>insert into test_count values(1,10);Query OK, 1 row affected (0.03 sec)dba_jingjing@3306>[rds_test]>insert into test_count values(abc,null);ERROR 1054 (42S22): Unknown column 'abc' in 'field list'dba_jingjing@3306>[rds_test]>insert into test_count values('abc',null);Query OK, 1 row affected (0.04 sec)dba_jingjing@3306>[rds_test]>insert into test_count values(null,null);Query OK, 1 row affected (0.04 sec)dba_jingjing@3306>[rds_test]>insert into test_count values('368rhf8fj',null);Query OK, 1 row affected (0.03 sec)dba_jingjing@3306>[rds_test]>select * from test_count;+-----------+------+| c1  | c2 |+-----------+------+| 1   | 10 || abc  | NULL || NULL  | NULL || 368rhf8fj | NULL |+-----------+------+4 rows in set (0.00 sec)

测试:

dba_jingjing@3306>[rds_test]>select count(*) from test_count;+----------+| count(*) |+----------+|  4 |+----------+1 row in set (0.00 sec)   EXPLAIN: {  "query_block": {   "select_id": 1,   "message": "Select tables optimized away"  1 row in set, 1 warning (0.00 sec)
dba_jingjing@3306>[rds_test]>select count(1) from test_count;+----------+| count(1) |+----------+|  4 |+----------+1 row in set (0.00 sec)   EXPLAIN: {  "query_block": {   "select_id": 1,   "message": "Select tables optimized away"  1 row in set, 1 warning (0.00 sec)
dba_jingjing@3306>[rds_test]>select count(c1) from test_count;+-----------+| count(c1) |+-----------+|   3 |+-----------+1 row in set (0.00 sec)   "table": {    "table_name": "test1",    "access_type": "index",    "key": "idx_c1",    "used_key_parts": [     "c1"    ],    "key_length": "33",

那么这里面的"key_length": "33",为什么是33呢,什么是二级索引?见下节

count(*) 和count(1) 是没有区别的,而count(col) 是有区别的

执行计划有特点:可以看出它没有查询索引和表,有时候会出现select tables optimized away 不会查表,速度会很快

Extra有时候会显示"Select tables optimized away",意思是没有更好的可优化的了。

官方解释For explains on simple count queries (i.e. explain select count(*) from people) the extra
section will read "Select tables optimized away."
This is due to the fact that MySQL can read the result directly from the table internals and therefore does not need to perform the select.

---MySQL对于"Select tables optimized away"的含义, 不是"没有更好的可优化的了", 官方解释中关键的地方在于:
MySQL can read the result directly

所以,合理的解释是:

1 数据已经在内存中可以直接读取;

2 数据可以被认为是一个经计算后的结果,如函数或表达式的值;

3 一旦查询的结果被优化器"预判"可以不经执行就可以得到结果,所以才有"not need to perform the select".

关于MySQL中count(*)、count(1)、count(col)三者的区别是什么问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注行业资讯频道了解更多相关知识。

数据 结果 问题 解释 官方 更多 有时候 索引 帮助 查询 测试 解答 易行 简单易行 内存 内容 函数 含义 地方 小伙 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 美国互联网科技巨头监管 科技类互联网 互联网科技股票 致骋互联网科技 奇酷互联网科技公司 奇酷互联网络科技公司 互联网前沿科技 海南科技产业互联网平台 青岛中康互联网医疗科技有限公司 互联网科技博主是什么意思 苏州铭阳互联网科技有限公司 搜狐科技互联网 浙南科技城互联网大厦建设工程 恒生互联网科技业指数调整周期 中科明德北京互联网科技 伍佰万互联网科技怎么赚钱 互联网科技数字货币 互联网科技包装股份有限公司上市 广州梦谷互联网科技有限公司领导 广州三三互联网科技有限公司 互联网科技工作委员会毛军 高科技与互联网 浩特互联网科技 互联网科技ppt模板 火传媒上海互联网科技 中国科技互联网公司收入 智腾互联网科技有限公司 山东子淼互联网科技有限公司 中国互联网有什么科技 互联网科技企业商标注意事项
0