Re: Optimising Union Query. - Mailing list pgsql-general

From ptjm@interlog.com (Patrick TJ McPhee)
Subject Re: Optimising Union Query.
Date
Msg-id 3d014hF6ps78jU2@uni-berlin.de
Whole thread Raw
In response to Optimising Union Query.  (Rob Kirkbride <rob.kirkbride@thales-is.com>)
Responses Re: Optimising Union Query.  ("Jim C. Nasby" <decibel@decibel.org>)
List pgsql-general
In article <4268F322.1040106@thales-is.com>,
Rob Kirkbride <rob.kirkbride@thales-is.com> wrote:

% I've done a explain analyze and as I expected the database has to check
% every row in each of the three tables below but I'm wondering if I can

This is because you're returning a row for every row in the three
tables.

% select l.name,l.id from pa i,locations l where i.location=l.id union
% select l.name,l.id from andu i,locations l where i.location=l.id union
% select l.name,l.id from idu i,locations l where i.location=l.id;

You might get some improvement from

 select name,id from locations
  where id in (select distinct location from pa union
               select distinct location from andu union
               select distinct location from idu);

this query might be helped by an index on location in each of those
three tables, but probably not.
--

Patrick TJ McPhee
North York  Canada
ptjm@interlog.com

pgsql-general by date:

Previous
From: "Zlatko Matic"
Date:
Subject: Re: psqlodbc MSAccess and Postgresql
Next
From: David Roussel
Date:
Subject: Re: Table modifications with dependent views - best practices?