InnoDB handles SELECT COUNT(*) and SELECT COUNT(1) operations in the same way. There is no performance difference.
MySQL :: MySQL 8.0 Reference Manual :: 12.20.1 Aggregate Function Descriptions https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_count
-
Returns a count of the number of non-
NULL
values ofexpr
in the rows retrieved by aSELECT
statement. The result is aBIGINT
value.If there are no matching rows,
COUNT()
returns0
.COUNT(NULL)
returns 0.This function executes as a window function if
over_clause
is present.over_clause
is as described in Section 12.21.2, “Window Function Concepts and Syntax”.mysql> SELECT student.student_name,COUNT(*) FROM student,course WHERE student.student_id=course.student_id GROUP BY student_name;
COUNT(*)
is somewhat different in that it returns a count of the number of rows retrieved, whether or not they containNULL
values.For transactional storage engines such as
InnoDB
, storing an exact row count is problematic. Multiple transactions may be occurring at the same time, each of which may affect the count.InnoDB
does not keep an internal count of rows in a table because concurrent transactions might “see” different numbers of rows at the same time. Consequently,SELECT COUNT(*)
statements only count rows visible to the current transaction.As of MySQL 8.0.13,
SELECT COUNT(*) FROM
query performance fortbl_name
InnoDB
tables is optimized for single-threaded workloads if there are no extra clauses such asWHERE
orGROUP BY
.InnoDB
processesSELECT COUNT(*)
statements by traversing the smallest available secondary index unless an index or optimizer hint directs the optimizer to use a different index. If a secondary index is not present,InnoDB
processesSELECT COUNT(*)
statements by scanning the clustered index.Processing
SELECT COUNT(*)
statements takes some time if index records are not entirely in the buffer pool. For a faster count, create a counter table and let your application update it according to the inserts and deletes it does. However, this method may not scale well in situations where thousands of concurrent transactions are initiating updates to the same counter table. If an approximate row count is sufficient, useSHOW TABLE STATUS
.InnoDB
handlesSELECT COUNT(*)
andSELECT COUNT(1)
operations in the same way. There is no performance difference.For
MyISAM
tables,COUNT(*)
is optimized to return very quickly if theSELECT
retrieves from one table, no other columns are retrieved, and there is noWHERE
clause. For example:mysql> SELECT COUNT(*) FROM student;
This optimization only applies to
MyISAM
tables, because an exact row count is stored for this storage engine and can be accessed very quickly.COUNT(1)
is only subject to the same optimization if the first column is defined asNOT NULL
. -
COUNT(DISTINCT
expr
,[expr
...])Returns a count of the number of rows with different non-
NULL
expr
values.If there are no matching rows,
COUNT(DISTINCT)
returns0
.mysql> SELECT COUNT(DISTINCT results) FROM student;
In MySQL, you can obtain the number of distinct expression combinations that do not contain
NULL
by giving a list of expressions. In standard SQL, you would have to do a concatenation of all expressions insideCOUNT(DISTINCT ...)
.
MySQL :: MySQL 8.0 Reference Manual :: 3.3.4.8 Counting Rows https://dev.mysql.com/doc/refman/8.0/en/counting-rows.html
MySQL :: MySQL 8.0 Reference Manual :: 13.7.7.38 SHOW TABLE STATUS Statement https://dev.mysql.com/doc/refman/8.0/en/show-table-status.html
Rows
The number of rows. Some storage engines, such as MyISAM
, store the exact count. For other storage engines, such as InnoDB
, this value is an approximation, and may vary from the actual value by as much as 40% to 50%. In such cases, use SELECT COUNT(*)
to obtain an accurate count.
The Rows
value is NULL
for INFORMATION_SCHEMA
tables.
For InnoDB
tables, the row count is only a rough estimate used in SQL optimization. (This is also true if the InnoDB
table is partitioned.)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
2020-07-28 弹性公网IP
2020-07-28 Linux下nf_conntrack(最全面)_董明磊-CSDN博客_nf_conntrack https://blog.csdn.net/qq_35299863/article/details/79530732
2020-07-28 Docker 中的网络功能介绍 外部访问容器 容器互联 配置 DNS 配置镜像
2019-07-28 未授权访问漏洞总结
2018-07-28 wx.request的并发问题
2018-07-28 遍历修改 间接实现 引用赋值
2018-07-28 更多的使用自定义元素(CustomElement)。