Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id AD7FE9FA2B2 for ; Thu, 1 Feb 2007 12:42:08 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-new, port 10024) with ESMTP id 63546-02 for ; Thu, 1 Feb 2007 12:42:06 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.4 Received: from ug-out-1314.google.com (ug-out-1314.google.com [66.249.92.168]) by postgresql.org (Postfix) with ESMTP id 9D0B99FA18E for ; Thu, 1 Feb 2007 12:42:05 -0400 (AST) Received: by ug-out-1314.google.com with SMTP id k3so457899ugf for ; Thu, 01 Feb 2007 08:42:04 -0800 (PST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:to:subject:mime-version:content-type:content-transfer-encoding:content-disposition; b=jKPoSuP9BJzEtGOv+kDdeIZ3yqNU1fGb9U0sH2MSvjHu/fsnbtC5n3Z37SgoGlag3Z725T6yGDxBG4wMcEEfewq12Cd/vSxTQh4B5qNZeeUh7S2kbiRmHu5jI4OgXEAXpnCfuQ9XMzJNtSwoQB3Xzn8MrstmfJRLNZIT4FLHRw4= Received: by 10.82.179.9 with SMTP id b9mr777635buf.1170348124029; Thu, 01 Feb 2007 08:42:04 -0800 (PST) Received: by 10.82.190.6 with HTTP; Thu, 1 Feb 2007 08:42:03 -0800 (PST) Message-ID: Date: Thu, 1 Feb 2007 11:42:03 -0500 From: "Michael Artz" To: pgsql-performance@postgresql.org Subject: Subselect query enhancement MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200702/2 X-Sequence-Number: 23056 I'm needing help determining the best all-around query for the following situation. I have primary table that holds ip information and two other tables that hold event data for the specific IP in with a one-to-many mapping between them, ie: CREATE TABLE ip_info ( ip IP4, --other data ); CREATE TABLE network_events ( ip IP4 NOT NULL REFERENCES ip_info(ip), name VARCHAR, port INTEGER, --other data ); CREATE TABLE host_events ( ip IP4 NOT NULL REFERENCES ip_info(ip), name VARCHAR port INTEGER, --other data ); There is quite a bit of commonality between the network_events and host_events schemas, but they do not currently share an ancestor. ip_info has about 13 million rows, the network_events table has about 30 million rows, and the host_events table has about 7 million rows. There are indexes on all the rows. The query that I would like to execute is to select all the rows of ip_info that have either network or host events that meet some criteria, i.e. name='blah'. I have 3 different possibilities that I have thought of to execute this. First, 2 'ip IN (SELECT ...)' statements joined by an OR: SELECT * FROM ip_info WHERE ip IN (SELECT ip FROM network_events WHERE name='blah') OR ip IN (SELECT ip FROM host_events WHERE name='blah'); Next, 1 'ip IN (SELECT ... UNION SELECT ...) statement: SELECT * FROM ip_info WHERE ip IN (SELECT ip FROM network_events WHERE name='blah' UNION SELECT ip FROM host_events WHERE name='blah'); Or, finally, the UNION statment with DISTINCTs: SELECT * FROM ip_info WHERE ip IN (SELECT DISTINCT ip FROM network_events WHERE name='blah' UNION SELECT DISTINCT ip FROM host_events WHERE name='blah');