best way to fetch next/prev record based on index - Mailing list pgsql-performance

From Merlin Moncure
Subject best way to fetch next/prev record based on index
Date
Msg-id 6EE64EF3AB31D5448D0007DD34EEB34101AEFE@Herge.rcsinc.local
Whole thread Raw
List pgsql-performance
> SELECT * FROM t WHERE
> (a >= a1 AND b>=b1 AND c>=c1) ORDER BY a,b,c LIMIT 1 OFFSET 1;
>
> using the way LIMIT cuts down on sort time (I've never tried it with
both
> LIMIT and OFFSET, though; you could always use LIMIT 2 and skip a
record
> client-side if that works better).

Don't want to further clutter the list (answered this question several
times already), but your query does not work.  What I meant to write
was:

select * from t where
    a >= a1 and
     (a >  a1 or b >= b1) and
     (a >  a1 or b > b1 or c > c1)
    order by a, b, c limit 1

The problem with your query is it excludes all values of c >= c1
regardless of values of a and b.

Merlin

pgsql-performance by date:

Previous
From: "Merlin Moncure"
Date:
Subject: Re: best way to fetch next/prev record based on index
Next
From: Tom Lane
Date:
Subject: Re: best way to fetch next/prev record based on index