pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: rj@last.fm
Cc: PgSQL Performance ML <pgsql-performance@postgresql.org>
Subject: Re: Selecting random rows efficiently
Date: Sat, 30 Aug 2003 11:14:04 -0400
Message-ID: <25757.1062256444@sss.pgh.pa.us> (raw)
In-Reply-To: <23400.1062254862@sss.pgh.pa.us>
References: <Pine.BSF.4.44.0308300905390.829-100000@torgo.978.org>
	<200308301425.51127.rj@last.fm>
	<23400.1062254862@sss.pgh.pa.us>

I said:
> 3. Your query now looks like
> 	SELECT * FROM table WHERE random_id >= random()
> 	ORDER BY random_id LIMIT 1;

Correction: the above won't give quite the right query because random()
is marked as a volatile function.  You can hide the random() call inside
a user-defined function that you (misleadingly) mark stable, or you can
just stick it into a sub-select:

regression=# explain select * from foo WHERE random_id >= (select random())
regression-# ORDER BY random_id LIMIT 1;
                               QUERY PLAN
-------------------------------------------------------------------------
 Limit  (cost=0.01..0.15 rows=1 width=8)
   InitPlan
     ->  Result  (cost=0.00..0.01 rows=1 width=0)
   ->  Index Scan using fooi on foo  (cost=0.00..45.50 rows=334 width=8)
         Index Cond: (random_id >= $0)
(5 rows)

This technique is probably safer against future planner changes,
however:

regression=# create function oneshot_random() returns float8 as
regression-# 'select random()' language sql stable;
CREATE FUNCTION
regression=# explain select * from foo WHERE random_id >= oneshot_random()
regression-# ORDER BY random_id LIMIT 1;
                               QUERY PLAN
-------------------------------------------------------------------------
 Limit  (cost=0.00..0.14 rows=1 width=8)
   ->  Index Scan using fooi on foo  (cost=0.00..46.33 rows=334 width=8)
         Index Cond: (random_id >= oneshot_random())
(3 rows)

The point here is that an indexscan boundary condition has to use stable
or immutable functions.  By marking oneshot_random() stable, you
essentially say that it's okay to evaluate it only once per query,
rather than once at each row.

			regards, tom lane



view thread (10+ messages)  latest in thread

Message-ID: <25757.1062256444@sss.pgh.pa.us>
Permalink:  ../25757.1062256444@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/25757.1062256444@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-performance@postgresql.org
  Cc: tgl@sss.pgh.pa.us, rj@last.fm
  Subject: Re: Selecting random rows efficiently
  In-Reply-To: <25757.1062256444@sss.pgh.pa.us>
  References: <Pine.BSF.4.44.0308300905390.829-100000@torgo.978.org> <200308301425.51127.rj@last.fm> <23400.1062254862@sss.pgh.pa.us> <25757.1062256444@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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