Re: Optimizing No matching record Queries

From: Gregory Stark <stark(at)enterprisedb(dot)com>
To: "Pallav Kalva" <pkalva(at)livedatagroup(dot)com>
Cc: "Richard Huxton" <dev(at)archonet(dot)com>, "Dean Gibson \(DB Administrator\)" <postgresql(at)ultimeth(dot)com>, <pgsql-performance(at)postgresql(dot)org>
Subject: Re: Optimizing No matching record Queries
Date: 2008-02-13 20:51:34
Message-ID: 877ih8bmw9.fsf@oxford.xeocode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

"Pallav Kalva" <pkalva(at)livedatagroup(dot)com> writes:

> This index would fix this problem but in general I would like to know what if
> there are queries where it does "index scan backwards" and there is no "order
> by clause" and the query is still bad ? Would there be a case like that or the
> planner uses index scan backwards only when use order by desc also.

I think you're oversimplifying. Basically you were asking the planner for the
most recent record for a given user. The planner had the choice either of

a) going through all the records for a given user and picking the most recent,

or b) scanning the records from most recent to oldest and looking for the
given user.

It was a choice between two evils. If there are a lot of records for the user
then a) will be bad since it has to scan all of them to find the most recent
and if there are no records for the user then b) will be bad because it'll
have to go through all of the records to the beginning of time.

The suggested index lets it scan the records for the given user from most
recent to oldest without seeing any records for any other user.

--
Gregory Stark
EnterpriseDB http://www.enterprisedb.com
Ask me about EnterpriseDB's Slony Replication support!

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Tobias Brox 2008-02-13 21:06:55 Re: Anyone using a SAN?
Previous Message Tore Halset 2008-02-13 20:28:21 Re: Dell Perc/6