From: Tom Lane <tgl@sss.pgh.pa.us>
To: dmitry potapov <potapov.dmitry@gmail.com>
Cc: pgsql-performance@postgresql.org
Subject: Re: "WHERE 1 = 2 OR ..." makes planner choose a very inefficient plan
Date: Thu, 18 Apr 2013 10:46:57 -0400
Message-ID: <2374.1366296417@sss.pgh.pa.us> (raw)
In-Reply-To: <CAJPZb_vmf3dkCv0Rre+VOm6bkJc3hugjEE2h0bgY1WqduDLUbg@mail.gmail.com>
References: <CAJPZb_vmf3dkCv0Rre+VOm6bkJc3hugjEE2h0bgY1WqduDLUbg@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-performance>
dmitry potapov <potapov.dmitry@gmail.com> writes:
> I recently stumbled upon on what could be a planner bug or a corner case.
> If "<false condition> OR ..." is added to WHERE clause of SELECT query,
> then the planner chooses a very inefficient plan. Consider a query:
> SELECT count(k0.id)
> FROM k0
> WHERE 1 = 2
> OR k0.id IN (
> SELECT k1.k0_id
> FROM k1
> WHERE k1.k1k2_id IN (
> SELECT k2.k1k2_id
> FROM k2
> WHERE k2.t = 2
> AND (coalesce(k2.z, '')) LIKE '%12%'
> )
> );
Perhaps you should fix your application to not generate such incredibly
silly SQL. Figuring out that 1=2 is constant false and throwing it away
costs the server easily a thousand times as many instructions as it
would take for the client to not emit that in the first place.
The reason you don't get a nice semijoin plan when you do that is that
conversion of IN clauses to semijoins happens before
constant-subexpression simplification. So the planner hasn't yet
figured out that the OR is useless when it would need to know that to
produce a good plan. (And no, we can't just flip the order of those two
steps. Doing two rounds of const-simplification wouldn't be a good
answer either, because it would penalize well-written queries to benefit
badly-written ones.)
regards, tom lane
--
Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-performance
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: tgl@sss.pgh.pa.us, potapov.dmitry@gmail.com
Subject: Re: "WHERE 1 = 2 OR ..." makes planner choose a very inefficient plan
In-Reply-To: <2374.1366296417@sss.pgh.pa.us>
* 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