I have a query that looks something like
SELECT to_number FROM sent_texts
WHERE to_number NOT IN(SELECT mobile FROM action_6_members);
A WHERE is applied to the result set after the query is complete.
What would the effect be (improve/degrade) if the sub query contained
WHERE mobile = to_number
A HAVING is applied to the result set during the query.
What would the effect be (improve/degrade) if the sub query contained
HAVING mobile = to_number
What are the pros/cons of just using the original query?
Update
It seems my initial thoughts were wrong, thanks to Bill Karwin’s answer.
So Im going to update this with the explain of the original query.
This query is causing me my server to use 100% of the cpu.
Maybe someone can say why, and how to fix it?
id select_type table type possible_keys key key_len ref rows Extra 2 DEPENDENT SUBQUERY action_6_members index mobile 42 179218 Using where; Using index 1 PRIMARY sent_txts index to_number 123 256066 Using where; Using index
This is the explain based off the join(after some more optimization)
id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE sent_txts index to_number 78 256066 Using index 1 SIMPLE action_6_members index mobile 27 179218 Using where; Using index; Not exists
Just use the original query. MySQL optimizes this case all right, especially if
mobileis an indexed column. It runs the non-correlated subquery once, and comparesto_numberto the set ofmobilenumbers reasonably efficiently.I don’t know where you got the ideas about
WHEREconditions being applied after the query andHAVINGconditions being applied during the query. This is not accurate.Think of it this way:
WHEREconditions eliminate rows from the result set. This is done during the query.HAVINGconditions eliminate groups from the result set. This is also done during the query, but afterGROUP BYhas collected rows into groups.You should not use
HAVINGif you aren’t usingGROUP BY.