What is better in MYSQL count(*) or count(1)?(MYSQL count(*) 和 count(1) 哪个更好?)
问题描述
相关(SQL Server): Count(*) vs Count(1)
你能告诉我什么是更好的性能(MySQL)吗?计数(*)还是计数(1)?
Could you please tell me what is better in performance (MySQL)? Count(*) or count(1)?
推荐答案
这是 MySQL 的答案.
This is a MySQL answer.
它们的执行完全相同 - 除非您使用的是 MyISAM,否则存在 COUNT(*) 的特殊情况.无论如何,我总是使用 COUNT(*).
They perform exactly the same - unless you are using MyISAM, then a special case for COUNT(*) exists. I always use COUNT(*) anyway.
https://dev.mysql.com/doc/refman/5.6/en/aggregate-functions.html#function_count
对于 MyISAM 表,COUNT(*) 被优化为快速返回,如果SELECT 从一张表中检索,不检索其他列,并且没有 WHERE 子句.例如:
For
MyISAMtables,COUNT(*)is optimized to return very quickly if theSELECTretrieves from one table, no other columns are retrieved, and there is noWHEREclause. For example:
mysql> SELECT COUNT(*) FROM student;
此优化仅适用于MyISAM表,因为为此存储引擎存储了精确的行数并且可以非常快速地访问.COUNT(1) 只受相同如果第一列定义为NOT NULL,则优化.
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 as NOT NULL.
<小时>###编辑你们中的一些人可能错过了幽默的黑暗尝试.我更愿意将此作为一个非重复的问题,以便 MySQL 对 SQL Server 做一些不同的事情.所以我投票重新提出这个问题(答案显然是错误的).
上述MyISAM优化同样适用于
###EDIT Some of you may have missed the dark attempt at humour. I prefer to keep this as a non-duplicate question for any such day when MySQL will do something different to SQL Server. So I threw a vote to reopen the question (with a clearly wrong answer).
The above MyISAM optimization applies equally to
COUNT(*)
COUNT(1)
COUNT(pk-column)
COUNT(any-non-nullable-column)
所以真正的答案是它们总是相同.
So the real answer is that they are always the same.
这篇关于MYSQL count(*) 和 count(1) 哪个更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:MYSQL count(*) 和 count(1) 哪个更好?
基础教程推荐
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- oracle区分大小写的原因? 2021-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
- 在多列上分布任意行 2021-01-01
- 什么是 orradiag_<user>文件夹? 2022-01-01
