Re: Optimising Union Query.

From: ptjm(at)interlog(dot)com (Patrick TJ McPhee)
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Optimising Union Query.
Date: 2005-04-23 22:39:14
Message-ID: 3d014hF6ps78jU2@uni-berlin.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

In article <4268F322(dot)1040106(at)thales-is(dot)com>,
Rob Kirkbride <rob(dot)kirkbride(at)thales-is(dot)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(at)interlog(dot)com

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tatsuo Ishii 2005-04-23 23:22:33 Re: Pgpool questions
Previous Message William Yu 2005-04-23 22:30:28 Re: UltraSPARC versus AMD