Which is faster count (*) or count (table_field_name) in mysql?

Possible duplicate:
COUNT (*) vs COUNT (1) and COUNT (pk): which is better?

I want to get a counter from a select request.

Which is faster: count(*) or count(table_field_name) ?

I want to know which path is faster for performance.

+1
source share
2 answers

The difference Count(field) returns the number of NOT NULL values โ€‹โ€‹in the field, or COUNT(*) returns COUNT rows. COUNT(*) in MyIsam should be faster.
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_count

+5
source

at least on MyISAM tables, count(*) should be faster than count(fliedname) since it allows mysql to use the index (primary key more often) for counting. if the given field name is the primary key, it has no meaning.

with * , mysql wont will dump โ€œload entire row dataโ€ like others said - count(*) always the fastest or one of the fastest options, and count(fieldname) can be slower, depending on which field set.

EDIT :
the documentation says:

COUNT (*) is optimized for quick return [...]. This optimization applies only to MyISAM tables.

read the documentation for more information on this topic.

Important note: count(*) returns the total number of rows, and count(fieldname) returns the number of rows in which a non- NULL field is specified. this is logically consistent since with * mysql it cannot know what to leave NULL values. always think about it when doing count() , as this can have a bicomputer effect on the result.

0
source

All Articles