pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Bruno Wolff III <bruno@wolff.to>
To: pgsql-performance@postgresql.org
Subject: Multicolumn indexes and equal conditions
Date: Sun, 13 Apr 2003 20:45:47 -0500
Message-ID: <20030414014547.GA19450@wolff.to> (raw)

I noticed that when a multicolumn index exists it isn't necessarily
fully used when the first column is constrained by an equals condition.
However by adding a redundant sort condition you can get both columns
used.

In the following examples crate has an index on gameid and areaid.

The examples below are for 7.4 development, but 7.3.2 behaves similarly.

explain analyze select areaid from crate where gameid = 'TTN' order by areaid;
                                                         QUERY PLAN                                                         
----------------------------------------------------------------------------------------------------------------------------
 Sort  (cost=132.93..133.02 rows=36 width=11) (actual time=5.44..5.57 rows=287 loops=1)
   Sort Key: areaid
   ->  Index Scan using crate_game on crate  (cost=0.00..132.00 rows=36 width=11) (actual time=0.06..1.94 rows=287 loops=1)
         Index Cond: (gameid = 'TTN'::text)
 Total runtime: 5.81 msec
(5 rows)


explain analyze select areaid from crate where gameid = 'TTN' order by gameid, areaid;
                                                      QUERY PLAN                                                      
----------------------------------------------------------------------------------------------------------------------
 Index Scan using crate_game on crate  (cost=0.00..132.00 rows=36 width=18) (actual time=0.08..2.06 rows=287 loops=1)
   Index Cond: (gameid = 'TTN'::text)
 Total runtime: 2.51 msec
(3 rows)




Message-ID: <20030414014547.GA19450@wolff.to>
Permalink:  ../20030414014547.GA19450@wolff.to/
Also on:    postgresql.org/message-id/20030414014547.GA19450@wolff.to

 · 

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-performance@postgresql.org
  Cc: bruno@wolff.to
  Subject: Re: Multicolumn indexes and equal conditions
  In-Reply-To: <20030414014547.GA19450@wolff.to>
  References: <20030414014547.GA19450@wolff.to>

* 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