Answer the question
In order to leave comments, you need to log in
PHP/Yii2: how to speed up the execution of ~1 million requests in a row?
Hello.
A large array of data is obtained from a third-party server through the API, which needs to be scattered across different tables. In total - one million with something of records. It is obviously impossible to try to solve the problem "on the forehead":
foreach ($array as $attributes) {
$model = new Model();
$model->setAttributes($attributes);
$model->save();
}
Answer the question
In order to leave comments, you need to log in
2. Writing all data to each table in one query. How? Transaction?
I see 2 options:
1. LOAD DATA - the link has already been given above.
2. insert many rows at once:
Example:
dev.mysql.com/doc/refman/5.6/en/insert.html
+ The best thing is to tune the server so that it is ready for such a volume (a million lines - that doesn't say anything)
but how exactly does the server fail? (a million lines - that doesn't say anything)
You can also merge everything into a file, and then import via the console.
1) www.yiiframework.com/doc-2.0/yii-db-command.html#b...
2) I have seen this trick
file_put_contents(
'/path/to/some/insert/file.sql', // куда
'INSERT INTO table VALUES', // формируем запрос вставки с экранированными данными
FILE_APPEND // константа, которая указывает, что добавляем в конец файла
)
https://dev.mysql.com/doc/refman/5.6/en/load-data.html
The standard way to load large volumes.
"The server can't cope" - most likely there is not enough memory, break it into pieces that fit into memory / allocate more of it if necessary through Yii
I propose such a simple implementation of a buffer that batch writes to the database every n rows: https://gist.github.com/marcuzy/3bff7ab350ac6741def0
There was a similar problem in an environment with a very weak server, they fought like this - they collected all one text, and then they loaded everything via LOAD DATA. There was an even more crutch solution through a CSV file and exec.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question