Re: [PERFORM] Selecting random rows efficiently

2003-09-03 Thread scott.marlowe
Can you just create an extra serial column and make sure that one is always in order and no holes in it? (i.e. a nightly process, etc...)??? If so, then something like this truly flies: select * from accounts where aid = (select cast(floor(random()*10)+1 as int)); My times on it on a

[PERFORM] Selecting random rows efficiently

2003-08-30 Thread Richard Jones
Hi, i have a table of around 3 million rows from which i regularly (twice a second at the moment) need to select a random row from currently i'm doing order by rand() limit 1 - but i suspect this is responsible for the large load on my db server - i guess that PG is doing far too much work

Re: [PERFORM] Selecting random rows efficiently

2003-08-30 Thread Jeff
On Sat, 30 Aug 2003, Richard Jones wrote: Hi, i have a table of around 3 million rows from which i regularly (twice a second at the moment) need to select a random row from currently i'm doing order by rand() limit 1 - but i suspect this is responsible for the large load on my db server - i

Re: [PERFORM] Selecting random rows efficiently

2003-08-30 Thread Richard Jones
On Saturday 30 August 2003 1:08 pm, you wrote: On Sat, 30 Aug 2003, Richard Jones wrote: Hi, i have a table of around 3 million rows from which i regularly (twice a second at the moment) need to select a random row from currently i'm doing order by rand() limit 1 - but i suspect this is

Re: [PERFORM] Selecting random rows efficiently

2003-08-30 Thread Rod Taylor
i was hoping there was some trickery with sequences that would allow me to easily pick a random valid sequence number..? I would suggest renumbering the data. ALTER SEQUENCE ... RESTART WITH 1; UPDATE table SET pkey = DEFAULT; Of course, PostgreSQL may have trouble with that update due to

Re: [PERFORM] Selecting random rows efficiently

2003-08-30 Thread Ron Johnson
On Sat, 2003-08-30 at 08:09, Richard Jones wrote: Hi, i have a table of around 3 million rows from which i regularly (twice a second at the moment) need to select a random row from currently i'm doing order by rand() limit 1 - but i suspect this is responsible for the large load on my db

Re: [PERFORM] Selecting random rows efficiently

2003-08-30 Thread Tom Lane
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