pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedSingle vs. multiple indexes
6+ messages / 5 participants
[nested] [flat]
* Single vs. multiple indexes
@ 1998-07-09 16:35 Tom Lane <tgl@sss.pgh.pa.us>
1998-07-09 17:16 ` Re: [SQL] Single vs. multiple indexes Bruce Momjian <maillist@candle.pha.pa.us>
0 siblings, 1 reply; 6+ messages in thread
From: Tom Lane @ 1998-07-09 16:35 UTC (permalink / raw)
To: pgsql-sql
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?
regards, tom lane
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: [SQL] Single vs. multiple indexes
1998-07-09 16:35 Single vs. multiple indexes Tom Lane <tgl@sss.pgh.pa.us>
@ 1998-07-09 17:16 ` Bruce Momjian <maillist@candle.pha.pa.us>
1998-07-09 18:04 ` Re: [SQL] Single vs. multiple indexes Jeff Aitken <jaitken@dimension.net>
0 siblings, 1 reply; 6+ messages in thread
From: Bruce Momjian @ 1998-07-09 17:16 UTC (permalink / raw)
To: tgl@sss.pgh.pa.us; +Cc: pgsql-sql
> 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)
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: [SQL] Single vs. multiple indexes
1998-07-09 16:35 Single vs. multiple indexes Tom Lane <tgl@sss.pgh.pa.us>
1998-07-09 17:16 ` Re: [SQL] Single vs. multiple indexes Bruce Momjian <maillist@candle.pha.pa.us>
@ 1998-07-09 18:04 ` Jeff Aitken <jaitken@dimension.net>
1998-07-09 18:13 ` Re: [SQL] Single vs. multiple indexes Bruce Momjian <maillist@candle.pha.pa.us>
0 siblings, 1 reply; 6+ messages in thread
From: Jeff Aitken @ 1998-07-09 18:04 UTC (permalink / raw)
To: maillist@candle.pha.pa.us; +Cc: tgl@sss.pgh.pa.us; pgsql-sql
Bruce Momjian writes:
> 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.
>
Just to be sure I understand what you're saying here, let's assume
I've got a table with three integer fields a, b, and c. I can do
one of two things:
1. Create an index on (a), an index on (b), and an index on (c).
2. Create an index on (a, b, c).
Now let's assume I perform a query that references b only. In this
case, if I understand you correctly, method #1 above is better,
because the index in method #2 will not be used.
However, if I perform a query that references a and b, then method
#2 would be better, because the composite index will be used,
whereas in method #1, only one of the two relevant indices would be
chosen?
--Jeff
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: [SQL] Single vs. multiple indexes
1998-07-09 16:35 Single vs. multiple indexes Tom Lane <tgl@sss.pgh.pa.us>
1998-07-09 17:16 ` Re: [SQL] Single vs. multiple indexes Bruce Momjian <maillist@candle.pha.pa.us>
1998-07-09 18:04 ` Re: [SQL] Single vs. multiple indexes Jeff Aitken <jaitken@dimension.net>
@ 1998-07-09 18:13 ` Bruce Momjian <maillist@candle.pha.pa.us>
0 siblings, 0 replies; 6+ messages in thread
From: Bruce Momjian @ 1998-07-09 18:13 UTC (permalink / raw)
To: jaitken@dimension.net; +Cc: tgl@sss.pgh.pa.us; pgsql-sql
> Bruce Momjian writes:
> > 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.
> >
>
> Just to be sure I understand what you're saying here, let's assume
> I've got a table with three integer fields a, b, and c. I can do
> one of two things:
>
> 1. Create an index on (a), an index on (b), and an index on (c).
> 2. Create an index on (a, b, c).
>
> Now let's assume I perform a query that references b only. In this
> case, if I understand you correctly, method #1 above is better,
> because the index in method #2 will not be used.
>
> However, if I perform a query that references a and b, then method
> #2 would be better, because the composite index will be used,
> whereas in method #1, only one of the two relevant indices would be
> chosen?
Exactly.
--
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)
^ permalink raw reply [nested|flat] 6+ messages in thread
* RE: [SQL] Single vs. multiple indexes
@ 1998-07-10 17:37 Jackson, DeJuan <djackson@cpsgroup.com>
1998-07-12 11:03 ` RE: [SQL] Single vs. multiple indexes Herouth Maoz <herouth@oumail.openu.ac.il>
0 siblings, 1 reply; 6+ messages in thread
From: Jackson, DeJuan @ 1998-07-10 17:37 UTC (permalink / raw)
To: Bruce Momjian <maillist@candle.pha.pa.us>; jaitken@dimension.net; +Cc: tgl@sss.pgh.pa.us; pgsql-sql
> > Bruce Momjian writes:
> > > 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.
> > >
> >
> > Just to be sure I understand what you're saying here, let's assume
> > I've got a table with three integer fields a, b, and c. I can do
> > one of two things:
> >
> > 1. Create an index on (a), an index on (b), and an index on (c).
> > 2. Create an index on (a, b, c).
> >
> > Now let's assume I perform a query that references b only. In this
> > case, if I understand you correctly, method #1 above is better,
> > because the index in method #2 will not be used.
> >
> > However, if I perform a query that references a and b, then method
> > #2 would be better, because the composite index will be used,
> > whereas in method #1, only one of the two relevant indices would be
> > chosen?
>
> Exactly.
>
But let's not forget one of the wonders of PostgreSQL. In this world
you can have your cake and eat it to. So, create all 4 indices and let
the optimizer sort em out for your queries.
-DEJ
^ permalink raw reply [nested|flat] 6+ messages in thread
* RE: [SQL] Single vs. multiple indexes
1998-07-10 17:37 RE: [SQL] Single vs. multiple indexes Jackson, DeJuan <djackson@cpsgroup.com>
@ 1998-07-12 11:03 ` Herouth Maoz <herouth@oumail.openu.ac.il>
0 siblings, 0 replies; 6+ messages in thread
From: Herouth Maoz @ 1998-07-12 11:03 UTC (permalink / raw)
To: pgsql-sql
At 20:37 +0300 on 10/7/98, Jackson, DeJuan wrote:
> >
> But let's not forget one of the wonders of PostgreSQL. In this world
> you can have your cake and eat it to. So, create all 4 indices and let
> the optimizer sort em out for your queries.
Yes, but whenever you update or insert a row, it's that much slower. One
should always remember not to go wild with indexing - they carry a penalty
on INSERT and UPDATE.
Herouth
--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma
^ permalink raw reply [nested|flat] 6+ messages in thread
end of thread, other threads:[~1998-07-12 11:03 UTC | newest]
Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
1998-07-09 16:35 Single vs. multiple indexes Tom Lane <tgl@sss.pgh.pa.us>
1998-07-09 17:16 ` Bruce Momjian <maillist@candle.pha.pa.us>
1998-07-09 18:04 ` Jeff Aitken <jaitken@dimension.net>
1998-07-09 18:13 ` Bruce Momjian <maillist@candle.pha.pa.us>
1998-07-10 17:37 RE: [SQL] Single vs. multiple indexes Jackson, DeJuan <djackson@cpsgroup.com>
1998-07-12 11:03 ` Herouth Maoz <herouth@oumail.openu.ac.il>
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