Re: ORDER BY RAND() Too Slow! Alternatives?

2001-02-10 Thread Stephen Waits
"Jeffrey D. Wheelhouse" wrote: > > Here's another approach. I'm curious about the performance implications: > > SELECT @lines:=COUNT(id) FROM table; > SET @rand=CEILING(RAND()*@lines); > SELECT * FROM table WHERE (@rand:=@rand-1)+id=id; > > This *should* give each row an equal chance, but it'

Re: ORDER BY RAND() Too Slow! Alternatives?

2001-02-10 Thread Stephen Waits
"Jeffrey D. Wheelhouse" wrote: > > SELECT @lines:=COUNT(id) FROM table; > SET @rand=CEILING(RAND()*@lines); > SELECT * FROM table WHERE (@rand:=@rand-1)+id=id; Never mind on the "it doesn't work on my system" more like it didn't work on my brain :) Works fine. And now that I ponder it a bit

ORDER BY RAND() Too Slow! Alternatives?

2001-02-10 Thread Stephen Waits
Hi there, In the quest to get a random row from a table, "order by rand()" has proven too inefficient and slow. It's slow because MySQL apparently selects ALL rows into memory, then randomly shuffles ALL of them, then gives you the first one - very inefficient. There are a few other ways I've