From: Tom Lane <tgl@sss.pgh.pa.us>
To: Arjen van der Meijden <acmmailing@tweakers.net>
Cc: Dave Dutcher <dave@tridecap.com>
Cc: nikolay@samokhvalov.com
Cc: pgsql-performance@postgresql.org
Subject: Re: Query plan for "heavy" SELECT with "lite" sub-SELECTs
Date: Fri, 03 Nov 2006 09:50:53 -0500
Message-ID: <1994.1162565453@sss.pgh.pa.us> (raw)
In-Reply-To: <454B0E3C.4060600@tweakers.net>
References: <e431ff4c0611020307x1fca3a14w16f60dbd80d0e856@mail.gmail.com>
<012a01c6fe8a$bee330e0$8300a8c0@tridecap.com>
<20061102191555.GL25444@alvh.no-ip.org>
<454B0E3C.4060600@tweakers.net>
Arjen van der Meijden <acmmailing@tweakers.net> writes:
> ... Rewriting it to something like this made the last iteration about as
> fast as the first:
> SELECT docid, (SELECT work to be done for each document)
> FROM documents
> WHERE docid IN (SELECT docid FROM documents
> ORDER BY docid
> LIMIT 1000
> OFFSET ?
> )
The reason for this, of course, is that the LIMIT/OFFSET filter is the
last step in a query plan --- it comes *after* computation of the SELECT
output list. (So does ORDER BY, if an explicit sort step is needed.)
So if you have an expensive-to-compute output list, a trick like Arjen's
will help. I don't think you can use an "IN" though, at least not if
you want to preserve the sort ordering in the final result.
regards, tom lane
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, acmmailing@tweakers.net, dave@tridecap.com, nikolay@samokhvalov.com
Subject: Re: Query plan for "heavy" SELECT with "lite" sub-SELECTs
In-Reply-To: <1994.1162565453@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