简体   繁体   English

在PHP中加快While循环

[英]Speed Up While loop In php

right the problem is a smallish one but hope someone can help. 没错,这个问题很小,但希望有人能提供帮助。 Basically we use memcache to cache some of our mysql queries and then store the results in an array for memcache. 基本上,我们使用memcache来缓存一些mysql查询,然后将结果存储在用于memcache的数组中。 Then when the query runs again it grabs the array results from memcache and processes the results like normal. 然后,当查询再次运行时,它将从内存缓存中获取数组结果,并像平常一样处理结果。 So the code looks like this. 所以代码看起来像这样。

$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 basically just either runs the query or returns the array from memcache. mysql_query_run基本上只是运行查询或从内存缓存返回数组。 The mysql_mem_fetch_array either processes the results from mysql or transverses the array. mysql_mem_fetch_array要么处理来自mysql的结果,要么横穿该数组。

The transversing part uses this code. 横向部分使用此代码。

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;

        }

The problem is the current and next commands - in large arrays (which are very rare) it causes some hanging. 问题是当前命令和下一个命令-在大型数组中(这非常少见),它会导致挂起。 Currently we are in php version 5.1.6 and we are going to 5.3 will the problem be solved? 当前我们在php版本5.1.6中,并且我们将在5.3中解决该问题? Or is there a better way to handle the array? 还是有更好的方法来处理数组? Thanks for your help. 谢谢你的帮助。 Richard 理查德

in large arrays (which are very rare) it causes some hanging. 在大阵列中(这非常罕见),它会导致一些挂起。

Easy. 简单。 Just avoid storing large arrays in memcache. 只是要避免将大型数组存储在内存缓存中。

If you do: 如果您这样做:

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

...is it any better? ...更好吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM