Re: Query only slow on first run

From: "Steinar H(dot) Gunderson" <sgunderson(at)bigfoot(dot)com>
To: cluster <skrald(at)amossen(dot)dk>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: Query only slow on first run
Date: 2007-11-27 23:38:33
Message-ID: 20071127233833.GA24031@uio.no
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

On Tue, Nov 27, 2007 at 11:51:40PM +0100, cluster wrote:
> A thing that strikes me is the following. As you can see I have the
> constraint: q.status = 1. Only a small subset of the data set has this
> status. I have an index on q.status but for some reason this is not used.
> Instead the constraint are ensured with a "Filter: (q.status = 1)" in an
> index scan for the primary key in the "q" table. If the small subset having
> q.status = 1 could be isolated quickly using an index, I would expect the
> query to perform better. I just don't know why the planner doesn't use the
> index on q.status.

An index scan (as opposed to a bitmap index scan) can only use one index at a
time, so it will choose the most selective one. Here it quite correctly
recognizes that there will only be one matching record for the given
question_id, so it uses the primary key instead.

You could make an index on (question_id,status) (or a partial index on
question id, with status=1 as the filter), but I'm not sure how much it would
help you unless the questions table is extremely big. It doesn't appear to
be; in fact, it appears to be all in RAM, so that's not your bottleneck.

/* Steinar */
--
Homepage: http://www.sesse.net/

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Dave Dutcher 2007-11-27 23:47:31 Re: Query only slow on first run
Previous Message Pablo Alcaraz 2007-11-27 23:06:34 Re: TB-sized databases