From: Stephan Szabo <sszabo@megazone.bigpanda.com>
To: Phil Endecott <spam_from_postgresql_lists@chezphil.org>
Cc: pgsql-performance@postgresql.org
Subject: Re: Optimisation of INTERSECT expressions
Date: Tue, 23 Mar 2004 06:50:53 -0800 (PST)
Message-ID: <20040323063650.X85869@megazone.bigpanda.com> (raw)
In-Reply-To: <20040323141200.34D86D1EC02@svr1.postgresql.org>
References: <20040323141200.34D86D1EC02@svr1.postgresql.org>
On Tue, 23 Mar 2004, Phil Endecott wrote:
> Dear PostgresQL Experts,
>
> I am trying to get to the bottom of some efficiency problems and hope that
> you can help. The difficulty seems to be with INTERSECT expressions.
>
> I have a query of the form
> select A from T where C1 intersect select A from T where C2;
> It runs in about 100 ms.
>
> But it is equivalent to this query
> select A from T where C1 and C2;
> which runs in less than 10 ms.
>
> Looking at the output of "explain analyse" on the first query, it seems
> that PostgresQL always computes the two sub-expressions and then computes
> an explicit intersection on the results. I had hoped that it would notice
> that both subexpressions are scanning the same input table T and convert
> the expression to the second form.
>
> Is there a reason why it can't do this transformation?
Probably because noone's bothered to try to prove under what conditions
it's the same.
For example, given a non-unique A, the two queries can give different
answers (if say the same two A values match both C1 and C2 in different
rows how many output rows does each give? *), also given a non-stable A
(for example random) the two queries are not necessarily equivalent.
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-performance@postgresql.org
Cc: sszabo@megazone.bigpanda.com, spam_from_postgresql_lists@chezphil.org
Subject: Re: Optimisation of INTERSECT expressions
In-Reply-To: <20040323063650.X85869@megazone.bigpanda.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox