whats faster, count(*) or count(table_field_name) in mysql? [duplicate]
Possible Duplicate:
COUNT(*) vs. COUNT(1) vs. COUNT(pk): which is better?
I want to retrieve the count from a select query.
What is faster: count(*)
or count(table_field_name)
?
I want to know which 开发者_开发知识库way is faster for performance.
The difference is Count(field)
returns count of NOT NULL values in the field, whether COUNT(*)
returns COUNT of rows.
COUNT(*)
in MyIsam should be faster.
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_count
at least on MyISAM tables count(*)
should be faster than count(fliedname)
as it allows mysql to use an index (the primary key most times) to do the counting. if the given fieldname is the primary key, it wont make any difference.
using *
, mysql wont be so dump to "load the data of the entire row" as others said - count(*)
is always the fastest or one of the fastest options while count(fieldname)
could be slower, depending on what field is given.
EDIT:
the documantation says:
COUNT(*) is optimized to return very quickly [...]. This optimization applies only to MyISAM tables only
read on the documentation for more information about this topic.
Important note: count(*)
returns to total count of rows while count(fieldname)
returns the count of rows there that given field isn't NULL
. this is logically consistent as with *
mysql can't know wich NULL
-values to leave out. always think of this when doing count()
as it may have a bic impact on the result.
精彩评论