pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Craig James <craig_james@emolecules.com>
To: Tilmann Singer <tils-pgsql@tils.net>
Cc: andrew@pillette.com
Cc: pgsql-performance@postgresql.org
Subject: Re: Slow query with backwards index scan
Date: Sat, 28 Jul 2007 13:06:02 -0700
Message-ID: <46ABA1AA.7020701@emolecules.com> (raw)
In-Reply-To: <20070728192713.GH19392@tils.net>
References: <200707281903.l6SJ3qs07818@pillette.com>
	<20070728192713.GH19392@tils.net>

Tilmann Singer wrote:
> * andrew@pillette.com <andrew@pillette.com> [20070728 21:05]:
>> Let's try putting the sort/limit in each piece of the UNION to speed them up separately.
>>
>> SELECT * FROM (
>>  (SELECT * FROM large_table lt
>>  WHERE lt.user_id = 12345
>>  ORDER BY created_at DESC LIMIT 10) AS q1
>>  UNION
>>  (SELECT * FROM large_table lt
>>  WHERE user_id IN (SELECT contact_id FROM relationships WHERE user_id=12345)
>>  ORDER BY created_at DESC LIMIT 10) AS q2
>> ORDER BY created_at DESC LIMIT 10;
> 
> It's not possible to use ORDER BY or LIMIT within unioned queries.
> 
> http://www.postgresql.org/docs/8.2/static/sql-select.html#SQL-UNION

If I'm reading this documentation correctly, it *is* possible, as long as they're inside of a sub-select, as in this case.

Craig



view thread (12+ messages)  latest in thread

Message-ID: <46ABA1AA.7020701@emolecules.com>
Permalink:  ../46ABA1AA.7020701@emolecules.com/
Also on:    postgresql.org/message-id/46ABA1AA.7020701@emolecules.com

 · 

reply

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: craig_james@emolecules.com, tils-pgsql@tils.net, andrew@pillette.com
  Subject: Re: Slow query with backwards index scan
  In-Reply-To: <46ABA1AA.7020701@emolecules.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