1

Possible Duplicate:
In SQL, what's the difference between count(column) and count()?
Count() vs Count(1)

I have big tables which keep long texts for example email content or news. And my question is there any difference for performance for counting table rows :

SELECT COUNT(*) FROM table_name

SELECT COUNT(t.id) FROM table_name as t

Which one is better? or the latter one will be optimized by query optimizer? Is there any documentation regarding this?

Community
  • 1
  • 1
Jama A.
  • 15,680
  • 10
  • 55
  • 88

2 Answers2

2

They are different.

COUNT(*) will retrieve all values (even the NULL values count). COUNT(t.id) doesn't count the NULL values.


In terms of performance, they are the same (the query optimizer is smart).

aF.
  • 64,980
  • 43
  • 135
  • 198
  • 1
    In terms of performance they are only categorically the same for non nullable columns. If the column is not nullable then the QO can choose a narrower index that does not even contain the specified column or (for MySQL - can't remember which storage engine) use the cached value that does not require a table access at all. – Martin Smith Jan 13 '12 at 15:24
1

Query plan looks the same, but in testing, using * is faster (slightly).

Nulls are not taking into account when specifying a column name though.

UnhandledExcepSean
  • 12,504
  • 2
  • 35
  • 51