SOS Help: random database record selection

Discussion in 'PHP' started by sageman, Oct 10, 2008.

  1. #1
    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!!
     
    sageman, Oct 10, 2008 IP
  2. Kuldeep1952

    Kuldeep1952 Active Member

    Messages:
    290
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    60
    #2
    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):
     
    Kuldeep1952, Oct 10, 2008 IP
  3. sageman

    sageman Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks...
    i tried it but it didnt quite work. any other solutions?

    Thanks!
     
    sageman, Oct 10, 2008 IP
  4. djzmo

    djzmo Active Member

    Messages:
    165
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #4
    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.
     
    djzmo, Oct 10, 2008 IP
  5. sageman

    sageman Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    thanks for you help. the code looks pretty but it didnt work. :)

    anymore suggestions?
     
    sageman, Oct 10, 2008 IP
  6. djzmo

    djzmo Active Member

    Messages:
    165
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #6
    What did you mean by 'didnt work'?
    Did you see any errors?
     
    djzmo, Oct 11, 2008 IP
  7. sageman

    sageman Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    no, it does not generate any errors. it just gives you blank areas where the data is suppose to be.
     
    sageman, Oct 11, 2008 IP
  8. djzmo

    djzmo Active Member

    Messages:
    165
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #8
    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:
     
    djzmo, Oct 11, 2008 IP
  9. sageman

    sageman Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    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?:):)
     
    sageman, Oct 11, 2008 IP