From: Carlo Stonebanks <stonec.register@sympatico.ca>
To: 'Dave Crooke' <dcrooke@gmail.com>
To: 'Claudio Freire' <klaussfreire@gmail.com>
Cc: pgsql-performance@postgresql.org
Subject: Re: Composite keys
Date: Wed, 12 Oct 2011 00:39:08 -0400
Message-ID: <EF3BA8897ED94C449F8D3C2722AE4210@CAPRICA> (raw)
In-Reply-To: <CALi4UpjWC15S8D3w-8u7TDVRdcq+PtHoaCOBSppxPiKw-BXJVw@mail.gmail.com>
References: <CCB4B01D184042ECBBE456D1ACB185A4@CAPRICA>
<CAGTBQpYMzj+xufEQBr7_aL3ZjZWaADU1oF40-ji8n8GcpZLj1A@mail.gmail.com>
<CALi4UpjWC15S8D3w-8u7TDVRdcq+PtHoaCOBSppxPiKw-BXJVw@mail.gmail.com>
Thanks Dave & Claudio.
Unfortunately, my specific example had a primary key in it (based on a
real-world case) but this kind of distracted from the general point.
So with PG I will stick to the general SQL rule that IF I use compound keys
then we have the most selective columns to the left. correct?
_____
From: Dave Crooke [mailto:dcrooke@gmail.com]
Sent: October 11, 2011 9:28 PM
To: Claudio Freire
Cc: Carlo Stonebanks; pgsql-performance@postgresql.org
Subject: Re: [PERFORM] Composite keys
Claudio is on point, I'll be even more pointed ....
If pkey_id truly is a primary key in the database sense of the term, and
thus unique, then IIUC there is no circumstance in which your composite
index would ever even get used ... all it's doing is slowing down writes :-)
If the query is sufficiently selective on pkey_id to merit using an index,
then the planner will use the primary key index, because it's narrower; if
not, then the only other option is to do a full table scan because there is
no index of which another_id is a prefix.
There are only three options which make sense:
1. No additional indexes, just the primary key
2. An additional index on (another_id)
3. An additional index on (another_id, pkey_id)
4. Both 2. and 3.
Choosing between these depends on a lot of variables of the query mix in
practice ... you could set up both 2. and 3. and then see which indexes the
planner actually uses in practice and then decide which to keep.
The value in having pkey_id in the index in 3. is for queries whose primary
selectivity is on another_id, but which also have some selectivity on
pkey_id .... the planner can use an index scan to filter candidate rows /
blocks to look at. This is especially helpful if another_id is not very
selective and / or the rows are quite wide.
On gut feel, it seems unlikely that you'd have a real-world circumstance in
which it makes sense to choose option 4. but it can't be ruled out without
further context.
Cheers
Dave
On Tue, Oct 11, 2011 at 7:52 PM, Claudio Freire <klaussfreire@gmail.com>
wrote:
On Tue, Oct 11, 2011 at 5:16 PM, Carlo Stonebanks
<stonec.register@sympatico.ca> wrote:
> Question 2) Regardless of the answer to Question 1 - if another_id is not
> guaranteed to be unique, whereas pkey_id is - there any value to changing
> the order of declaration (more generally, is there a performance impact
for
> column ordering in btree composite keys?)
Multicolumn indices on (c1, c2, ..., cn) can only be used on where
clauses involving c1..ck with k<n.
So, an index on (a,b) does *not* help for querying on b.
Furthermore, if a is unique, querying on a or querying on a and b is
equally selective. b there is just consuming space and cpu cycles.
I'd say, although it obviously depends on the queries you issue, you
only need an index on another_id.
--
Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-performance
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 using the --to and --cc options:
reply via email
To: pgsql-performance@postgresql.org
Cc: stonec.register@sympatico.ca, dcrooke@gmail.com, klaussfreire@gmail.com
Subject: Re: Composite keys
In-Reply-To: <EF3BA8897ED94C449F8D3C2722AE4210@CAPRICA>
* 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