pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Bruce Momjian <maillist@candle.pha.pa.us>
To: tgl@sss.pgh.pa.us
Cc: pgsql-sql@postgreSQL.org
Subject: Re: [SQL] Single vs. multiple indexes
Date: Thu, 9 Jul 1998 13:16:27 -0400 (EDT)
Message-ID: <199807091716.NAA28651@candle.pha.pa.us> (raw)
In-Reply-To: <2366.900002129@sss.pgh.pa.us>

> Question: what's the difference between making a single index covering
> multiple fields of the underlying table, and making a separate index
> for each of the fields?
> 
> I understand that the semantics are different if I create a UNIQUE
> index: a unique index on a field enforces that every record in the table
> have a different value of that field, but a unique index on a set of
> fields only enforces that the combination of all those fields be unique
> in each record.
> 
> Leaving that aside, what are the performance implications?  Does one
> structure support fast execution of query types that the other doesn't,
> and if so which one's better for what?  Is a single index cheaper to
> update than multiple indexes?  Does it save disk space?
> 

Good question.  The optimizer can only use only one index in a query, so
if you create five indexes, the optimizer will pick the best one to use,
and disregard the rest.  If you create one index with five fields, it
will use as much of the index as it can.  If you restrict on the first
field of the index, it will use only that part of the index.  If you
restrict on the first three fields, it will use all three parts,
resulting in better performance than just having an index on the first
field.

If you restrict only on the second field of the index, the index is
useless and will not be used.

Hope this answers the question.

-- 
Bruce Momjian                          |  830 Blythe Avenue
maillist@candle.pha.pa.us              |  Drexel Hill, Pennsylvania 19026
  +  If your life is a hard drive,     |  (610) 353-9879(w)
  +  Christ can be your backup.        |  (610) 853-3000(h)



view thread (6+ messages)  latest in thread

Message-ID: <199807091716.NAA28651@candle.pha.pa.us>
Permalink:  ../199807091716.NAA28651@candle.pha.pa.us/
Also on:    postgresql.org/message-id/199807091716.NAA28651@candle.pha.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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: maillist@candle.pha.pa.us, tgl@sss.pgh.pa.us
  Subject: Re: [SQL] Single vs. multiple indexes
  In-Reply-To: <199807091716.NAA28651@candle.pha.pa.us>
  References: <2366.900002129@sss.pgh.pa.us> <199807091716.NAA28651@candle.pha.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