简体   繁体   English

PHP随着时间的推移循环减速

[英]PHP While Loop Slowdown over time

I have seemingly harmless while loop that goes through the result-set of a mysql query and compares the id returned from mysql, to one in a very large multidimensional array: 我看似无害的while循环遍历mysql查询的结果集,并将mysql返回的id与一个非常大的多维数组中的id进行比较:

//mysqli query here
while($row = fetch_assoc())
{
    if(!in_array($row['id'], $multiDArray['dimensionOne']))
    {
        //do something
    }
}

When the script first executes, it is running through the results at about 2-5k per second. 当脚本首次执行时,它以大约每秒2-5k的速度运行结果。 Sometimes more, rarely less. 有时更多,很少少。 The result set brings back 7million rows, and the script peaks at 2.8GB of memory. 结果集带回700万行,脚本峰值为2.8GB内存。

In terms of big data, this is not a lot. 就大数据而言,这并不是很多。

The problem is, around the 600k mark, the loop starts to slow down, and by 800k, it is processing a few records a second. 问题是,在600k标记附近,循环开始减速,而在800k时,它正在处理一些记录。

In terms of server load and memory use, there are no issues. 在服务器负载和内存使用方面,没有问题。

This is behaviour I have noticed before in other scripts dealing with large data sets. 这是我之前在处理大型数据集的其他脚本中注意到的行为。

Is array seek time progressively slower as the internal pointer moves deeper? 当内部指针移动得更深时,阵列寻道时间是否逐渐变慢?

That really depends on what happens inside the loop. 这实际上取决于循环内部发生的事情。 I know you are convinced it's not a memory issue but it looks like one. 我知道你确信这不是一个记忆问题,但它看起来像一个。 Program usually get very slow when system tries to get extra RAM by using SWAP. 当系统试图通过使用SWAP获得额外的RAM时,程序通常会变得很慢。 Using hard drive is obviously very slow and that's what you might be experiencing. 使用硬盘驱动器显然非常慢,而这正是您可能遇到的情况。 It's very easy to benchmark it. 对它进行基准测试非常容易。

In one terminal run 在一个终端运行中

vmstat 3 100

Run you scrip and observe vmstat. 运行脚本并观察vmstat。 Look into IO and SWAP. 查看IO和SWAP。 If that is really not the case then profile execution with XDEBUG. 如果确实不是这种情况,那么使用XDEBUG进行概要执行。 It might be tricky because you do many iterations and this will also cause major IO. 它可能很棘手,因为你做了很多迭代,这也会导致主要的IO。

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

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