MySQL中count(*)、count(1)和count(col)的区别汇总

作者:jingjing.wang 时间:2024-01-23 07:13:43 

前言

count函数是用来统计表中或数组中记录的一个函数,count(*) 它返回检索行的数目, 不论其是否包含 NULL值。最近感觉大家都在讨论count的区别,那么我也写下吧:欢迎留言讨论,话不多说了,来一起看看详细的介绍吧。

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".

来源:https://yq.aliyun.com/articles/519068

标签:mysql,count,count(col)
0
投稿

猜你喜欢

  • python numpy 显示图像阵列的实例

    2022-09-23 01:24:13
  • IE7下 filter:Alpha(opacity=xx) 的小问题

    2008-12-02 16:24:00
  • python输入整条数据分割存入数组的方法

    2022-04-10 06:10:25
  • Go语言sort包函数使用示例

    2023-10-15 03:29:59
  • 简单的抓取淘宝图片的Python爬虫

    2022-01-19 14:42:31
  • Python实现带图形界面的炸金花游戏

    2021-06-21 15:41:56
  • 5分钟快速掌握JS中var、let和const的异同

    2024-05-09 15:05:49
  • php+highchats生成动态统计图

    2024-05-02 17:19:08
  • Python 实现使用空值进行赋值 None

    2021-04-12 07:01:09
  • ubuntu 16.04下mysql5.7.17开放远程3306端口

    2024-01-17 13:00:03
  • 如何解决“cint和clng的溢出出错”问题?

    2009-12-03 20:21:00
  • Python使用logging模块实现打印log到指定文件的方法

    2023-12-14 03:59:29
  • 实例详解Python装饰器与闭包

    2021-05-27 10:12:21
  • python图片灰度化处理的几种方法

    2023-03-05 01:14:57
  • 以SQLite和PySqlite为例来学习Python DB API

    2023-07-13 02:19:14
  • Python ZipFile模块详解

    2021-09-17 06:30:24
  • 在docker中安装mysql详解

    2024-01-26 09:57:55
  • React路由鉴权的实现方法

    2024-05-02 17:06:12
  • OpenCV 图像梯度的实现方法

    2023-07-14 08:25:43
  • mysql数据库下损坏数据的恢复操作其过程总结

    2009-02-13 13:36:00
  • asp之家 网络编程 m.aspxhome.com