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

Re: Slow query with backwards index scan

From: Craig James <craig_james(at)emolecules(dot)com>
To: Tilmann Singer <tils-pgsql(at)tils(dot)net>
Cc: andrew(at)pillette(dot)com, pgsql-performance(at)postgresql(dot)org
Subject: Re: Slow query with backwards index scan
Date: 2007-07-28 20:06:02
Message-ID: 46ABA1AA.7020701@emolecules.com (view raw or flat)
Thread:
Lists: pgsql-performance
Tilmann Singer wrote:
> * andrew(at)pillette(dot)com <andrew(at)pillette(dot)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

In response to

Responses

pgsql-performance by date

Next:From: Bruno Rodrigues SiqueiraDate: 2007-07-28 20:12:10
Subject: RES: select on 1milion register = 6s
Previous:From: Craig JamesDate: 2007-07-28 19:59:26
Subject: Re: select on 1milion register = 6s

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