pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Appropriate indices to create for these queries
2+ messages / 2 participants
[nested] [flat]

* Appropriate indices to create for these queries
@ 2001-04-03 01:49  Gerald Gutierrez <gutz@kalador.com>
  0 siblings, 1 reply; 2+ messages in thread

From: Gerald Gutierrez @ 2001-04-03 01:49 UTC (permalink / raw)
  To: pgsql-sql


I've been looking into indices and which ones to create and I'm getting
myself a little confused. The "PostgreSQL Introduction and Concepts" book
didn't help very much. I wonder if a kind soul can give me some tips.

SELECT * FROM T1 WHERE a=1 and b='hello';

Is the appropriate index for this query:

CREATE INDEX ndx ON T1 (a, b) ?

When I was testing, it seemed that even if I created the index with only
"a", EXPLAIN told me that it would just do an index scan, seemingly
indicating that it didn't matter whether I had an "a" index, or an "a, b"
index.

How about for WHERE a=1 or b='hello' and other more complex forms? Is there
documentation that describes a variety of different queries and what kind of
indices are best?

Thanks.




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

* Re: Appropriate indices to create for these queries
@ 2001-04-03 08:03  Richard Huxton <dev@archonet.com>
  parent: Gerald Gutierrez <gutz@kalador.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Richard Huxton @ 2001-04-03 08:03 UTC (permalink / raw)
  To: Gerald Gutierrez <gutz@kalador.com>; pgsql-sql

From: "Gerald Gutierrez" <gutz@kalador.com>

>
> I've been looking into indices and which ones to create and I'm getting
> myself a little confused. The "PostgreSQL Introduction and Concepts" book
> didn't help very much. I wonder if a kind soul can give me some tips.
>
> SELECT * FROM T1 WHERE a=1 and b='hello';
>
> Is the appropriate index for this query:
>
> CREATE INDEX ndx ON T1 (a, b) ?

Maybe - you seem to have got to the core of the matter below...

> When I was testing, it seemed that even if I created the index with only
> "a", EXPLAIN told me that it would just do an index scan, seemingly
> indicating that it didn't matter whether I had an "a" index, or an "a, b"
> index.

For the above query, any of : index on "a" , "b", "a,b" will probably be
used (if you have enough data to justify it).

> How about for WHERE a=1 or b='hello' and other more complex forms? Is
there
> documentation that describes a variety of different queries and what kind
of
> indices are best?

In this case, an index on "a,b" isn't much use since the b='hello' values
are presumably scattered amongst all the various 'a' values.

In practice, unless you do a lot of a=1 and b="hello" queries you're
probably better off with separate indexes on a and b, or possibly even just
on one of them.

I tend to apply indexes to fields that take part in a join then add them one
at a time to other fields as it becomes clear which takes part in important
queries. Don't forget that it takes a certain amount of effort to maintain
an index.

You've already found the EXPLAIN command - this is your best guide to where
an index can be useful.

- Richard Huxton




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


end of thread, other threads:[~2001-04-03 08:03 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2001-04-03 01:49 Appropriate indices to create for these queries Gerald Gutierrez <gutz@kalador.com>
2001-04-03 08:03 ` Richard Huxton <dev@archonet.com>

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