Reputation: 3731
Right now I'm selecting 3 random rows from a table using ORDER BY Rand();
as pointed out all over the web this method is very slow. I am using it on a database with only 30 rows and it takes a long time to return the value. Most of the other solutions i've found only return one row. What's the best way to return multiple random rows?
$get_projects_query =
mysql_query(
"SELECT p_id FROM project_data WHERE p_featured='1' ORDER BY Rand() LIMIT 3"
) or die(mysql_error());
while($project_row = mysql_fetch_array($get_projects_query)) {?>
//Do stuff
}
//end three random featured projects loop)
Upvotes: 0
Views: 417
Reputation: 882
As everyone else said. Having only 30 items in your query do like this:
Or your query has only for the moment 30 results but in time it will be bigger and bigger?
Upvotes: 0
Reputation: 12589
If there are only 30 rows, you have a couple other options.
Option #1
Option #2
Option #3
Upvotes: 1
Reputation: 1604
First select the table count. Order by an index column (probably the primary key). Then select limit 1 with offset = to a random integer in the range of 0 to the table count.
Upvotes: 1
Reputation: 8503
If you only have 30 rows why not download all 30 rows to your client and then generate 3 random row indexes
Upvotes: 0