Re: index being ignored for "limit n" queries

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Piotr Sulecki <Piotr(dot)Sulecki(at)sybilla(dot)traxelektronik(dot)pl>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: index being ignored for "limit n" queries
Date: 2005-08-09 16:03:32
Message-ID: 16746.1123603412@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Piotr Sulecki <Piotr(dot)Sulecki(at)sybilla(dot)traxelektronik(dot)pl> writes:
> I have two really big tables, the problem is with one of them. The table
> looks as follows:
> ...
> Indexes:
> "pakiety_pkey" PRIMARY KEY, btree (pktid)
> "pakiety_stid_received_idx" UNIQUE, btree (stid, received)
> "pakiety_measured_idx" btree (measured)
> "pakiety_received_idx" btree (received)
> "pakiety_stid_measured_idx" btree (stid, measured)

> trax=# explain select * from pakiety where stid = 234::smallint order by
> received desc limit 1;

The reason this won't use the (stid, received) index is that the
requested sort order doesn't match that index. Try

select * from pakiety where stid = 234::smallint
order by stid desc, received desc
limit 1;

There is code in CVS tip to recognize that the equality constraint on
stid allows the index to be considered as producing data ordered by
received alone --- but no released version will make that deduction
for you.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Alvaro Herrera 2005-08-09 16:11:08 Re: Solicitud de informacion de psql con php
Previous Message Martijn van Oosterhout 2005-08-09 15:59:46 Re: postgres & server encodings