Thread: UNION or OR / INTERSECT or AND ?

UNION or OR / INTERSECT or AND ?

From
"David W"
Date:
Hello,

do i have to prefered to use UNION and INTERSECT or is it better with
OR and AND ?

Query example :

1. OR

SELECT a.designation, m.label FROM article a JOIN manufacturer m ON
m.id = a.manufacturer_id
WHERE a.designation || m.label ILIKE '%elec%'
OR a.designation || m.label ILIKE '%compa%'
OR a.designation || m.label ILIKE '%xs%'

2. UNION

SELECT a.designation, m.label FROM article a JOIN manufacturer m ON
m.id = a.manufacturer_id WHERE a.designation || m.label ILIKE '%elec%'
UNION
SELECT a.designation, m.label FROM article a JOIN manufacturer m ON
m.id = a.manufacturer_id WHERE a.designation || m.label ILIKE '%compa%'
UNION
SELECT a.designation, m.label FROM article a JOIN manufacturer m ON
m.id = a.manufacturer_id WHERE a.designation || m.label ILIKE '%xs%'

My actual database, is too small to see a real difference. That's why i
prefered to ask this newsgroup.

Thanks for your support,
David "Willou".
http://www.willou.net.


Re: UNION or OR / INTERSECT or AND ?

From
Michael Glaesemann
Date:
On Feb 7, 2006, at 23:04 , David W wrote:

> do i have to prefered to use UNION and INTERSECT or is it better with
> OR and AND ?

In general, EXPLAIN ANALYZE is your friend. I suspect using OR will
be better (and that's only a suspicion), but by using EXPLAIN ANALYZE
you can see exactly how the server is planning the queries based on
your actual situation (as long as your statistics are accurate. See
ANALYZE as well).

[EXPLAIN](http://www.postgresql.org/docs/current/interactive/sql-
explain.html)
[ANALYZE](http://www.postgresql.org/docs/current/interactive/sql-
analyze.html)

Once you've got some good EXPLAIN ANALYZE output, people on the
performance list may be able to help out if you still have questions.

http://archives.postgresql.org/pgsql-performance/

Michael Glaesemann
grzm myrealbox com