为什么php脚本速度变慢? [英] Why is php script slowing down?

查看:219
本文介绍了为什么php脚本速度变慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究迁移脚本,该脚本从一个MySQL数据库中选择数据,然后通过理论将其导入到另一个MySQL数据库中.问题是每创建一个实体后,我的脚本就会变慢.

I was working on migration scripts which selects data from one MySQL database and import through doctrine into another MySQL database. The problem was that after every chunk of created entities, my scripts slowed down.

前100篇文章大约需要5秒钟才能导入,接下来的100篇文章需要7秒钟,接下来的10秒钟,依此类推.这确实是一个大问题,因为我需要导入约1.500.000文章.

first 100 articles takes about 5 seconds to import, next 100 articles takes 7 seconds, next 10 seconds and so on. It is really big problem, because I need to import about 1.500.000 articles.

推荐答案

我发现php> = 5.3具有垃圾收集清理程序.因此,当我导入大量文章时,我会调用 gc_collect_cycles(); 清除所有实体中的内存,不再需要该脚本.该脚本不再放慢速度!

I found out that php >=5.3 has garbage collector cleaner. So, when I import chunk of articles I call gc_collect_cycles(); to clear memory out of all entities which script will needs no more. The script is slowing down no more!

如果使用的是框架,请检查其是否具有自己的缓存系统. 如果您使用的是教义,请关闭SQL记录器

If you are using a framework, check if it has its own cache system. If you are using doctrine turn off SQL logger

/** @var $em EntityManager */
$em = $this->getContainer()->get('doctrine')->getEntityManager();
$em->getConnection()->getConfiguration()->setSQLLogger(null);

,然后在导入每个块之后清除学说缓存

and then clear doctrine cache after every chunk is imported

$em->clear();

这篇关于为什么php脚本速度变慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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