Skip site navigation (1) Skip section navigation (2)

Re: Query is not using index when it should

From: Stephan Szabo <sszabo(at)megazone(dot)bigpanda(dot)com>
To: Tomas Skäre <tomas(at)nocrew(dot)org>
Cc: pgsql-general(at)postgresql(dot)org, pgsql-performance(at)postgresql(dot)org
Subject: Re: Query is not using index when it should
Date: 2004-12-11 02:28:38
Message-ID: 20041210182453.G92467@megazone.bigpanda.com (view raw or flat)
Thread:
Lists: pgsql-generalpgsql-performancepgsql-sql
On Fri, 10 Dec 2004, Tomas [iso-8859-1] Skre wrote:

> I have a table that looks like this:
>
>          Table "public.cjm_object"
>   Column   |       Type        | Modifiers
> -----------+-------------------+-----------
>  timestamp | bigint            | not null
>  jobid     | bigint            | not null
>  objectid  | bigint            | not null
>  class     | integer           | not null
>  field     | character varying | not null

In 7.4.x and earlier, you need to cast the value you're comparing to into
a bigint in order to make sure the indexes are used (in your timestamp
case it appears to work because the value doesn't fit in a plain integer).
8.0 should handle this better.

> But when doing a search with objectid, class and field, it doesn't use
> the idx_cjm_object1 index.
> db=# explain analyze select * from cjm_object where objectid=4534 and class=12 and field='paroid';

Using one of
 objectid=4534::bigint
 objectid='4534'
 objectid=CAST(4534 as bigint)
rather than objectid=4534 should make this indexable in 7.4.x.

In response to

Responses

pgsql-performance by date

Next:From: Josh BerkusDate: 2004-12-11 05:40:18
Subject: Re: LIMIT causes SEQSCAN in subselect
Previous:From: Steinar H. GundersonDate: 2004-12-11 01:45:28
Subject: Re: Slow insert

pgsql-sql by date

Next:From: Michael FuhrDate: 2004-12-11 04:11:52
Subject: Re: Create Calendar
Previous:From: Matthew EngelbertDate: 2004-12-11 00:57:02
Subject: Re: Indexing Strategy

pgsql-general by date

Next:From: Michael GlaesemannDate: 2004-12-11 02:58:31
Subject: Re: information schema extra fields
Previous:From: Eric BrownDate: 2004-12-11 02:15:50
Subject: What's faster

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group