CodeClerks

Alternative to ORDER BY RAND()



Write the reason you're deleting this FAQ

Alternative to ORDER BY RAND()

I've found out that ORDER BY RAND() in MySQL is very slow. I found out the hard way and it wasn't a problem until I had a LOT of rows to sort through. Can anyone suggest any alternatives? Is there a faster implementation I can use? For now, I just removed the statement.

I thought about code randomizing but then I'd have to pull more results (currently only pulling 7 results). It isn't ideal and I don't think it would actually have random results, just pull the same set of results over and over again.

ideas?

Comments

Please login or sign up to leave a comment

Join
andgkj
Instead of using rand() in query use it n some procedure and get rand() numbers. Put those in an array and use that order as a order by criteria using stored proc or procedure in SQL.



Are you sure you want to delete this post?

robertman11
Use the programming language to sort the returned results it possible. This probably isn't feasible if you have millions of rows though.



Are you sure you want to delete this post?

andgkj
You can still use programming language. You have just split the records based on your memory size ;)



Are you sure you want to delete this post?