Re: Order-by and indexes

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Odd Hogstad <odd(dot)hogstad(at)smartm(dot)no>
Cc: "pgsql-novice(at)postgresql(dot)org" <pgsql-novice(at)postgresql(dot)org>
Subject: Re: Order-by and indexes
Date: 2011-06-29 15:06:44
Message-ID: 6005.1309360004@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Odd Hogstad <odd(dot)hogstad(at)smartm(dot)no> writes:
> I need to get the latest entry of a large table matching a certain criteria.
> This is my query:

> SELECT * FROM "data" WHERE "data"."fk" = 238496 ORDER BY "data"."id" DESC
> LIMIT 1

A two-column index on (fk, id) would help for this. If you think about
the ordering of the index entries you'll see why.

regards, tom lane

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Vincent Ficet 2011-06-29 15:11:52 pg_advisory_locks in a multithreaded application context
Previous Message Jean-Yves F. Barbier 2011-06-29 15:02:54 Re: Order-by and indexes