Re: Selecting a random row

From: Kari Lavikka <tuner(at)bdb(dot)fi>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Selecting a random row
Date: 2004-11-04 13:34:33
Message-ID: Pine.HPX.4.51.0411041526160.3138@purple.bdb.fi
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


Works but is too slooow. Shuffling whole table and selecting the first
row is not the way to go in this case.

Limit (cost=5340.74..5340.74 rows=1 width=4)
-> Sort (cost=5340.74..5440.70 rows=39986 width=4)
Sort Key: random()
-> Seq Scan on users (cost=0.00..2284.37 rows=39986 width=4)
Filter: (status = 'a'::bpchar)

|\__/|
( oo ) Kari Lavikka - tuner(at)bdb(dot)fi
__ooO( )Ooo_______ _____ ___ _ _ _ _ _ _ _
""

On Thu, 4 Nov 2004, Holger Klawitter wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On Thursday 04 November 2004 12:36, Kari Lavikka wrote:
> > Is there any explanation for this strange behavior or are there better
> > ways to select a random row?
>
> How about
>
> SELECT ...whatever... ORDER BY random() LIMIT 1;
>
> Mit freundlichem Gruß / With kind regards
> Holger Klawitter
> - --
> lists <at> klawitter <dot> de
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.2.2 (GNU/Linux)
>
> iD8DBQFBiibF1Xdt0HKSwgYRAlJXAJ4nUpDfKBKCigPVMt8WpKG4gZmt4wCcD/ZC
> KHBlBl1+5FZ4pgqkZlyzWQA=
> =MrrE
> -----END PGP SIGNATURE-----
>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to majordomo(at)postgresql(dot)org
>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Joe Maldonado 2004-11-04 13:51:35 VACUUMING questions...
Previous Message Martin Foster 2004-11-04 13:17:22 Re: Restricting Postgres