Re: select single entry and its neighbours using direct-acess to index? - Mailing list pgsql-general

From Andrew - Supernews
Subject Re: select single entry and its neighbours using direct-acess to index?
Date
Msg-id slrncr8tcj.sjo.andrew+nonews@trinity.supernews.net
Whole thread Raw
In response to select single entry and its neighbours using direct-acess to index?  (peter pilsl <pilsl@goldfisch.at>)
Responses Re: select single entry and its neighbours using direct-acess to index?  (Pierre-Frédéric Caillaud<lists@boutiquenumerique.com>)
List pgsql-general
On 2004-12-06, Pierre-Frédéric Caillaud <lists@boutiquenumerique.com>
wrote:
> SELECT * FROM test WHERE (name='b' and name2>'a') OR (name>'b') ORDER BY
> name,name2 ASC LIMIT 1;

Write that WHERE clause instead as:

 WHERE name>='b' AND (name>'b' OR (name='b' AND name2>'a'))

This is logically equivalent, but it gives the planner a better handle on
how to use an index scan to satisfy the query.

> SELECT * FROM test WHERE (name='b' and name2<'a') OR (name<'b') ORDER BY
> name,name2 DESC LIMIT 1;

That needs to be ORDER BY name DESC, name2 DESC (the direction indicator
applies per-column and not to the output ordering). Same goes for the
WHERE clause in this query as the previous one, too.

--
Andrew, Supernews
http://www.supernews.com - individual and corporate NNTP services

pgsql-general by date:

Previous
From: Daniel Martini
Date:
Subject: Re: When to encrypt
Next
From: Pierre-Frédéric Caillaud
Date:
Subject: Re: select single entry and its neighbours using direct-acess to index?