I currently have a summary table to keep track of my users’ post counts, and I run SELECTs on that table to sort them by counts, like WHERE count > 10, for example. Now I know having an index on columns used in WHERE clauses speeds things up, but since these fields will also be updated quite often, would indexing provide better or worse performance?
I currently have a summary table to keep track of my users’ post counts,
Share
If you have a query like
Then you cannot put an index on count, you need to put an index on the
group byfield instead.If you have a field named
countThen putting an index in this query may speed up the query, it may also make no difference at all:
Whether an index on
countwill speed up the query really depends on what percentage of the rows satisfy thewhereclause. If it’s more than 30%, MySQL (or any SQL for that matter) will refuse to use an index.It will just stubbornly insist on doing a full table scan. (i.e. read all rows)
This is because using an index requires reading 2 files (1 index file and then the real table file with the actual data).
If you select a large percentage of rows, reading the extra index file is not worth it and just reading all the rows in order will be faster.
If only a few rows pass the sets, using an index will speed up this query a lot
Know your data
Using
explain selectwill tell you what indexes MySQL has available and which one it picked and (kind of/sort of in a complicated kind of way) why.See: http://dev.mysql.com/doc/refman/5.0/en/explain.html