From: David G Johnston <david.g.johnston@gmail.com>
To: pgsql-performance@postgresql.org
Subject: Re: Slow query with indexed ORDER BY and LIMIT when using OR'd conditions
Date: Mon, 21 Jul 2014 14:31:19 -0700 (PDT)
Message-ID: <1405978279538-5812285.post@n5.nabble.com> (raw)
In-Reply-To: <CACuOPqC9h6vK5rniezSOm2fu44PNp0NrB8kdtapT=xqNzQgidA@mail.gmail.com>
References: <CACuOPqC9h6vK5rniezSOm2fu44PNp0NrB8kdtapT=xqNzQgidA@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-performance>
johno wrote
> The question is... why is the query planner unable to make this
> optimization for the slow query? What am I missing?
Short answer - your first and last queries are not relationally equivalent
and the optimizer cannot change the behavior of the query which it is
optimizing. i.e. you did not make an optimization but rather choose to
reformulate the question so that it could be answered more easily while
still providing an acceptable answer.
The question main question is better phrased as:
Give me 100 updated at t(0) but only that are subsequent to a given ID. If
there are less than 100 such records give me enough additional rows having t
> t(0) so that the total number of rows returned is equal to 100.
Both queries give the same answer but only due to the final LIMIT 100. They
arrive there in different ways which necessitates generating different
plans. At a basic level it is unable to push down LIMIT into a WHERE clause
and it cannot add additional sub-queries that do not exist in the original
plan - which includes adding a UNION node.
David J.
--
View this message in context: http://postgresql.1045698.n5.nabble.com/Slow-query-with-indexed-ORDER-BY-and-LIMIT-when-using-OR-d-c...
Sent from the PostgreSQL - performance mailing list archive at Nabble.com.
--
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: david.g.johnston@gmail.com
Subject: Re: Slow query with indexed ORDER BY and LIMIT when using OR'd conditions
In-Reply-To: <1405978279538-5812285.post@n5.nabble.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