pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Chris Hoover <revoohc@gmail.com>
Cc: pgsql-admin@postgresql.org Admin <pgsql-admin@postgresql.org>
Subject: Re: any way to make query use index?
Date: Thu, 15 Nov 2007 11:25:24 -0500
Message-ID: <3669.1195143924@sss.pgh.pa.us> (raw)
In-Reply-To: <1d219a6f0711150714j6f192548xe4c134a25e5b57b2@mail.gmail.com>
References: <1d219a6f0711150714j6f192548xe4c134a25e5b57b2@mail.gmail.com>

"Chris Hoover" <revoohc@gmail.com> writes:
> SELECT rev_desc FROM rev_code_desc WHERE rev_code = lpad('0300', 4, '0')

> Is there anyway to make postgres use an index on this query?

> Here is the table definition:
> CREATE TABLE rev_code_desc
> (
>   rev_code character(4) NOT NULL,

Since rev_code is char(n), you need to cast the other thing to char(n)
as well:

SELECT rev_desc FROM rev_code_desc WHERE rev_code = lpad('0300', 4, '0')::char(4)

lpad() is declared to return text, which means that your original query
resolves as ... WHERE rev_code::text = lpad('0300', 4, '0'), which
means that the index on rev_code is useless (since it has bpchar
semantics not text semantics).

If changing the queries seems impractical, you could add an index on
rev_code::text, or reconsider the choice of column type.

			regards, tom lane



view thread (4+ messages)  latest in thread

Message-ID: <3669.1195143924@sss.pgh.pa.us>
Permalink:  ../3669.1195143924@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/3669.1195143924@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-admin@postgresql.org
  Cc: tgl@sss.pgh.pa.us, revoohc@gmail.com
  Subject: Re: any way to make query use index?
  In-Reply-To: <3669.1195143924@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox