pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: worky.workerson@gmail.com
To: pgsql-performance@postgresql.org
Subject: Self-join query and index usage
Date: 14 Jul 2006 11:56:53 -0700
Message-ID: <1152903413.210226.284600@75g2000cwc.googlegroups.com> (raw)

I'm doing a self join of some shipping data and wanted to get the best
query possible.  The interesting table is the event table, and it has
the following structure:

  startnode int,
  endnode int,
  weight int,
  starttime timestamp,
  endtime timestamp

and the query that I would like to run is:

SELECT e1.endnode, count(*), sum(e1.weight) AS weight1, sum(e2.weight)
AS weight2
FROM event e1, event e2
WHERE e1.endnode = e2.startnode AND e1.starttime < e2.starttime AND
e2.starttime < e1.endtime
GROUP BY e1.endnode

Assuming that I have indexes on all the columns, should this query be
able to make use of the indexes on starttime and endtime?

The "best" plan that I could see is a merge join between a sorted
sequential scan on e2.startnode and an index scan on e1.endnode, which
I figure takes care of the "e1.endnode = e2.startnode".  The join
filter is then "e1.starttime < e2.starttime AND e2.starttime <
e1.endtime" ... does this use an index?  Can the planner to use a
bitmap index scan to use the indexes on the start/endtimes in the join?

Table is about 3GB.




view thread (2+ messages)  latest in thread

Message-ID: <1152903413.210226.284600@75g2000cwc.googlegroups.com>
Permalink:  ../1152903413.210226.284600@75g2000cwc.googlegroups.com/
Also on:    postgresql.org/message-id/1152903413.210226.284600@75g2000cwc.googlegroups.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 using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: worky.workerson@gmail.com
  Subject: Re: Self-join query and index usage
  In-Reply-To: <1152903413.210226.284600@75g2000cwc.googlegroups.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