Right now, I'm debating whether or not to use COUNT(id)
or "count" columns. I heard that InnoDB COUNT
is very slow without a WHERE
clause because it needs to lock the table and do a full index scan. Is that the same behavior when using a WHERE
clause?
For example, if I have a table with 1 million records. Doing a COUNT
without a WHERE
clause will require looking up 1 million records using an index. Will the query become significantly faster if adding a WHERE
clause decreases the number of rows that match the criteria from 1 million to 500,000?
Consider the "Badges" page on SO, would adding a column in the badges
table called count
and incrementing it whenever a user earned that particular badge be faster than doing a SELECT COUNT(id) FROM user_badges WHERE user_id = 111
?
Using MyIASM is not an option because I need the features of InnoDB to maintain data integrity.