mysql rand() is slow

Discussion in 'PHP' started by olddocks, Jul 24, 2007.

  1. #1
    anybody knows a good workaround for rand() function with mysql as it desperately slows down the performance on large datasets.

    I found some kind of solution here, still looking for much better one.
     
    olddocks, Jul 24, 2007 IP
  2. HuggyCT2

    HuggyCT2 Guest

    Messages:
    222
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    What are the results you are showing?

    You can just use php and make a limit on the query, something like:

    
    $total = mysql_num_rows($your_query_here);
    $num1 = rand(0,$total);
    $num2 = rand(0, $total);
    $result = mysql_query("SELECT * FROM `table` LIMIT ".$num1.",".$num2;
    
    Code (markup):
    Havent tested but I am sure it will work :)
     
    HuggyCT2, Jul 24, 2007 IP