Re: Query not using the indexes properly.

From: Chris <dmagick(at)gmail(dot)com>
To: Tim Uckun <timuckun(at)gmail(dot)com>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Query not using the indexes properly.
Date: 2009-10-02 02:04:29
Message-ID: 4AC55FAD.7040509@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Tim Uckun wrote:
> I have a pretty simple query on a pretty simple table with about 60
> million records in it.
>
> This is the query.
>
> SELECT * FROM "changes" WHERE (id > 1935759 and company_id = 4 and
> source_model_name = 'CommissionedVisit') ORDER BY id ASC LIMIT 1
>
>
> The id field is the primary key. The other fields are indexed
> (company_id and source_model_name).
>
> This query takes about 30 seconds to run on a pretty beefy machine.
>
> Here is the explain.
>
> "Limit (cost=0.00..7.46 rows=1 width=45) (actual
> time=28799.712..28799.712 rows=0 loops=1)"
> " -> Index Scan using changes_pkey on changes
> (cost=0.00..2331939.52 rows=312519 width=45) (actual
> time=28799.710..28799.710 rows=0 loops=1)"
> " Index Cond: (id > 1935759)"
> " Filter: ((company_id = 4) AND ((source_model_name)::text =
> 'CommissionedVisit'::text))"
> "Total runtime: 28799.749 ms"
>
>
> It seem to me that it's ignoring the indexes on the text fields. Is
> that right?

It probably thinks the id check is going to be better to limit the
result set.

How many records are there for id > 1935759 ?
vs
How many records for company_id = 4 and source_model_name =
'CommissionedVisit' ?

If this is a common query you could probably do a multi-column index on
all 3 columns (id, company_id, source_model_name) - but if company_id
and source_model_name have a low number of distinct values, then it's
not going to help.

--
Postgresql & php tutorials
http://www.designmagick.com/

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message David Wilson 2009-10-02 02:18:56 Re: Query not using the indexes properly.
Previous Message Tim Uckun 2009-10-02 01:47:23 Query not using the indexes properly.