rand()的订单处理大型帖子时遇到麻烦 [英] order by rand() gets trouble for large posts
本文介绍了rand()的订单处理大型帖子时遇到麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这段代码,这段代码很麻烦,要花很多时间才能显示来自DB的RANDOM帖子:
I have this code, this code gets trouble and so much time to be executed to show RANDOM posts from DB:
$totalrows = 10;
$sql = "SELECT
posts.Tags as tags,
posts.OwnerUserId as postsid,
posts.Id as postid,
posts.Body as body,
posts.Title as title,
users.Id as userid,
users.DisplayName as usersname
FROM posts
JOIN users ON posts.OwnerUserId = users.Id
WHERE posts.Title != '' order by rand() asc limit " . $totalrows;
$r = mysql_query($sql) or die(mysql_error());
请告诉我我应该更改些什么以更快地显示随机帖子?
Please tell me what should I change to show random posts more quikly?
问候,丹
推荐答案
rand() is very expensive. This will help: http://www.titov.net/2005/09/21/do-not-use-order-by-rand-or-how-to-get-random-rows-from-table/
这篇关于rand()的订单处理大型帖子时遇到麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文