Re: Composite keys

From: "Carlo Stonebanks" <stonec(dot)register(at)sympatico(dot)ca>
To: "'Dave Crooke'" <dcrooke(at)gmail(dot)com>, "'Claudio Freire'" <klaussfreire(at)gmail(dot)com>
Cc: <pgsql-performance(at)postgresql(dot)org>
Subject: Re: Composite keys
Date: 2011-10-12 04:39:08
Message-ID: EF3BA8897ED94C449F8D3C2722AE4210@CAPRICA
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

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(at)gmail(dot)com]
Sent: October 11, 2011 9:28 PM
To: Claudio Freire
Cc: Carlo Stonebanks; pgsql-performance(at)postgresql(dot)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(at)gmail(dot)com>
wrote:

On Tue, Oct 11, 2011 at 5:16 PM, Carlo Stonebanks
<stonec(dot)register(at)sympatico(dot)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(at)postgresql(dot)org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-performance

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Svetlin Manavski 2011-10-12 08:55:46 Join over two tables of 50K records takes 2 hours
Previous Message Dave Crooke 2011-10-12 01:28:29 Re: Composite keys