优化我的随机行获取

发布于 2024-12-22 12:58:41 字数 1083 浏览 2 评论 0原文

我一直在阅读有关通过 MYSQL 和 PHP 获取随机行的速度问题,并且想知道如何改进我的代码以提高速度。

我的数据库中有一个名为“banners”的表中的横幅信息,我想显示与页面上的位置相关的随机横幅,并为该横幅的 view_count 添加 +1。我的方法有效,但是对于每个页面加载时都会发生这种情况的繁忙网站,可以提高速度吗?谢谢

/* Get banners for position 1 then choose a random one to display  */
$banners = mysql_query("SELECT id,title,banner_url,destination FROM ".TBL_BANNERS." WHERE position = '1' AND status = '1'");
$banner_count = mysql_num_rows($banners) - 1;
$rand_offset = mt_rand(0,$banner_count);
$result = mysql_query("SELECT id,title,banner_url,destination FROM ".TBL_BANNERS." LIMIT $rand_offset, 1 "); 

$banner_id = mysql_result($result,0,"id");
$banner_title = mysql_result($result,0,"title");
$banner_url = mysql_result($result,0,"banner_url");
$banner_dest = mysql_result($result,0,"destination");

/* Add view to this banner */
$database->addViewToBanner($banner_id);

最后一个函数使用查询:

    "UPDATE banners SET view_count = view_count+1 WHERE id = '$banner_id'"

我还需要说的是,“banners”表中任何时候都可能不会超过 100 条记录,但 ID 中会有漏洞。 ID 可能会增加到 200 个,但其中只有一半仍然存在。

I have been reading about speed issues in relation to getting a random row via MYSQL and PHP, and wondered how my code might be improved for speed.

I have banner information in my database in a table called "banners" and i want to display a random banner in relation to the position on the page, and add +1 to the view_count for that banner. My method works, but for a busy site where this happens on each page load, can this be improved for speed? Thanks

/* Get banners for position 1 then choose a random one to display  */
$banners = mysql_query("SELECT id,title,banner_url,destination FROM ".TBL_BANNERS." WHERE position = '1' AND status = '1'");
$banner_count = mysql_num_rows($banners) - 1;
$rand_offset = mt_rand(0,$banner_count);
$result = mysql_query("SELECT id,title,banner_url,destination FROM ".TBL_BANNERS." LIMIT $rand_offset, 1 "); 

$banner_id = mysql_result($result,0,"id");
$banner_title = mysql_result($result,0,"title");
$banner_url = mysql_result($result,0,"banner_url");
$banner_dest = mysql_result($result,0,"destination");

/* Add view to this banner */
$database->addViewToBanner($banner_id);

The last function uses the query:

    "UPDATE banners SET view_count = view_count+1 WHERE id = '$banner_id'"

I also need to say, that there probably wont be any more than 100 records in the "banners" table at any one time but there will be holes in the ID. The IDs might go up to say 200 but only half of those will still exist.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

别念他 2024-12-29 12:58:41

在 php 中生成一个随机数并以这种方式将其钻入 PK

SQL 将是

SELECT id,title,banner_url,destination 
FROM TBL_BANNERS 
WHERE id = $rand_offset

如果你错过了,请再次运行它。这可以非常有效地查找一行,这比使用限制/偏移处理更好

Generate a random number in php and drill it that way into the PK

The SQL would be

SELECT id,title,banner_url,destination 
FROM TBL_BANNERS 
WHERE id = $rand_offset

If you miss, run it again. This gives a very efficient seek to one row which will be better than using limit/offset processing

月下客 2024-12-29 12:58:41

理想的方法是,您可以在互联网上的很多地方阅读,例如 Anton Titov 的博客 是执行 2 个查询:

SELECT COUNT(*) AS banners FROM quotes

然后用您的编程语言生成一个随机数(即 PHP 使用 mt_rand)。并将其输入到此查询中:

SELECT * FROM banners LIMIT $generated_number, 1

注意:如果您的表只有少量行,则这不适合使用。一般来说,我仍然使用 ORDER BY RAND() 直到我知道表中的行数将超过 100 行。

The ideal way to do it, as you can read in lots of places around the internet, e.g. Anton Titov's blog is to do 2 queries:

SELECT COUNT(*) AS banners FROM quotes

Then generate a random number in your programming language (i.e. PHP use mt_rand). and feed it into this Query:

SELECT * FROM banners LIMIT $generated_number, 1

NOTE: this isn't good to use if your table only has a small number of rows. Generally I still use ORDER BY RAND() up until I know there are going to be more than 100 rows in the table.

浅忆 2024-12-29 12:58:41

不要使用第一个SELECT,而是使用这个:

$banners_count_result = mysql_query("SELECT COUNT(*) AS num_banners FROM ".TBL_BANNERS." WHERE position = '1' AND status = '1'");
$banner_count = mysql_result($banners_count_result, 0, "num_banners");

除非您每隔几秒添加和删除新的横幅,否则请考虑将此结果缓存在某处。

Instead of the first SELECT, use this:

$banners_count_result = mysql_query("SELECT COUNT(*) AS num_banners FROM ".TBL_BANNERS." WHERE position = '1' AND status = '1'");
$banner_count = mysql_result($banners_count_result, 0, "num_banners");

Unless you add and remove new banners every couple of seconds, consider caching this result somewhere.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文