Materialization
of a subquery can also occur when the subquery is nonflattenable and there
is an equijoin between the subquery and another FROM table in the query.
For
example:
SELECT i, a FROM t1,
(SELECT DISTINCT a FROM T2) x1
WHERE t1.i = x1.a AND t1.i in (1, 3, 5, 7)
In this example, the
subquery x1 is noncorrelated because it does not reference any of the columns
from the outer query. The subquery is nonflattenable because of the DISTINCT
keyword.
Derby does not
flatten DISTINCT subqueries. This subquery is eligible for materialization.
Since there is an equijoin predicate between the subquery x1 and the table
t1 (namely, t1.i = x1.a), the
Derby optimizer
will consider performing a hash join between t1 and x1 (with x1 as the inner
operand). If that approach yields the best cost,
Derby materializes
the subquery x1 to perform the hash join. The subquery is evaluated only a
single time and the results are stored in an in-memory hash table.
Derby then
executes the join using the in-memory result set for x1.