Re: Confusion about composite indexes

From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: Dmitriy Igrishin <dmitigr(at)gmail(dot)com>
Cc: Bill Mitchell <bill(at)publicrelay(dot)com>, General PostgreSQL List <pgsql-general(at)postgresql(dot)org>
Subject: Re: Confusion about composite indexes
Date: 2012-05-21 21:11:21
Message-ID: CAHyXU0xnN6-H0ks0Un+fzSQ5-WHhqW=ZTtBTOjjeU=vCD2C4=A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Mon, May 21, 2012 at 3:36 PM, Dmitriy Igrishin <dmitigr(at)gmail(dot)com> wrote:
>> So you can get fully index lookups on all of a, b, ab, and ba.  the
>> primary key can't optimize ba because indexes only fully match if
>> candidate fields are supplied from left to right order.  They can
>> still help somewhat, but to a lesser degree.
>
> BTW, I would like to know is it worth it to create 3rd index on map(a)
> to reduce the size of the index which will be used by the planer
> to save some server's RAM (obviously, at the cost of extra disk space) ?

What Dmitriy is talking about here is that even though an index on
(a,b) can efficiently (in terms of searching through the tree) match
terms on just 'a', you still pay a price because the entries on the
index have to store the data for b as well, So even though it's
algorithmically efficient you have to browse more data to do it which
pressures RAM. In other words, an index on just 'a' is ideal for
searches on just 'a', although a,b is much better than (b,a) or no
index at all.

I personally think that generally it's better not to do that in most
cases especially if you're indexing integer keys since you're not
making *that* much difference on the overall index size. Also,
primary key indexes are much more likely to have to stay 'hot' in the
cache anyways since they will be serving fkey reference lookups and
stuff like that so in the end you might be consuming *more* ram, not
less.

An exception might be if your key on a,b has a very small 'a' and a
very large 'b'. But that's pretty rare in practice and it's usually a
good idea to avoid indexing large fields if you can help it. It
really depends on the workload.

merlin

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Bill Mitchell 2012-05-21 21:41:37 Re: Confusion about composite indexes
Previous Message Lonni J Friedman 2012-05-21 21:05:51 significant performance hit whenever autovacuum runs after upgrading from 9.0 -> 9.1