Possible Duplicate:
COUNT() vs. COUNT(1) vs. COUNT(pk): which is better?
count() and count(column_name), what's the diff?
count(*) vs count(column-name) – which is more correct?
The benefit of using count(*) in a select statement is that I can use it with any table and that makes automating scripts easier:
count_sql = 'select count(*) ' + getRestOfSQL('tablename');
But, is it less efficient than using count(specific_field)?
For InnoDB
If
specific_fieldis not nullable, they are equivalent and have the same performance.If
specific_fieldis nullable, they don’t do the same thing.COUNT(specific_field)counts the rows which have a not null value ofspecific_field. This requires looking at the value ofspecific_fieldfor each row.COUNT(*)simply counts the number of rows and in this case can be faster as it does not require examining the value ofspecific_field.For MyISAM
There is a special optimization for the following so that it does not even need to fetch all rows: