Pergunta

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?

Foi útil?

Solução

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

Outras dicas

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

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top