pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Michael Artz <mlartz@gmail.com>
To: pgsql-performance@postgresql.org
Subject: Subselect query enhancement
Date: Thu, 1 Feb 2007 11:42:03 -0500
Message-ID: <e9c163070702010842ja639378v75e8c0eb757d9b72@mail.gmail.com> (raw)

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');



view thread (10+ messages)  latest in thread

Message-ID: <e9c163070702010842ja639378v75e8c0eb757d9b72@mail.gmail.com>
Permalink:  ../e9c163070702010842ja639378v75e8c0eb757d9b72@mail.gmail.com/
Also on:    postgresql.org/message-id/e9c163070702010842ja639378v75e8c0eb757d9b72@mail.gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-performance@postgresql.org
  Cc: mlartz@gmail.com
  Subject: Re: Subselect query enhancement
  In-Reply-To: <e9c163070702010842ja639378v75e8c0eb757d9b72@mail.gmail.com>
  References: <e9c163070702010842ja639378v75e8c0eb757d9b72@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox