July 2010
Intermediate to advanced
840 pages
16h 33m
English
A UNION is often implemented by constructing the two result sets, then merge-sorting them together. The optimizer works only within a single SELECT statement or subquery. For example:
SELECT * FROM Personnel WHERE work = 'New York' UNION SELECT * FROM Personnel WHERE home = 'Chicago';
is the same as:
SELECT DISTINCT *
FROM Personnel
WHERE work = 'New York'
OR home = 'Chicago';The second will run faster.
Another trick is to use UNION ALL in place of UNION whenever duplicates are not a problem. The UNION ALL is implemented as an append operation, without the need for a sort to aid duplicate removal.
Read now
Unlock full access