I have two tables users and posts with 500k records each.
I want to find users who had written between 100 and 200 posts.
My query is:
SELECT u.accountid, COUNT(*)
FROM users u
JOIN posts p
ON u.accountid = p.owneruserid
GROUP BY u.accountid
HAVING COUNT(*) BETWEEN 100 AND 200;
And I get answer in about a second.
I added indexes on accountid and owneruserid fields in tables users and posts respectively but the query didn't speed up. Why?