COUNT SQL 函数的性能

在使用 COUNT 函数编写 SQL 语句时,我有两种选择.

I have two choices when writing an SQL statement with the COUNT function.

  1. SELECT COUNT(*) FROM
  2. SELECT COUNT(some_column_name) FROM

就性能而言,最好的 SQL 语句是什么?我可以通过使用选项 1 获得一些性能提升吗?

In terms of performance, what is the best SQL statement? Can I obtain some performance gain by using option 1?

推荐答案

性能不重要,因为它们做了 2 个不同的聚合

Performance should not matter because they do 2 different aggregates

  • COUNT(*) 是所有行,包括 NULLs
  • COUNT(some_column_name),排除some_column_name"中的NULL
  • COUNT(*) is all rows, including NULLs
  • COUNT(some_column_name), excludes NULL in "some_column_name"

有关更多信息,请参阅Count(*) vs Count(1)"问题

See the "Count(*) vs Count(1)" question for more

相关文章