I am using the function below to randomly select from a database and print the results using a template. It works fine when the records are under 1,000. Howver, I am using 100k records. Even though it works, the load time is very slow and it is heavily tasking my server. How can I rewrite this code to excute the task more efficiently with minimum stress on myserver. also the default value of $descript = "3". I want to use of a range of 3 to 5 as the default value. Any help will be appreciated. function description ($descript = "3") { include "config.php"; mysql_connect("$mysql_server", "$mysql_user","$mysql_pass"); $getlinks="SELECT * from $words_table order by RAND() limit $descript "; $getlinks2=mysql_query($getlinks) or die("Could not get links"); while ($getlinks3=mysql_fetch_array($getlinks2)) { $desc = $getlinks3[$words_table_name]." "; print $desc; } } Code (markup): this is the code i use in the config file: $words_table = "words"; $words_table_name = "text"; Code (markup): this is the code i use to output the results in the template <?php description (7);?> Code (markup): I will really appreciate your help. Thanks!!
If you pre-filter using a where clause based on an indexed field, you may be able to speed up the results and also maintain some randomness, see suggestion below: function description ($descript = "3") { include "config.php"; mysql_connect("$mysql_server", "$mysql_user","$mysql_pass"); $initial= substr("abcdefghijklmnopqrstuvwxyz",0, rand(0,25)); $getlinks="SELECT * from $words_table where text like '". $initial . "%' order by RAND() limit $descript "; $getlinks2=mysql_query($getlinks) or die("Could not get links"); while ($getlinks3=mysql_fetch_array($getlinks2)) { $desc = $getlinks3[$words_table_name]." "; print $desc; } } Code (markup):
How about this: function description ($descript = "3") { include "config.php"; mysql_connect("$mysql_server", "$mysql_user","$mysql_pass"); $num = mysql_num_rows("SELECT * FROM $words_table"); $rand_num = rand(0, $num); $rand_end = $rand_num + $descript; while($rand_end > $num) --$rand_end; $getlinks="SELECT * FROM $words_table LIMIT $rand_num, $rand_end"; $getlinks2=mysql_query($getlinks) or die("Could not get links"); while ($getlinks3=mysql_fetch_array($getlinks2)) { $desc = $getlinks3[$words_table_name]." "; print $desc; } } PHP: I haven't tested it anyway.
Ah, yeah. My fault. Here is the fixed one: function description ($descript = "3") { include "config.php"; mysql_connect("$mysql_server", "$mysql_user","$mysql_pass"); $num = mysql_num_rows(mysql_query("SELECT * FROM $words_table")); //my fault $rand_num = rand(0, $num); $rand_end = $rand_num + $descript; while($rand_end > $num) --$rand_end; $getlinks="SELECT * FROM $words_table LIMIT $rand_num, $rand_end"; $getlinks2=mysql_query($getlinks) or die("Could not get links"); while ($getlinks3=mysql_fetch_array($getlinks2)) { $desc = $getlinks3[$words_table_name]." "; print $desc; } } PHP:
thanks a lot. this works in an interesting way. it completely neglects <?php description (1);?> and randomly loads up the entire table with 23k entries. but its much faster. i will like to regulate the output number with <?php description (1);?> any more ideas?