I have a poorly performing query:
SELECT user_id, count(item_id) as count
FROM table items
WHERE category = 'magazine'
AND created_at > 1384754400
GROUP BY user_id
ORDER BY count(item_id) desc
LIMIT 100
Whats the optimal indexing strategy in order to optimize this query?
Table Details
500million records with the following structure / cardinalities:
PRIMARY KEY (item_id)
- cardinality: 500 Muser_id
- cardinality: ~ 25 Mcategory
- cardinality: ~ 2.5 Mcreated_at
- cardinality: ~ 150 M
Indexing:
- I have individual indexes on each the user_id, category and created_at fields
I also have the following covering indexes:
- (
category
,user_id
) - this is the one the query optimizer defaults to when running explain - (
category
,created_at
) - (
category
,created_at
,user_id
) - this is one I attempted to create in order to optimize this query, however, it doesn't seem to be working very well.