Re: Random tuple?

From: Josh Berkus <josh(at)agliodbs(dot)com>
To: Philip Hallstrom <philip(at)adhesivemedia(dot)com>
Cc: Brendan LeFebvre <brendanl(at)iname(dot)com>, "Pgsql-Novice(at)Postgresql(dot) Org" <pgsql-novice(at)postgresql(dot)org>
Subject: Re: Random tuple?
Date: 2002-10-18 00:08:34
Message-ID: 200210171708.34539.josh@agliodbs.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice


Phil,

> Question... does the above query have to "look" at every row in tablea,
> assign each a random number, sort it, and then return just one result?

Yeah. It's resource-intensive if you have, say, 8 million rows.

> If so, would it make more sense to setup a periodic process (via cron say)
> to do extract 10-20 products into a featured_product table and query that?
>
> Just thinking in terms of the amount of pounding the DB would take...

Well, if you have a large table, the logical approach would be a PL/pgSQL
function that selects a random row based on the number of rows in the table *
random, and then picks a row based on LIMIT and OFFSET. This would be
somewhat less resource-intensive for a large table, but has the disadvantage
that it would have to be re-run for each random row you wanted to retrieve.

--
-Josh Berkus
Aglio Database Solutions
San Francisco

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Tom Lane 2002-10-18 01:39:16 Re: information
Previous Message Josh Berkus 2002-10-17 23:13:42 Re: Random tuple?