Re: improving performance of UNION and ORDER BY

From: Jean-Luc Lachance <jllachan(at)nsd(dot)ca>
To: cgg007(at)yahoo(dot)com
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: improving performance of UNION and ORDER BY
Date: 2002-03-01 21:33:42
Message-ID: 3C7FF3B6.B27B00E0@nsd.ca
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Chris,

I believe you can cut down the processing time by including the order by
and limit in each select:

(SELECT ... ORDER BY 2 DESC, 4 LIMIT 20)
union
(SELECT ... ORDER BY 2 DESC, 4 LIMIT 20)
ORDER BY 2 DESC, 4 LIMIT 20

Give it a try and let me know

JLL

P.S.

Question: Why not have a single table with a field log_type in ( 'A',
'B') ???

Chris Gamache wrote:
>
> Three tables...
> [...]
>
> I imagine the combination of UNION and ORDER BY causes the problem, since
> Postgres has to locate all the rows that match the search criteria, merge them,
> order them, then return the top 20...
>
> Any suggestions? Did I forget to provide any data that would make things
> clearer?

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2002-03-01 21:41:32 Re: Qestion about CREATE FUNCTION
Previous Message Cornelia Boenigk 2002-03-01 21:10:50 Qestion about CREATE FUNCTION