0

I'm wondering if there is a difference performance wise between using COUNT(*) and COUNT(date_created). I've read that the only semantic difference is that COUNT(*) also includes NULL values, however the date_created field is not nullable in this specific case.

I've looked at various posts and none really answer this question for MySQL. The best I've found is a comment by @tsilb on this thread.

Community
  • 1
  • 1
hafichuk
  • 10,351
  • 10
  • 38
  • 53

3 Answers3

2

Count(*) is faster, as it does not require that the field be inspected.

Count(column_name) requires a table scan unless that column is already indexed.

Count(*) can just look at the primary key index.

dodexahedron
  • 4,584
  • 1
  • 25
  • 37
2

If you have an index on date_created, there will be no difference. If it's an unindexed column, you'll end up doing a full table scan. When you have questions like this, you can find out a lot by using EXPLAIN SELECT COUNT(*) FROM my_table and comparing that to EXPLAIN SELECT COUNT(date_created) FROM my_table.

Sean McSomething
  • 6,376
  • 2
  • 23
  • 28
0

Try to use COUNT(*) because dbms figures out which index to use for best performance.

Mārtiņš Briedis
  • 17,396
  • 5
  • 54
  • 76