Sunday, November 25, 2007

Don't use Union unless you know what you're doing

I got bitten by a little insect I didn't know about, the other day. I'd written a family of views to simplify something horribly complex. Except in the penultimate view I had used UNION instead of UNION ALL.

I *HAD* too read the postgresql documentation on SELECT which clearly states that:
The result of UNION does not contain any duplicate rows unless the ALL option is specified. ALL prevents elimination of duplicates. (Therefore, UNION ALL is usually significantly quicker than UNION; use ALL when you can.)
.

But clearly I hadn't internalized it and continued preferring UNION because it was one word instead of two :-).

The bug report set me straight though. Now I will *definitely* remember the difference between UNION and UNION ALL (in that UNION folds duplicate rows into just one).

No comments: