Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id D4F789FB291 for ; Thu, 1 Feb 2007 18:14:57 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-new, port 10024) with ESMTP id 72713-10 for ; Thu, 1 Feb 2007 18:14:46 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from pillette.com (adsl-67-119-5-202.dsl.snfc21.pacbell.net [67.119.5.202]) by postgresql.org (Postfix) with ESMTP id 9743F9FB28B for ; Thu, 1 Feb 2007 18:14:45 -0400 (AST) Received: from dhcp202.pillette.com (dhcp202.pillette.com [192.168.1.202]) by pillette.com (8.11.6/8.11.6) with ESMTP id l11MEgk10296 for ; Thu, 1 Feb 2007 14:14:42 -0800 Date: Thu, 1 Feb 2007 14:37:35 -0800 From: Andrew Lazarus X-Mailer: The Bat! (v3.95.6) UNREG / CD5BF9353B3B7091 Organization: Pillette Investment Management X-Priority: 3 (Normal) Message-ID: <373981020.20070201143735@pillette.com> To: pgsql-performance@postgresql.org Subject: Re: Subselect query enhancement In-Reply-To: References: <45C22353.4020902@blackducksoftware.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.009 tagged_above=0 required=5 tests=AWL, BAYES_50, FORGED_RCVD_HELO, UPPERCASE_25_50 X-Spam-Level: X-Archive-Number: 200702/11 X-Sequence-Number: 23065 >> 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.