Re: any way to make query use index?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Chris Hoover" <revoohc(at)gmail(dot)com>
Cc: "pgsql-admin(at)postgresql(dot)org Admin" <pgsql-admin(at)postgresql(dot)org>
Subject: Re: any way to make query use index?
Date: 2007-11-15 16:25:24
Message-ID: 3669.1195143924@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

"Chris Hoover" <revoohc(at)gmail(dot)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

In response to

Responses

Browse pgsql-admin by date

  From Date Subject
Next Message Chris Hoover 2007-11-15 16:37:49 Re: any way to make query use index?
Previous Message Tomeh, Husam 2007-11-15 16:16:35 Re: Error: duplicate key violates unique constraint - "pg_toast_3270368541_index