pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Re: [SQL] query to do a backward 'skip', possible with (index) optimization ?
3+ messages / 3 participants
[nested] [flat]

* Re: [SQL] query to do a backward 'skip', possible with (index) optimization ?
@ 2000-01-10 19:14 Alain TESIO <alain_tesio@yahoo.com>
  0 siblings, 0 replies; 3+ messages in thread

From: Alain TESIO @ 2000-01-10 19:14 UTC (permalink / raw)
  To: Albert Loo <albertsql@hotmail.com>; +Cc: pgsql-sql

> > => select * from mytable where id 
> < '12345' order by id desc limit 1;
> 
> > - which returned the correct 
> result '12344', but without optimization.

Just an idea : add another column
negative_id which is equal to -id and
reverse the test ...

Alain

__________________________________________________
Do You Yahoo!?
Talk to your friends online with Yahoo! Messenger.
http://im.yahoo.com



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* query to do a backward 'skip', possible with (index) optimization ?
@ 2000-01-10 21:15 Albert Loo <albertsql@hotmail.com>
  2000-01-10 15:17 ` Re: [SQL] query to do a backward 'skip', possible with (index) optimization ? Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 3+ messages in thread

From: Albert Loo @ 2000-01-10 21:15 UTC (permalink / raw)
  To: pgsql-sql

hi,

Coming from xbase background and having just started on sql, I tried sql 
queries that does xbase 'skip'. I wanted to implement record browsing with 
'Next' and 'Previous' features. Currently, I'm testing this with a table of 
about 20,000 rows using psql.

I'm able to perform 'forward skip' query with :

=> select * from mytable where id > '12345' order by id limit 1;

- which returned what I wanted, 1 record with id value '12346'. Indexing on 
id further optimised the query.

However, I'm unable to optimise query when doing a 'backward skip'. I've 
tried :

=> select * from mytable where id < '12345' order by id desc limit 1;

- which returned the correct result '12344', but without optimization.

I was hoping a descending order index key would optimise it, but I think 
postgresql don't support it (yet ?).

Next, I tried using offset -1...which is not possible either :)

=> select * from mytable where id > '12345' order by id limit 1 offset -1;
ERROR:  parser: parse error at or near "-"

Has anyone tried 'backward skip' with optimization ?

______________________________________________________
Get Your Private, Free Email at http://www.hotmail.com




^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: [SQL] query to do a backward 'skip', possible with (index) optimization ?
  2000-01-10 21:15 query to do a backward 'skip', possible with (index) optimization ? Albert Loo <albertsql@hotmail.com>
@ 2000-01-10 15:17 ` Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 3+ messages in thread

From: Tom Lane @ 2000-01-10 15:17 UTC (permalink / raw)
  To: Albert Loo <albertsql@hotmail.com>; +Cc: pgsql-sql

"Albert Loo" <albertsql@hotmail.com> writes:
> => select * from mytable where id < '12345' order by id desc limit 1;

> - which returned the correct result '12344', but without optimization.

6.5.* doesn't know how to do that using an index (but 7.0 will).  I seem
to recall that someone posted a patch that partially solved the problem
in 6.5.* sources; check the archives if you are in a hurry.

BTW, I think you're probably going at this the wrong way, because it
doesn't extend to the case of fetching the *next* record in a
prespecified query.  You probably want to use DECLARE CURSOR and FETCH.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2000-01-10 21:15 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2000-01-10 19:14 Re: [SQL] query to do a backward 'skip', possible with (index) optimization ? Alain TESIO <alain_tesio@yahoo.com>
2000-01-10 21:15 query to do a backward 'skip', possible with (index) optimization ? Albert Loo <albertsql@hotmail.com>
2000-01-10 15:17 ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox