【发布时间】:2013-07-01 13:45:15
【问题描述】:
我有一些 PHP 代码,一旦按下下一个箭头,它基本上只会获取数据库中的下一个图像。然而,在我的网站上,我有时可以让 600 多个在线用户全部点击下一步。有什么办法可以优化这个 php 代码以更快地执行?
谢谢!
$nxtImage = mysql_query ("SELECT * FROM images WHERE active=1 and id>$id and section=$section ORDER BY id ASC LIMIT 1") or die (mysql_error());
$nxtrow = mysql_fetch_array($nxtImage);
$nxtnum = mysql_num_rows($nxtImage);
$nid = $nxtrow['id'];
$npicTitle = $nxtrow['img_title'];
$nimgLink = preg_replace("![^a-z0-9]+!i", "-", $npicTitle);
$nimgLink = strtolower($nimgLink);
【问题讨论】:
-
如果
id是某种索引,如主键,它已经排序,所以你不必告诉它排序任何东西。删除此ORDER BY id ASC可能已经提高了此查询的性能。 -
使用 Memcached 之类的东西来缓存查询结果怎么样?
-
尽管 mysql* 函数在速度上具有边际优势,但您应该切换到 mysqli*,尤其是在 $id 变量声明如下:$id = $_GET['id']...跨度>
-
你的表结构是什么? IE。
DESC images;