View Single Post
Old 2008-08-22, 02:20 PM   #8
cd34
a.k.a. Sparky
 
cd34's Avatar
 
Join Date: Sep 2004
Location: West Palm Beach, FL, USA
Posts: 2,396
no question that it is better to do order by rand() limit 10 than to take 2300 results and do a shuffle/array_slice. Its also better do select the 2-3 fields you need rather than select * if you're going to do an order by rand() since mysql will have to create a temporary table to contain the results before doing the order. If you had to do an order by rand() on 2300 results, its possible a subquery would handle the results a little faster if structured properly. I don't know as I haven't tested it, but, knowing how the query optimizer works with indexing, I can see some potential for it being a little quicker.

It also depends on how often you do that query. Does the page need to be generated each time the page is loaded? I've seen linklists that generate the page on every pageview where the results only change once a day or after reviews are done. Its all about handing the page to the surfer as quickly as possible, and, each time you do an SQL query, you slow down page generation a fraction and have the potential for losing your customer.

order by rand() just has so much potential for abuse -- that's why I dislike it.
__________________
SnapReplay.com a different way to share photos - iPhone & Android
cd34 is offline   Reply With Quote