I have something like this:
SELECT FROM table
WHERE field NOT IN (
SELECT FROM other_table ... //SECOND QUERY
)
Problem: I don't want SECOND QUERY be executed for each row of table. Can I save QUERY 2 result to some variable?
You can check if the query will run for every row by running EXPLAIN and looking if it its a DEPENDENT SUBQUERY or SUBQUERY. Dependent will run for each row.
Based on that you want to 'save it to a variable' I'm guessing it will not be dependent.
If you want to save a resultset to a 'variable' you need to use a temp table. In this case it will not be necessary.
A LEFT NULL JOIN may be a lot faster for you.
SELECT *
FROM table AS t
LEFT JOIN other_table AS ot ON ot.field = t.field
WHERE ot.field IS NULL
other_tableandtablehave anything in common? Maybe usingWHERE NOT EXISTScould be better? Or aJOIN?