- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
_batchSize = 1000; $entTar-6ren">
我需要在 Doctrine2 中处理大量的 INSERT 操作,这里所说的大量是指大约 13 万条或更多的记录。这是我的代码的样子:
$this->_batchSize = 1000;
$entTarget = $this->_em->getRepository('PDOneBundle:Target')->findAll();
$currTargetArr = [];
foreach ($entTarget as $currTarget) {
$currTargetArr[] = $currTarget->getVeevaAccountId();
}
// $currTargetArr => is empty because there is nothing on that table
echo count($finalOutput);
for ($i = 0; $i < count($finalOutput); $i++) {
echo count($finalOutput[$i]['records']), "\n";
}
for ($i = 0; $i < count($finalOutput); $i++) {
for ($j = 0; $j < count($finalOutput[$i]['records']); $j++) {
$veevaAccountId = $finalOutput[$i]['records'][$j]['Id'];
// if there is no territory, then we add
if ($veevaAccountId !== null && !in_array($veevaAccountId, $currTargetArr, true)) {
// we set the values from veeva
if (
$finalOutput[$i]['records'][$j]['Id'] !== null &&
$finalOutput[$i]['records'][$j]['FirstName'] !== null &&
$finalOutput[$i]['records'][$j]['LastName'] !== null
) {
$newTarget = new Entity\Target();
$newTarget->setVeevaAccountId($finalOutput[$i]['records'][$j]['Id']);
$newTarget->setNpi($finalOutput[$i]['records'][$j]['NPI_vod__c']);
$newTarget->setFirst(ucfirst(strtolower($finalOutput[$i]['records'][$j]['FirstName'])));
$newTarget->setLast(ucfirst(strtolower($finalOutput[$i]['records'][$j]['LastName'])));
$newTarget->setTitle($finalOutput[$i]['records'][$j]['Title__c']);
$newTarget->setDisplayName(
ucfirst(strtolower($finalOutput[$i]['records'][$j]['FirstName'])).' '.ucfirst(
strtolower($finalOutput[$i]['records'][$j]['LastName'])
)
);
$newTarget->setLastSyncAt(new \DateTime());
$this->_em->persist($newTarget);
if (($i % $this->_batchSize) === 0) {
echo 'Flushing batch...'."\n";
echo 'Memory: '.$this->getReadableSize(memory_get_usage())."\n";
$this->_em->flush();
$this->_em->clear(); // Detaches all objects from Doctrine!
echo 'After batch...'."\n";
echo 'Memory: '.$this->getReadableSize(memory_get_usage())."\n";
}
}
$targetArr[] = $newTarget->getId();
$targetFailArr[] = $finalOutput[$i]['records'][$j]['FirstName'].' '.$finalOutput[$i]['records'][$j]['LastName'];
} else {
$entTarget = $this->_em->getRepository('PDOneBundle:Target')->find($veevaAccountId);
$lastModifiedDate = new \DateTime(
$finalOutput[$i]['records'][$j]['LastModifiedDate']
);
if ($lastModifiedDate > $entTarget->getUpdatedAt()) {
// obtained a territory, we update its data
// $entTarget->setName($finalOutput[$i]['records'][$j]['Name']);
}
$targetArr[] = $entTarget->getId();
}
}
}
$this->_em->flush();
$this->_em->clear();
当达到 2000 条记录时脚本停止执行并失败并显示此消息:
Fatal error: Allowed memory size of 2147483648 bytes exhausted (tried to allocate 18 bytes) in /var/www/html/reptooln_admin/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php on line 542
Call Stack:
0.0001 229280 1. {main}() /var/www/html/reptooln_admin/app/console:0
0.0152 3246832 2. Symfony\Component\Console\Application->run() /var/www/html/reptooln_admin/app/console:27
0.0167 3449664 3. Symfony\Bundle\FrameworkBundle\Console\Application->doRun() /var/www/html/reptooln_admin/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:126
1.8824 54241840 4. Symfony\Component\Console\Shell->run() /var/www/html/reptooln_admin/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php:91
14.7210 54278272 5. Symfony\Component\Console\Application->run() /var/www/html/reptooln_admin/vendor/symfony/symfony/src/Symfony/Component/Console/Shell.php:112
14.7211 54278480 6. Symfony\Bundle\FrameworkBundle\Console\Application->doRun() /var/www/html/reptooln_admin/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:126
14.7213 54278736 7. Symfony\Component\Console\Application->doRun() /var/www/html/reptooln_admin/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php:96
14.7214 54279656 8. Symfony\Component\Console\Application->doRunCommand() /var/www/html/reptooln_admin/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:195
14.7248 54502144 9. Symfony\Component\Console\Command\Command->run() /var/www/html/reptooln_admin/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:886
14.7250 54507096 10. PDI\PDOneBundle\Command\PDOneSyncCommand->execute() /var/www/html/reptooln_admin/vendor/symfony/symfony/src/Symfony/Component/Console/Command/Command.php:259
14.7251 54508672 11. PDI\PDOneBundle\Service\SyncController->syncVeevaData() /var/www/html/reptooln_admin/src/PDI/PDOneBundle/Command/PDOneSyncCommand.php:29
15.8199 54512624 12. PDI\PDOneBundle\Service\SyncController->syncTargets() /var/www/html/reptooln_admin/src/PDI/PDOneBundle/Service/SyncController.php:95
269.3835 719438056 13. EntityManager55881a77c7652_546a8d27f194334ee012bfe64f629947b07e4919\__CG__\Doctrine\ORM\EntityManager->flush() /var/www/html/reptooln_admin/src/PDI/PDOneBundle/Service/SyncController.php:486
269.3835 719438104 14. Doctrine\ORM\EntityManager->flush() /var/www/html/reptooln_admin/app/cache/dev/jms_diextra/doctrine/EntityManager_55881a77c7652.php:305
269.3835 719438104 15. Doctrine\ORM\UnitOfWork->commit() /var/www/html/reptooln_admin/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:340
269.3835 719438104 16. Doctrine\ORM\UnitOfWork->computeChangeSets() /var/www/html/reptooln_admin/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:297
269.3835 719438104 17. Doctrine\ORM\UnitOfWork->computeScheduleInsertsChangeSets() /var/www/html/reptooln_admin/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:711
299.1056 2146967320 18. Doctrine\ORM\UnitOfWork->computeChangeSet() /var/www/html/reptooln_admin/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:404
299.1056 2146970736 19. ReflectionProperty->getValue() /var/www/html/reptooln_admin/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:542
我将其作为 Symfony2 任务执行,因此它使用 CLI 而不是 Web,我将其设置为 2G 作为最大内存:
php -d memory_limit=2G app/console --shell
我已经阅读了几个文档(1、2、3、4、5 ...),但对我没有任何帮助,能否给我一些关于如何处理的想法这?
更新:内存消耗较少但只有 2k 插入
在写了一些调试 信息以查看我的逻辑发生了什么之后,我注意到只有前 2k 个项目被保留,而不是其余的,我找不到原因。看到这个输出:
php -d memory_limit=1G app/console pdone:sync --no-debug
iterations
------------------------------------------------------------------
0- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-2000
1- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-4000
2- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-6000
3- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-8000
4- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-10000
5- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-12000
6- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-14000
7- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-16000
8- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-18000
9- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-20000
10- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-22000
11- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-24000
12- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-26000
13- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-28000
14- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-30000
15- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-32000
16- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-34000
17- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-36000
18- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-38000
19- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-40000
20- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-42000
21- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-44000
22- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-46000
23- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-48000
24- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-50000
25- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-52000
26- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-54000
27- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-56000
28- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-58000
29- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-60000
30- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-62000
31- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-64000
32- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-66000
33- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-68000
34- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-70000
35- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-72000
36- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-74000
37- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-76000
38- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-78000
39- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-80000
40- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-82000
41- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-84000
42- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-86000
43- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-88000
44- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-90000
45- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-92000
46- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-94000
47- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-96000
48- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-98000
49- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-100000
50- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-102000
51- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-104000
52- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-106000
53- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-108000
54- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-110000
55- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-112000
56- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-114000
57- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-116000
58- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-118000
59- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-120000
60- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-122000
61- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-124000
62- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-126000
63- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-128000
64- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-130000
65- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-132000
66- instancePath: /services/data/v28.0/query/01g8000002eusZCAAY-134000
total count:
------------------------------------------------------------------
682000
count per iteration:
------------------------------------------------------------------
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
2000
1733
为什么只保留前 2k 个?每次迭代中的其余项目会怎样?
最佳答案
您显然处于循环中,您没有向我们展示。
您是否考虑过在每个循环结束时使用
释放$newTarget
使用的内存
unset($newtarget);
这应该允许垃圾收集器在您开始接近内存限制时释放内存。
如果这确实是您的问题,您可能会发现您也不需要使用如此庞大的内存分配来运行。
关于php - 如何在 Symfony2/Doctrine2 中处理大量 INSERT 以避免 "Allowed memory size of 2147483648 bytes exhausted",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30982848/
我开始从事一个用 Symfony 2.8 编写的大型项目。将整个项目升级到 SF 3 需要数百小时,现在还不可能。我想到了一个想法,将 symfony/symfony 包解压到它替换的单个包中(com
我在提交表单后使用 FOSUserEvents,但订阅者调用了两次。 这样我的验证码第一次有效第二次无效 这是我的代码 router = $router; $this->request
我有以下路线: blog_show: path: /test/123 defaults: { _controller: TotalcanBravofillBundle:Te
我是测试新手。我想测试我的功能。我已经成功安装了 phpUnit。我在互联网上查看了许多教程。但我无法获得有关测试的正确信息。这是我的功能代码: public function loginAction
我正在尝试重现 facebook batch requests 的行为在他们的图形 api 上运行。 所以我认为最简单的解决方案是在 Controller 上向我的应用程序发出几个请求,例如: pub
在 Symfony Progress Bar documentation有一个超酷酒吧的示例图像。不幸的是,看起来文档的其余部分没有解释如何获得这样的结果。 这是图片,以防您错过: 我怎么才能得到它?
我使用Finder发送假脱机电子邮件,但是自动名称生成器将点放在文件名中,有时它们出现在文件的开头。 查找程序似乎无法获取具有该名称的文件-那些文件被隐藏了……有人经历过这种行为吗?有什么建议如何使用
我正在尝试进行 LDAP 身份验证,我目前遇到此类错误: ServiceNotFoundException: The service "security.firewall.map.context.ma
有没有办法验证和检查集合数组是否为空。我已经尝试过: /** * @Assert\NotBlank() * @Assert\Length( min = 1) */ protected $work
使用Smyfony2和Doctrin2,可以使用以下示例创建数据固定装置:http://symfony.com/doc/current/bundles/DoctrineFixturesBundle/i
我看到在大多数Symfony 2示例中,例如,不存在记录时,Symfony 2会引发异常。我认为这种方法对最终用户不友好。为什么有人更喜欢引发异常而不在Flashbag上添加一些错误消息? 最佳答案
我对项目中的以下服务有疑问: app.security.guardAuthenticatorLoginPassword: class: AppBundle\Security\LoginPa
symfony缓存和登录Docker容器存在问题。 Web服务器从www-data用户和组执行,当我使用docker上安装的php从docker容器中清除symfony缓存时,它从root执行。 因此
我想了解 symfony 中的服务 我已阅读http://symfony.com/doc/2.3/book/service_container.html#creating-configuring-se
因为我对 Symfony 和 Doctrine 还很陌生,所以我有一个可能很愚蠢的问题;-) 有人可以用简单的词语向我解释集合(尤其是实体中的ArrayCollections)吗?它是什么以及何时以及
我收到了这个表格: {{ form_start(form) }} {{ form_end(form) }} 我想检查用户是否登录,我这样做了: {% if is_g
我的网站已准备好部署,我正在尝试将其设置为在线。 一些信息: 主持人是 OVH。 它不允许 SSH,我必须使用 FTP 发送文件。也没有命令行。 我现在希望能够在子目录中设置网站:/www/test(
过去几个月以来,我一直在尝试与symfony合作。昨晚我自动删除了不需要的存储库。之后,我无法使用symfony命令创建新的symfony项目。当我在终端中运行Symfony new Security
In the environnement variable, then in system variable, I edited the path and added在环境变量中,然后在系统变量
我们有一个 Symfony 1.4 应用程序,想升级到 Symfony 4。是否有可能或者我们必须重新编程该应用程序? 我们询问了我们附近的一家软件公司,他们告诉我们必须重新编写应用程序。 最佳答案
我是一名优秀的程序员,十分优秀!