pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
any way to make query use index?
4+ messages / 2 participants
[nested] [flat]

* any way to make query use index?
@ 2007-11-15 15:14 Chris Hoover <revoohc@gmail.com>
  2007-11-15 16:25 ` Re: any way to make query use index? Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 4+ messages in thread

From: Chris Hoover @ 2007-11-15 15:14 UTC (permalink / raw)
  To: pgsql-admin

I am working through our application looking for ways to eliminate seq
scans in our database.  I am looking for some help with this query.

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,
  rev_desc text,
  user_id character varying(12),
  live_rec boolean DEFAULT true,
  CONSTRAINT rev_code_desc_pkey PRIMARY KEY (rev_code)
)

Right now, I can't seem to get it to use an index for this type of
query.  Of course, in our application it is SELECT rev_desc FROM
rev_code_desc WHERE rev_code = lpad(<program variable>, 4, '0').

Thanks for any help,

Chris

PG 8.1.3



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: any way to make query use index?
  2007-11-15 15:14 any way to make query use index? Chris Hoover <revoohc@gmail.com>
@ 2007-11-15 16:25 ` Tom Lane <tgl@sss.pgh.pa.us>
  2007-11-15 16:37   ` Re: any way to make query use index? Chris Hoover <revoohc@gmail.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Tom Lane @ 2007-11-15 16:25 UTC (permalink / raw)
  To: Chris Hoover <revoohc@gmail.com>; +Cc: pgsql-admin

"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



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: any way to make query use index?
  2007-11-15 15:14 any way to make query use index? Chris Hoover <revoohc@gmail.com>
  2007-11-15 16:25 ` Re: any way to make query use index? Tom Lane <tgl@sss.pgh.pa.us>
@ 2007-11-15 16:37   ` Chris Hoover <revoohc@gmail.com>
  2007-11-15 16:52     ` Re: any way to make query use index? Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 4+ messages in thread

From: Chris Hoover @ 2007-11-15 16:37 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-admin

On Nov 15, 2007 11:25 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:

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

> 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
>

how would you create this index?  create index rev_code_text_idx on
rev_code_desc(rev_code::text) fails complaining "ERROR:  syntax error
at or near "::""

Thanks

Chris



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: any way to make query use index?
  2007-11-15 15:14 any way to make query use index? Chris Hoover <revoohc@gmail.com>
  2007-11-15 16:25 ` Re: any way to make query use index? Tom Lane <tgl@sss.pgh.pa.us>
  2007-11-15 16:37   ` Re: any way to make query use index? Chris Hoover <revoohc@gmail.com>
@ 2007-11-15 16:52     ` Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 4+ messages in thread

From: Tom Lane @ 2007-11-15 16:52 UTC (permalink / raw)
  To: Chris Hoover <revoohc@gmail.com>; +Cc: pgsql-admin

"Chris Hoover" <revoohc@gmail.com> writes:
> On Nov 15, 2007 11:25 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>> If changing the queries seems impractical, you could add an index on
>> rev_code::text, or reconsider the choice of column type.

> how would you create this index?  create index rev_code_text_idx on
> rev_code_desc(rev_code::text) fails complaining "ERROR:  syntax error
> at or near "::""

You need more parentheses --- read the docs about expression indexes.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2007-11-15 16:52 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2007-11-15 15:14 any way to make query use index? Chris Hoover <revoohc@gmail.com>
2007-11-15 16:25 ` Tom Lane <tgl@sss.pgh.pa.us>
2007-11-15 16:37   ` Chris Hoover <revoohc@gmail.com>
2007-11-15 16:52     ` Tom Lane <tgl@sss.pgh.pa.us>

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