Re: Slow query with backwards index scan

From: Jeremy Harris <jgh(at)wizmail(dot)org>
To: pgsql-performance(at)postgresql(dot)org
Subject: Re: Slow query with backwards index scan
Date: 2007-07-28 19:40:12
Message-ID: 46AB9B9C.8070009@wizmail.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
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

"ORDER BY and LIMIT can be attached to a subexpression if it is enclosed in parentheses"

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Hervé Piedvache 2007-07-28 19:56:58 Re: select on 1milion register = 6s
Previous Message Bruno Rodrigues Siqueira 2007-07-28 19:27:14 select on 1milion register = 6s