I’m a MySQL query noobie so I’m sure this is a question with an obvious answer.
But, I was looking at these two queries. Will they return different result sets? I understand that the sorting process would commence differently, but I believe they will return the same results with the first query being slightly more efficient?
Query 1: HAVING, then AND
SELECT user_id
FROM forum_posts
GROUP BY user_id
HAVING COUNT(id) >= 100
AND user_id NOT IN (SELECT user_id FROM banned_users)
Query 2: WHERE, then HAVING
SELECT user_id
FROM forum_posts
WHERE user_id NOT IN(SELECT user_id FROM banned_users)
GROUP BY user_id
HAVING COUNT(id) >= 100
Actually the first query will be less efficient (
HAVINGapplied afterWHERE).UPDATE
Some pseudo code to illustrate how your queries are executed ([very] simplified version).
First query:
1.
SELECT user_id FROM forum_posts2.
SELECT user_id FROM banned_user3. Group, count, etc.
4. Exclude records from the first result set if they are presented in the second
Second query
1.
SELECT user_id FROM forum_posts2.
SELECT user_id FROM banned_user3. Exclude records from the first result set if they are presented in the second
4. Group, count, etc.
The order of steps 1,2 is not important, mysql can choose whatever it thinks is better. The important difference is in steps 3,4. Having is applied after
GROUP BY. Grouping is usually more expensive than joining (excluding records can be considering as join operation in this case), so the fewer records it has to group, the better performance.