Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id C0BA19FB335 for ; Fri, 3 Nov 2006 10:51:03 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 68217-10 for ; Fri, 3 Nov 2006 10:50:56 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id 38F3B9FB321 for ; Fri, 3 Nov 2006 10:50:56 -0400 (AST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.6/8.13.6) with ESMTP id kA3Eor1x001995; Fri, 3 Nov 2006 09:50:53 -0500 (EST) To: Arjen van der Meijden cc: Dave Dutcher , nikolay@samokhvalov.com, pgsql-performance@postgresql.org Subject: Re: Query plan for "heavy" SELECT with "lite" sub-SELECTs In-reply-to: <454B0E3C.4060600@tweakers.net> References: <012a01c6fe8a$bee330e0$8300a8c0@tridecap.com> <20061102191555.GL25444@alvh.no-ip.org> <454B0E3C.4060600@tweakers.net> Comments: In-reply-to Arjen van der Meijden message dated "Fri, 03 Nov 2006 10:39:08 +0100" Date: Fri, 03 Nov 2006 09:50:53 -0500 Message-ID: <1994.1162565453@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200611/49 X-Sequence-Number: 21819 Arjen van der Meijden 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