SQL getting row count optimization [duplicate]
-
28-10-2019 - |
题
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?
解决方案
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).
其他提示
Query plan looks the same, but in testing, using * is faster (slightly).
Nulls are not taking into account when specifying a column name though.