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

Peripheral Links

Header And Logo

PostgreSQL
| The world's most advanced open source database.

Site Navigation

Search for
  Advanced Search

Re: consistent random order



Jeff Herrin wrote:
I don't think cursors are going to help in this case. The order by random() is still going to give different result sets on different pages.

Jeff

A cursor will maintain the order it was created with until it is disposed of.

It won't work with a web app though as each page will come from a different connection in the available pool (or created for each page) meaning you will loose the cursor between pages.

I would think you want to look at having a sort column that has a random number in it that is used for sorting.

mysortcol integer default random()

or maybe update the column a couple of times a day to keep the variety you seem to be after.


----- Original Message -----
From: Andreas Kretschmer <akretschmer(at)spamfence(dot)net>
To: pgsql-sql(at)postgresql(dot)org
Sent: Wednesday, November 29, 2006 12:27:42 PM GMT-0500 US/Eastern
Subject: Re: [SQL] consistent random order

Jeff Herrin <jeff(at)alternateimage(dot)com> schrieb:

I am returning results ordered randomly using 'order by random()'. My issue has
to do with page numbers in our web application. When I hit the 2nd page and
retrieve results with an offset, ordering by random() isn't really what I want
since I will often receive results that were on the 1st page (they get re-
randomized).

I'm looking for a way to order in a controled random order. Maybe a UDF.

I think you are searching for CURSORs.

18:25 < akretschmer> ??cursor
18:25 < rtfm_please> For information about cursor
18:25 < rtfm_please> see http://www.postgresql.org/docs/current/static/plpgsql-cursors.html
18:25 < rtfm_please> or http://www.postgresql.org/docs/current/static/sql-declare.html

With a CURSOR, you get one result-set and can walk through this result.


Andreas


--

Shane Ambler
pgSQL(at)007Marketing(dot)com

Get Sheeky @ http://Sheeky.Biz



Home | Main Index | Thread Index

Privacy Policy | PostgreSQL Archives hosted by Command Prompt, Inc. | Designed by tinysofa
Copyright © 1996 – 2008 PostgreSQL Global Development Group