Re: improving performance of UNION and ORDER BY - Mailing list pgsql-general

From Jean-Luc Lachance
Subject Re: improving performance of UNION and ORDER BY
Date
Msg-id 3C7FF3B6.B27B00E0@nsd.ca
Whole thread Raw
In response to improving performance of UNION and ORDER BY  (Chris Gamache <cgg007@yahoo.com>)
Responses Re: improving performance of UNION and ORDER BY  (Chris Gamache <cgg007@yahoo.com>)
List 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?

pgsql-general by date:

Previous
From: "Cornelia Boenigk"
Date:
Subject: Qestion about CREATE FUNCTION
Next
From: Tom Lane
Date:
Subject: Re: Qestion about CREATE FUNCTION