The warning you received about performing subqueries for each row is true for interconnected subqueries.
SELECT COUNT(*) FROM Table1 a WHERE a.Table1id NOT IN ( SELECT b.Table1Id FROM Table2 b WHERE b.id_user = a.id_user );
Note that the subquery refers to the id_user column of the outer query. The id_user value for each row of Table1 may be different. Thus, the result of the subquery will probably be different, depending on the current row in the outer query. The RDBMS must execute the subquery many times, once for each row in the external query.
An example that you tested is an uncorrelated subquery . Most modern RDBMS optimizers worthy of their salt should be able to tell when the result of the subquery is independent of the values in each row of the external query. In this case, RDBMS launches the subquery at a time, caches its result and reuses it for the predicate in the external query.
PS: In SQL, IN() is called a "predicate", not an expression. A predicate is part of a language that evaluates to either true or false, but may not necessarily be executed independently as an operator. That is, you cannot just run this as an SQL query: "2 IN (1,2,3);" Although this is a valid predicate, it is not a valid statement.
Bill Karwin Apr 17 '09 at 17:35 2009-04-17 17:35
source share