Should I COUNT(*) or not?

前端 未结 14 1373
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-30 15:49

I know it\'s generally a bad idea to do queries like this:

SELECT * FROM `group_relations`

But when I just want the count, should I go for this

14条回答
  •  旧时难觅i
    2021-01-30 16:22

    COUNT(*) count all rows while COUNT(column_name) will count only rows without NULL values in the specified column.

    Important to note in MySQL:

    COUNT() is very fast on MyISAM tables for * or not-null columns, since the row count is cached. InnoDB has no row count caching, so there is no difference in performance for COUNT(*) or COUNT(column_name), regardless if the column can be null or not. You can read more on the differences on this post at the MySQL performance blog.

提交回复
热议问题