Jump to content

Recommended Posts

On a table containing 10,000+ records I got this level of performance, selecting and printing 5 random records

[code]$t1 = microtime(true);
$res = mysql_query("SELECT COUNT(*) FROM sales") or die(mysql_error());
echo '<pre>';
echo mysql_result($res, 0), ' records searched<br/>';

$res = mysql_query("SELECT * FROM sales ORDER BY RAND() LIMIT 5") or die(mysql_error());
while (list(,$code,$value) = mysql_fetch_row($res)) {
    printf ('%3s %8.2f<br/>', $code, $value);
}
$t2 = microtime(true);
$t = $t2-$t1;
printf ('Time taken %0.3f seconds', $t);
echo '</pre>';[/code]

Output -->

[code]10917 records searched
005    16.00
005     8.00
750    66.00
724     6.40
004   175.37
Time taken 0.018 seconds[/code]
Link to comment
https://forums.phpfreaks.com/topic/10686-random-articles/#findComment-40075
Share on other sites

Barand, I found the article:

[a href=\"http://www.greggdev.com/web/articles.php?id=6\" target=\"_blank\"]http://www.greggdev.com/web/articles.php?id=6[/a]

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]$random_row = mysql_fetch_row(mysql_query("select * from YOUR_TABLE order by rand() limit 1"));

$random_row will be an array containing the data extracted from the random row. However, when the table is large (over about 10,000 rows) this method of selecting a random row becomes increasingly slow with the size of the table and can create a great load on the server. I tested this on a table I was working that contained 2,394,968 rows. It took 717 seconds (12 minutes!) to return a random row.[/quote]
Link to comment
https://forums.phpfreaks.com/topic/10686-random-articles/#findComment-42282
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.