Skip site navigation (1) Skip section navigation (2)

Re: best way to fetch next/prev record based on index

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Merlin Moncure" <merlin(dot)moncure(at)rcsonline(dot)com>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: best way to fetch next/prev record based on index
Date: 2004-07-27 15:14:01
Message-ID: 28700.1090941241@sss.pgh.pa.us (view raw or flat)
Thread:
Lists: pgsql-performance
"Merlin Moncure" <merlin(dot)moncure(at)rcsonline(dot)com> writes:
> So, for a table t with a three part key over columns a,b,c, the query to
> read the next value from t for given values a1, b1, c1 is

> select * from t where
> 	a >= a1 and
>      (a >  a1 or b >= b1) and
>      (a >  a1 or b > b1 or c > c1)

> In about 95% of cases, the planner correctly selects the index t(a,b,c)
> and uses it.

I'm surprised it's that good.  Why not do

	select * from t where a >= a1 and b >= b1 and c >= c1
	order by a,b,c
	limit 1 offset 1;

which has a much more obvious translation to an indexscan.

			regards, tom lane

In response to

pgsql-performance by date

Next:From: Merlin MoncureDate: 2004-07-27 15:29:53
Subject: Re: best way to fetch next/prev record based on index
Previous:From: Merlin MoncureDate: 2004-07-27 15:03:14
Subject: Re: best way to fetch next/prev record based on index

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group