Tuesday, December 04, 2007

(ZT)Rewrite SQL subqueries as outer joins

Because SQL is a declarative language, you can write the same query in many forms, each getting the same result but with vastly different execution plans and performance.

In this example, we select all books that do not have any sales.  Note that this is a non-correlated sub-query, but it could be re-written in several ways.

select    book_key from    book where    book_key NOT IN (select book_key from sales);

There are serious problems with subqueries that may return NULL values. It is a good idea to discourage the use of the NOT IN clause (which invokes a sub-query) and to prefer NOT EXISTS (which invokes a correlated sub-query), since the query returns no rows if any rows returned by the sub-query contain null values.

select    book_key from    book where    NOT EXISTS (select book_key from sales);

Subqueries can often be re-written to use a standard outer join, resulting in faster performance.  As we may know, an outer join uses the plus sign (+) operator to tell the database to return all non-matching rows with NULL values.  Hence we combine the outer join with a NULL test in the WHERE clause to reproduce the result set without using a sub-query.

select    b.book_key from    book  b,    sales s where    b.book_key = s.book_key(+) and    s.book_key IS NULL;

This execution plan will also be faster by eliminating the sub-query.

0 Comments:

Post a Comment

<< Home