COUNT() function returns a number of rows returned by a query. In
a popular opinion COUNT(*)
should not be used on
InnoDB tables, but this is only half true.
If a query performs filtering on any column, there is no relevant
difference in how COUNT(*)
will be executed
regardless of the storage engine. In any such case MySQL has to
look for matching rows and then count them.
In the following queries COUNT(*) can be used without any negative impact on performance:
SELECT COUNT(*) FROM mytable WHERE id = 12345
SELECT COUNT(*) FROM mytable WHERE is_enabled =
1
SELECT COUNT(*) FROM mytable WHERE username LIKE 'a%' AND
is_enabled = 1
The real difference is when no filter is specified in WHERE clause, i.e. when query counts all rows in a table. MyISAM maintains cached row count for each table, so it can always return the value …
[Read more]