Multicolumn indexes and equal conditions

From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: pgsql-performance(at)postgresql(dot)org
Subject: Multicolumn indexes and equal conditions
Date: 2003-04-14 01:45:47
Message-ID: 20030414014547.GA19450@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

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)

Browse pgsql-performance by date

  From Date Subject
Next Message linweidong 2003-04-15 09:44:44 for help!
Previous Message Tom Lane 2003-04-12 20:53:23 Re: update query blows out