MYSQL计数(*)或计数(1)有什么好处?

时间:2023-02-05 20:08:39

Related (SQL Server): Count(*) vs Count(1)

相关(SQL Server):计数(*)与计数(1)

Could you please tell me what is better in performance (MySQL)? Count(*) or count(1)?

你能告诉我性能有哪些(MySQL)吗?算(*)还是算(1)?

1 个解决方案

#1


23  

This is a MySQL answer.

这是MySQL的答案。

They perform exactly the same - unless you are using MyISAM, then a special case for COUNT(*) exists. I always use COUNT(*) anyway.

它们执行完全相同 - 除非您使用MyISAM,否则存在COUNT(*)的特殊情况。无论如何我总是使用COUNT(*)。

http://dev.mysql.com/doc/refman/5.1/en/group-by-functions.html#function_count

http://dev.mysql.com/doc/refman/5.1/en/group-by-functions.html#function_count

COUNT(*) is optimized to return very quickly if the SELECT retrieves from one table, no other columns are retrieved, and there is no WHERE clause. For example:

如果SELECT从一个表检索,没有检索到其他列,并且没有WHERE子句,则COUNT(*)被优化为非常快速地返回。例如:

mysql> SELECT COUNT(*) FROM student;

This optimization applies only to MyISAM tables only, because an exact row count is stored for this storage engine and can be accessed very quickly. For transactional storage engines such as InnoDB, storing an exact row count is more problematic because multiple transactions may be occurring, each of which may affect the count.

此优化仅适用于MyISAM表,因为为此存储引擎存储了精确的行数,并且可以非常快速地访问。对于诸如InnoDB之类的事务存储引擎,存储精确行计数更成问题,因为可能正在发生多个事务,每个事务都可能影响计数。


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

有些人可能错过了幽默的黑暗尝试。我宁愿将此作为一个非重复的问题保留在任何这样的日子,当MySQL将做与SQL Server不同的事情。所以我投了一票来重新打开这个问题(答案明确错误)。

The above MyISAM optimization applies equally to

上述MyISAM优化同样适用于

COUNT(*)
COUNT(1)
COUNT(pk-column)
COUNT(any-non-nullable-column)

So the real answer is that they are always the same.

所以真正的答案是它们总是一样的。

#1


23  

This is a MySQL answer.

这是MySQL的答案。

They perform exactly the same - unless you are using MyISAM, then a special case for COUNT(*) exists. I always use COUNT(*) anyway.

它们执行完全相同 - 除非您使用MyISAM,否则存在COUNT(*)的特殊情况。无论如何我总是使用COUNT(*)。

http://dev.mysql.com/doc/refman/5.1/en/group-by-functions.html#function_count

http://dev.mysql.com/doc/refman/5.1/en/group-by-functions.html#function_count

COUNT(*) is optimized to return very quickly if the SELECT retrieves from one table, no other columns are retrieved, and there is no WHERE clause. For example:

如果SELECT从一个表检索,没有检索到其他列,并且没有WHERE子句,则COUNT(*)被优化为非常快速地返回。例如:

mysql> SELECT COUNT(*) FROM student;

This optimization applies only to MyISAM tables only, because an exact row count is stored for this storage engine and can be accessed very quickly. For transactional storage engines such as InnoDB, storing an exact row count is more problematic because multiple transactions may be occurring, each of which may affect the count.

此优化仅适用于MyISAM表,因为为此存储引擎存储了精确的行数,并且可以非常快速地访问。对于诸如InnoDB之类的事务存储引擎,存储精确行计数更成问题,因为可能正在发生多个事务,每个事务都可能影响计数。


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

有些人可能错过了幽默的黑暗尝试。我宁愿将此作为一个非重复的问题保留在任何这样的日子,当MySQL将做与SQL Server不同的事情。所以我投了一票来重新打开这个问题(答案明确错误)。

The above MyISAM optimization applies equally to

上述MyISAM优化同样适用于

COUNT(*)
COUNT(1)
COUNT(pk-column)
COUNT(any-non-nullable-column)

So the real answer is that they are always the same.

所以真正的答案是它们总是一样的。