运行数千条记录时,PHP 内存耗尽 [英] PHP memory exhausted when running through thousands of records

查看:57
本文介绍了运行数千条记录时,PHP 内存耗尽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对一组 5,000 个结果运行以下代码.由于内存耗尽而失败.

I'm running the following code over a set of 5,000 results. It's failing due to the memory being exhausted.

foreach ($data as $key => $report) {
  $data[$key]['data'] = unserialize($report['serialized_values']);
}

我知道我可以提高内存限制,但我希望可以毫无问题地运行它.我无法永远保持记忆.

I know I can up the memory limit, but I'd like to run this without a problem instead. I'm not going to be able to keep upping the memory forever.

编辑

$data 格式如下:

[1] => Array
    (
        [0] => 127654619178790249
        [report_id] => 127654619178790249
        [1] => 1
        [user_id] => 1
        [2] => 2010-12-31 19:43:24
        [sent_on] => 2010-12-31 19:43:24
        [3] => 
        [fax_trans_id] => 
        [4] => 1234567890
        [fax_to_nums] => 1234567890
        [5] => ' long html string here',
        [html_content] => 'long html string here',
        [6] => 'serialization_string_here',
        [serialized_values] => 'serialization_string_here',
        [7] => 70
        [id] => 70
    )

推荐答案

除了 for 和 foreach 的问题之外,您还需要重新构建您的解决方案.您正在达到内存限制,因为您合法地使用了过多的内存.每次反序列化数据库列的内容并存入数组

Beyond the problems of for and foreach, you need to re-architect your solution. You're hitting memory limits because you're legitimately using too much memory. Each time you unserialize the contents of the database column and store it in an array

$data[$key]['data']

PHP 需要留出一块内存来存储这些数据,以便以后可以访问.当您的数组变得太大时,您的内存不足.用简单的英语,你是在告诉 PHP

PHP needs to set aside a chunk of memory to store that data so it can be accessed later. When your array gets too big you're out of memory. In plain english, you're telling PHP

取所有 5000 行数据并将它们存储在内存中,稍后我将对其进行处理.

Take all 5000 rows of data and store them in memory, I'm going to do something with them later.

您需要想出不同的方法来解决您的问题.以下项目是对这个问题的两个快速想法.

You need to think of a different way to approach your problem. The below items are two quick thoughts on the problem.

您无法将项目存储在内存中,而只能在循环中执行您想要的任何操作,从而允许 php 根据需要丢弃项目

You could not store the items in memory and just take whatever actions you wanted to in the loop, allowing php to discard the items as need be

foreach ($data as $key => $report) {
    $object = unserialize($report['serialized_values']);        
    //do stuff with $object here
}

你也可以只从未序列化的对象中存储你需要的信息,而不是存储整个对象

You could also only store the information you need from the unserialized object, rather than storing the entire object

foreach ($data as $key => $report) {
    $object             = unserialize($report['serialized_values']);        
    $data               = array();
    $data['foo']        = $object->foo;
    $data[$key]['data'] = $data;
}

长话短说:您达到了内存限制,因为您实际上使用了太多内存.这里没有神奇的解决方案.存储序列化数据并尝试将其全部加载到单个程序中是一种内存密集型方法,与语言/平台无关.

Long story short: you're hitting memory limits because you're actually using too much memory. There's no magic solution here. Storing serialized data and attempting to load it all in a single program is a memory intensive approach, irrespective of language/platform.

这篇关于运行数千条记录时,PHP 内存耗尽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆