Re: where+orderby+limit not (always) using appropriate index?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Szűcs Gábor <surrano(at)gmail(dot)com>
Cc: "pgsql-performance(at)postgresql(dot)org" <pgsql-performance(at)postgresql(dot)org>
Subject: Re: where+orderby+limit not (always) using appropriate index?
Date: 2005-05-18 15:14:39
Message-ID: 21957.1116429279@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

=?ISO-8859-2?Q?Sz=FBcs_G=E1bor?= <surrano(at)gmail(dot)com> writes:
> Create a table with (at least) two fields, say i and o.
> Create three indexes on (i), (o), (i,o)
> Insert enough rows to test.
> Try to replace min/max aggregates with indexable queries such as:

> SELECT o FROM t WHERE i = 1 ORDER BY o LIMIT 1;

> Problem #1: This tends to use one of the single-column indexes (depending on
> the frequency of the indexed element), not the two-column index. Also, I'm
> not perfectly sure but maybe the planner is right. Why?

To get the planner to use the double-column index, you have to use an
ORDER BY that matches the index, eg

SELECT o FROM t WHERE i = 1 ORDER BY i,o LIMIT 1;

regards, tom lane

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Vivek Khera 2005-05-18 19:08:17 Re: [pgsql-benchmarks] Error when try installing pgbench ?
Previous Message PFC 2005-05-18 15:06:51 Re: where+orderby+limit not (always) using appropriate index?