优化我的随机行获取
我一直在阅读有关通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 php 中生成一个随机数并以这种方式将其钻入 PK
SQL 将是
如果你错过了,请再次运行它。这可以非常有效地查找一行,这比使用限制/偏移处理更好
Generate a random number in php and drill it that way into the PK
The SQL would be
If you miss, run it again. This gives a very efficient seek to one row which will be better than using limit/offset processing
理想的方法是,您可以在互联网上的很多地方阅读,例如 Anton Titov 的博客 是执行 2 个查询:
然后用您的编程语言生成一个随机数(即 PHP 使用 mt_rand)。并将其输入到此查询中:
注意:如果您的表只有少量行,则这不适合使用。一般来说,我仍然使用 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:
Then generate a random number in your programming language (i.e. PHP use mt_rand). and feed it into this Query:
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.
不要使用第一个
SELECT
,而是使用这个:除非您每隔几秒添加和删除新的横幅,否则请考虑将此结果缓存在某处。
Instead of the first
SELECT
, use this:Unless you add and remove new banners every couple of seconds, consider caching this result somewhere.