【问题标题】:Speed Up While loop In php在php中加速While循环
【发布时间】:2013-02-14 21:32:01
【问题描述】:

是的,问题很小,但希望有人能提供帮助。 基本上,我们使用 memcache 来缓存我们的一些 mysql 查询,然后将结果存储在 memcache 的数组中。然后,当查询再次运行时,它会从 memcache 中获取数组结果并像平常一样处理结果。 所以代码看起来像这样。

$query = "SELECT x,y,z FROM a WHERE b = 3";
$result = mysql_query_run($query,'memcache_name');
while($row = mysql_mem_fetch_array($result,'memcache_name')) {

 //do processing

}

mysql_query_run 基本上只是运行查询或从内存缓存返回数组。 mysql_mem_fetch_array 要么处理来自 mysql 的结果,要么遍历数组。

横断部分使用此码。

if(is_array($result)) {
            $return = current($result);
            //get the current result - based on the internal pointer
            next($result);//increment pointed
            return $return;//return result
        }
        else {

            //mysql result tab
            //so get the array from the mysql_fetch_array
            $array = mysql_fetch_array($result);

            if(is_array($MEMCACHE_SERVER_RESULTS[$memcache])==false) {
                //if there is no results then just set the results as array
                $MEMCACHE_SERVER_RESULTS[$memcache] = array();
            }
            //push the array onto the end of the current array - from memcache

            //if there are some results then push them on
            if($single_row == 1) {
                //only one row so just combine the 2 steps and store
                array_push($MEMCACHE_SERVER_RESULTS[$memcache],$array);
                $MEMCACHE_SERVER->set($memcache, $MEMCACHE_SERVER_RESULTS[$memcache],0,$memcache_time);
            }
            else if($array!==false) {

                array_push($MEMCACHE_SERVER_RESULTS[$memcache],$array);
                //and set it
            }
            else {
                //set the memcache to the array that it has been making.
                $MEMCACHE_SERVER->set($memcache, $MEMCACHE_SERVER_RESULTS[$memcache],0,$memcache_time);
            }

            //return array
            return $array;

        }

问题在于当前和下一个命令 - 在大型数组中(非常罕见),它会导致一些挂起。 目前我们在 php 版本 5.1.6 并且我们将要到 5.3 会解决问题吗? 还是有更好的方法来处理数组? 谢谢你的帮助。 理查德

【问题讨论】:

    标签: php while-loop memcached each


    【解决方案1】:

    在大型数组(非常罕见)中,它会导致一些挂起。

    简单。只是避免在内存缓存中存储大型数组。

    【讨论】:

    • 老实说,这是一个很好的答案——我可能会考虑这样做。谢谢! - 只是采取不同的观点! (感觉脸被鱼打了!)
    • 你知道,memcache 已经是这样一个数组,正是为此目的而制作的。并以惊人的速度访问它的元素。为什么要通过存储大量数据并将其前后移动来使其变慢 - 这让我感到困惑。
    【解决方案2】:

    如果你这样做:

    if (is_array($result)) {
      $return = each($result); //get the current result and advance the array pointer
      return $return['value']; //return result
    } else // ...and so on
    

    ...好点了吗?

    【讨论】:

    • ...这有什么问题?如果您有充分的理由,请务必对我投反对票,但请有礼貌地解释原因...
    • 对不起,我没有给你投反对票——我不知道是谁投的。作为回答,虽然我已经尝试过了,但 webgrind 软件说不 - 奇怪的是它更慢!不过感谢您将其添加到组合中。如果有什么对人们了解完整故事有用的地方,请点赞!
    • @RichardHousham PHP 可能很奇怪,当一个函数完成两个的工作时,你会认为它会更快,但有时它不是......我猜它与内部有关实施。我认为 Col.Shrapnels 答案可能是 the 答案,如果您正在执行返回非常大的结果集的查询,您可能不应该缓存它们,因为结果(更多)可能会改变在查询之间,它会吃掉内存。我想说的一件事 - 你可以通过使用 mysql_fetch_assocmysql_fetch_row 而不是 mysql_fetch_array 来提高效率。
    • 您从字面上理解了这个问题,但却错过了它的含义。让您知道,关于“如何迭代数组”的所有这些愚蠢的东西与性能无关。只有没有一点实践经验的菜鸟才会相信语法结构可能会影响任何事情。
    • @Col.Shrapnel 好吧,你这么说,但请尝试计算foreachwhile (list($var1,$var2) = each()) 之间的差异。我知道除非你有大量的科目或大量的科目,而且premature optimisation is the root of all evil,否则这并没有什么不同,但是当人们问关于效率的问题时,他们通常想要的是关于效率的答案,无论差异有多小练习...
    猜你喜欢
    • 2017-12-09
    • 2016-11-27
    • 1970-01-01
    • 1970-01-01
    • 2012-05-31
    • 1970-01-01
    • 2012-05-08
    • 2014-10-19
    • 2013-06-12
    相关资源
    最近更新 更多