Re: Optimize the query, help me please.

From: Achilleas Mantzios <achill(at)matrix(dot)gatewaynet(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Optimize the query, help me please.
Date: 2011-11-23 11:17:20
Message-ID: 201111231317.21168.achill@matrix.gatewaynet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Στις Wednesday 23 November 2011 12:56:23 ο/η Капралов Александр έγραψε:
> Query is:
> SELECT * FROM a UNION SELECT * FROM b ORDER BY time DESC LIMIT 100
>
> how can i get only last 100 row from a and b and then do union. Explain of
> select said that all recond selected from a and b.
>

In order to get 100 last rows from a and 100 last rows from b do:
(SELECT * FROM a ORDER BY time LIMIT 100) UNION ALL (SELECT * FROM b ORDER BY time LIMIT 100);

omitting the "ALL" modifier, you may end up with less rows, since UNION normally returns only
distinct rows.

> thanks.
>

--
Achilleas Mantzios

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Raghavendra 2011-11-23 14:05:59 In which catalog postgres Instance Crash time recorded ?
Previous Message Bèrto ëd Sèra 2011-11-23 11:08:37 Re: Optimize the query, help me please.