Re: index usage in multi-column ORDER BY

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Adam Pritchard" <vesper76(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: index usage in multi-column ORDER BY
Date: 2005-07-09 15:26:51
Message-ID: 4737.1120922811@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

"Adam Pritchard" <vesper76(at)gmail(dot)com> writes:
> CREATE TABLE t ( c1 INT, c2 INT );
> CREATE INDEX c1_idx ON t(c2);
> CREATE INDEX c2_idx ON t(c2);
> EXPLAIN SELECT * FROM t ORDER BY c1, c2;
> "Sort (cost=69.83..72.33 rows=1000 width=8)"
> " Sort Key: c1, c2"
> " -> Seq Scan on t (cost=0.00..20.00 rows=1000 width=8)"

> If we instead use an index on c1, c2:
> CREATE INDEX c1_c2_idx ON t(c1, c2);
> "Index Scan using c1_c2_idx on t (cost=0.00..52.00 rows=1000 width=8)"

The latter index matches the sort ordering requested by the query;
no single-column index can do so.

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2005-07-09 15:30:44 Re: Transaction isolation levels
Previous Message Geert Jansen 2005-07-09 14:44:29 Re: Transaction isolation levels