Re: Subselect query enhancement

From: Andrew Lazarus <andrew(at)pillette(dot)com>
To: pgsql-performance(at)postgresql(dot)org
Subject: Re: Subselect query enhancement
Date: 2007-02-01 22:37:35
Message-ID: 373981020.20070201143735@pillette.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance


>> How about this option:
>>
>> SELECT distinct ip_info.* FROM ip_info RIGHT JOIN network_events USING
>> (ip) RIGHT JOIN host_events USING (ip) WHERE
>> (network_events.name='blah' OR host_events.name = 'blah') AND
>> ip_info.ip IS NOT NULL;

MA> Nah, that seems to be much much worse. The other queries usually
MA> return in 1-2 minutes, this one has been running for 30 minutes and
MA> has still not returned

I find that an OR involving two different fields (in this case even
different tables) is faster when replaced by the equivalent UNION. In this
case---

SELECT distinct ip_info.* FROM ip_info RIGHT JOIN network_events USING
(ip) WHERE
network_events.name='blah' AND ip_info.ip IS NOT NULL
UNION
SELECT distinct ip_info.* FROM ip_info RIGHT JOIN host_events USING (ip) WHERE
host_events.name = 'blah' AND ip_info.ip IS NOT NULL;

Moreover, at least through 8.1, GROUP BY is faster than DISTINCT.

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Ben 2007-02-02 00:41:35 drive configuration for a new server
Previous Message Michael Artz 2007-02-01 19:30:42 Re: int4 vs varchar to store ip addr