Re: multicolumn index join

From: "Scott Marlowe" <scott(dot)marlowe(at)gmail(dot)com>
To: "Stephen Ince" <since(at)opendemand(dot)com>
Cc: "Postgres General List" <pgsql-general(at)postgresql(dot)org>
Subject: Re: multicolumn index join
Date: 2008-05-13 16:50:51
Message-ID: dcc563d10805130950w3cb48d04m9d2c325c24b1b510@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, May 13, 2008 at 9:27 AM, Stephen Ince <since(at)opendemand(dot)com> wrote:
> I have to do a multicolumn self-join to find the median. I am using postgres
> 8.2. How do I force postgres to use an index scan? I have a multicolumn
> index but postgres is not using it.

While it's quite acceptable to force index usage during testing, it's
a bad idea to do so on a production server unless you have to.

From the psql command line do a "show all;" and look for the
enable_xxx settings. Those allow you to do things like turn off seq
scans (actually it makes them really expensive so that they don't get
chosen, usually).

enable_indexscan
enable_seqscan

Are the two to look for.

> Query
> ------------------------------------
> explain select e.time
> from page_view e, page_view d
> where e.test_run_id = d.test_run_id and e.web_resource_id =
> d.web_resource_id
> and e.web_resource_id = 3961 and e.test_run_id = 2

I'd rewrite this as:

explain analyze
select e.time
from page_view e
join page_view d
on ((e.test_run_id, e.web_resource_id) = (d.test_run_id,d.web_resource_id))
where e.web_resource_id = 3961 and e.test_run_id = 2

Which makes it more readable. Don't know if that syntax makes the
planner smarter or not. Note the addition of analyze up there. that
will help you know what's actually happening.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Glyn Astill 2008-05-13 16:55:37 Re: how can i get initdb
Previous Message Guillaume Lelarge 2008-05-13 16:48:38 Re: pg_standby / WAL archive-restore through system restarts