Skip site navigation (1) Skip section navigation (2)

Re: Query plan for "heavy" SELECT with "lite" sub-SELECTs

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Arjen van der Meijden <acmmailing(at)tweakers(dot)net>
Cc: Dave Dutcher <dave(at)tridecap(dot)com>, nikolay(at)samokhvalov(dot)com, pgsql-performance(at)postgresql(dot)org
Subject: Re: Query plan for "heavy" SELECT with "lite" sub-SELECTs
Date: 2006-11-03 14:50:53
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-performance
Arjen van der Meijden <acmmailing(at)tweakers(dot)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
> )

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

In response to

pgsql-performance by date

Next:From: Tom LaneDate: 2006-11-03 15:25:47
Subject: Re: Context switch storm
Previous:From: Richard HuxtonDate: 2006-11-03 14:38:25
Subject: Re: Context switch storm

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group