Re: Order-by and indexes - Mailing list pgsql-novice

From Odd Hogstad
Subject Re: Order-by and indexes
Date
Msg-id BANLkTikyHAErwdxaTkJbzO179sTpKxg0TQ@mail.gmail.com
Whole thread Raw
In response to Re: Order-by and indexes  ("Jean-Yves F. Barbier" <12ukwn@gmail.com>)
List pgsql-novice
>> 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.

I added an index for this, and now it goes much faster, thanks. The ones that contain data is now superfast, 1-2 ms, but the ones that does not match, and returns empty sets, are considerably slower, 150 -200 ms. Is this something I have to live with, or are there any tricks that will make these queries fast to?

Thanks again!

Odd-R.

pgsql-novice by date:

Previous
From: Vincent Ficet
Date:
Subject: Re: pg_advisory_locks in a multithreaded application context
Next
From: Michael Wood
Date:
Subject: Re: Connecting to PostgreSQL server