Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is php script slowing down?

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.

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.

like image 787
Tomáš Tibenský Avatar asked Mar 19 '14 09:03

Tomáš Tibenský


People also ask

How long can a PHP script run?

By default, the maximum execution time for PHP scripts is set to 30 seconds. If a script runs for longer than 30 seconds, PHP stops the script and reports an error. You can control the amount of time PHP allows scripts to run by changing the max_execution_time directive in your php. ini file.

Do PHP comments affect performance?

But as for execution speed, it has no influence, because they are just ignored by the interpreter. So basically, it does not matter if you add comments.

Why is my PHP slow?

Long processing of database queries is often the cause of slow pages loading. Often the reason for this is incorrectness of queries writing, lack of competent tables indexing, bulky and too complex requests and etc.


Video Answer


1 Answers

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!

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();
like image 124
Tomáš Tibenský Avatar answered Oct 09 '22 01:10

Tomáš Tibenský