Re: Optimisation of INTERSECT expressions

From: "Phil Endecott" <spam_from_postgresql_lists(at)chezphil(dot)org>
To: pgsql-performance(at)postgresql(dot)org
Subject: Re: Optimisation of INTERSECT expressions
Date: 2004-03-23 16:21:39
Message-ID: 20040323162823.B4131D1EC40@svr1.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

I asked:
> select A from T where C1 intersect select A from T where C2;
> select A from T where C1 and C2;
> [why isn't the first optimised into the second?]

Stephan Szabo answered:
> Given a non-unique A, C1 as B>5, c2 as C>5 and the data:
> A | B | C
> 1 | 6 | 1
> 1 | 1 | 6
> The intersect gives 1 row, the and query gives 0 AFAICS.

Tom Lane answered:
> Another way that the queries are not equivalent is that INTERSECT is
> defined to remove duplicate output rows (much like DISTINCT) whereas
> the AND form of course won't do that.

Thanks! In my case the attribute A is unique - it is the primary key - and
I hadn't considered the more general case properly.

So I suppose I'll have to find a more sophisticated way to generate my
queries. Imagine a user interface for a search facility with various
buttons and text entry fields. At the moment, for each part of the search
that the user has enabled I create a string of SQL. I then compose them
into a single statement using INTERSECT. Each sub-query always returns the
same attribute, but to make things complicated they may come from different
tables. It now seems that I'll have to merge the queries more thoroughly.
Does anyone have any suggestions about how to do this? I'd like a nice
general technique that works for all possible subqueries, as my current
composition with INTERSECT does.

Any thoughts on my other question about empty intersections?

Thanks again for the feedback.

--Phil.

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Bruno Wolff III 2004-03-23 17:05:00 Re: Optimisation of INTERSECT expressions
Previous Message Josh Berkus 2004-03-23 16:15:39 Re: [PERFORM] Benchmarking postgres on Solaris/Linux