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


猜你喜欢
- 原来的题目设想为界面视觉效果的统一性,但是“统一”这个词似乎有点敏感,怕触动萌点无数,而我也无意去设定一个什么什么的统一性来侃侃而谈,极为专
- 1、python 操作xml的方式介绍查看全部包含“三种⽅法:⼀是xml.dom. * 模块,它是W3CDOMAPI的实
- Oracle是世界上用得最多的数据库之一,活动服务器网页(ASP)是一种被广泛用于创建 * 页的功能强大的服务器端脚本语言。许多ASP开发人
- 我们先来看一下效果(简单的写了一个):原理:将post请求的代码数据写入了服务器的一个文件,然后用服务器的python编译器执行返回结果实现
- #coding:utf8import reimport urllibdef getHTML(url):
- 1、启动SQL Server Management Studio,以Windows身份验证方式登录。2、在对象资源管理器窗口中,右键单击服务
- 前言左思右想没有头绪时,刚好看到一篇介绍Pygame制作飞机大战的文章。文章写的不错,文中代码拿来就能跑。有了!要不直接把飞机大战改成接兔子
- EXISTS该函数返回集合中第一个元素的索引,如果集合为空,返回NULLNULLNULLCollection.EXISTS(index)CO
- 本章为大家介绍的模块,在python2的时候,并不受宠,主要的问题是存在安全漏洞,发现问题就要及时解决,因此在现在3版本中,已经得到了妥善的
- 之前mysql用着好着,可是今天在启动mysql后输入密码出现了闪退,在任务管理器中发现mysql服务没有启动,当手动启动时提示拒绝访问。在
- 什么是yaml一种标记语言。yaml 是专门用来写配置文件的语言,非常简洁和强大更直观,更方便,有点类似于json格式yaml文件格式:te
- 本文详细描述使用Django 的ORM框架操作PostgreSQL数据库删除不生效问题的定位过程及解决方案,并总结使用ORM框架操作数据库不
- 在Windows系统下,如何在cmd中输入命令来运行.py文件呢?一. 设置环境变量想要在cmd中运行python文件,必须要设置pytho
- 在上一篇文章中,简单介绍了下闭包(closure)和原型链,现在继续来研究闭包的内部机制。对了,所有的东西都参考自这篇文章:Javascri
- 随着搜索引擎大兴, 排列在前的网站引入大量流量. 无论是搜索页面的广告还是查出来的结果, 与搜索者的目标匹配度都比较高 (如果搜索引擎足够智
- 废话少说,直接上代码:<?php/** * Note:for octet-stream upload * 这个是流式上传PHP文件 *
- PM2实现Nodejs项目自动部署首先简单说下思路:本地git仓库与远程仓库关联(github、码云等平台),然后pm2按照指定配置登录服务
- 1、copy.copy()函数可用于复制列表或字典等可变值,复制后的列表和原列表是两个独立的列表。import copyorigin = [
- 1.用python调用python脚本#!/usr/local/bin/python3.7import timeimport os coun
- python cron定时任务触发接口自动化巡检定时任务触发方式有几种类型,日常的工作中,研发同学运用比较多的就是cron方式查了一下APS