Hi, Great site you have here. My question regards the use of the "order by RAND()", particularly in MySQL. There is often the need for websites to display items on their pages in a random order to give the visitor the sense of "always new stuff", etc. However, on a table with 10 thousand rows, ordering by rand means that the mysql server must read the entire table and because it's not using an index it's quite painfull. Is there a way to use index and still getting the items ordered by a random function? asked Jan 31 '11 at 12:01 myke123 |
Hi Myke, That's an exciting question. The short answer is: No, However, there might be another way to get a random result without instead of
try to select a random ID that is indexed and (approximately) evenly distributed:
Note the following:
answered Jan 31 '11 at 13:08 Markus Winand ♦♦ |
Hi Markus, Thank you so much for replying to my question and for giving me the example model, so detailed and well-explained. Usually people want some sort of example to be provided by the one who asks the question and I know I did not gave one but you went the extra mile and provided your example, so tnx. It is indeed pitty the solution cannot be used to obtain more records in one shot, but it is a great starting point. I will try your example and make some tests to see what happens. answered Feb 01 '11 at 07:25 myke123 |