Doctrine findBy()内存不足错误 [英] Doctrine findBy() out of memory error

查看:161
本文介绍了Doctrine findBy()内存不足错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含大约300k行描述苹果推送通知服务的设备的表格。我使用Doctrine 2作为一个ORM。

I have a table containing roughly 300k rows describing devices for Apple's Push Notification service. I use Doctrine 2 as an ORM.

插入设备是没有问题的,但检索它们是一个完全不同的故事。使用简单的MySQL SELECT 我可以在几秒钟内得到它们,其中WiFi是主要的瓶颈。但是,如果我尝试通过Doctrine获取它们,即使我允许PHP高达1 GB,也会耗尽内存。我已经根据文档创建了教条实体的getter和setter和protected属性。

Inserting devices is no problem, however, retrieving them is a whole different story. Using a simple MySQL SELECT I can get them in a few seconds, where the WiFi is the main bottleneck. However, if I try to fetch them via Doctrine, it runs out of memory even if I allow PHP up to 1 gigabyte. I have created getters and setters and protected properties for the Doctrine entities, as per the documentation.

我不知道我做错了什么。这很好:

I have no clue what I'm doing wrong. This is fine:

$devices = mysql_query("SELECT * FROM `Devices` WHERE `deviceProperty`='someValue'");

$message = new Message();
while($device = mysql_fetch_array($devices))
{
    $message->addRecipient($device['pushToken']);
}

但是第一行的内存不足(从来没有达到断点在下一行):

but this runs out of memory on the first line (it never reaches a breakpoint on the next line):

$devices = self::$entityManager->getRepository('Device')->findBy(array("deviceProperty" => "someValue"));
$message = new Message();
foreach($devices as $device)
{
    $message->addRecipient($device->getPushToken);
}


推荐答案

对象,它会消耗太多的内存,尝试处理大块...

You're pulling in 300k objects, it'll consume way too much memory, try processing in chunks...

$message = new Message();

$limit = 50;
$offset = 0;
while($devices = self::$entityManager->getRepository('Device')->findBy(array("deviceProperty" => "someValue"), array(), $limit, $offset))
{
   foreach($devices as $device)
   {
       $message->addRecipient($device->getPushToken);
   }
   $offset += $limit;
}

这篇关于Doctrine findBy()内存不足错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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