X-Original-To: pgsql-performance-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id F0E379FAE4E for ; Fri, 14 Jul 2006 15:57:14 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 40552-03 for ; Fri, 14 Jul 2006 15:57:04 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from floppy.pyrenet.fr (news.pyrenet.fr [194.116.145.2]) by postgresql.org (Postfix) with ESMTP id 8931C9FAC76 for ; Fri, 14 Jul 2006 15:57:04 -0300 (ADT) Received: by floppy.pyrenet.fr (Postfix, from userid 106) id EBEDE30B98; Fri, 14 Jul 2006 20:57:00 +0200 (MET DST) From: worky.workerson@gmail.com X-Newsgroups: pgsql.performance Subject: Self-join query and index usage Date: 14 Jul 2006 11:56:53 -0700 Organization: http://groups.google.com Lines: 31 Message-ID: <1152903413.210226.284600@75g2000cwc.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" X-Complaints-To: groups-abuse@google.com User-Agent: G2/0.2 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.8.0.4) Gecko/20060508 Firefox/1.5.0.4,gzip(gfe),gzip(gfe) Complaints-To: groups-abuse@google.com Injection-Info: 75g2000cwc.googlegroups.com; posting-host=63.239.69.1; posting-account=EhkmOg0AAAAu8-rAx4_fA4J_FdZnfg8I To: pgsql-performance@postgresql.org X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200607/145 X-Sequence-Number: 20005 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.