Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 batchInsert eats all server memory

If I insert million rows in 100 batch inserts than every iteration memory_usage getting bigger and then php memory error occurs. It is connected with Yii insert command, because if I comment insert operation memory_usage are stable.

for ($i = 0; $i < $iterations; $i ++) {
    ...
    Yii::$app->db->createCommand()
        ->batchInsert(static::tableName(), $columns, $rows)
        ->execute();
    echo memory_get_usage();
}

I've tried do disable debug mode and it's not helped.

like image 486
user1561346 Avatar asked Mar 21 '26 15:03

user1561346


1 Answers

I think that problem is in logger of Yii2. Just try to use something like that:

common/components/EmptyLogger.php:

<?php
namespace common\components;

use yii\log\Logger;

class EmptyLogger extends Logger
{
    public function log($message, $level, $category = 'application')
    {
        return false;
    }
}

and then in your action of your controller include next code at the beginning:

Yii::setLogger(new EmptyLogger());

of course also add it in your uses:

use common\components\EmptyLogger;

after all you will receive something like that:

console\controllers\TempController.php:

<?php

namespace console\controllers;

use common\components\EmptyLogger;
use Yii;
use yii\console\Controller;

class TempController extends Controller
{
    public function actionIndex()
    {
        Yii::setLogger(new EmptyLogger());
        ...

        Yii::$app->db->createCommand()
            ->batchInsert(static::tableName(), $columns, $rows)
            ->execute();
        ...

    }
}

Hope it helps. But actually it's not the best solution. Just a hack..

like image 185
Ivan Pomortsev Avatar answered Mar 24 '26 18:03

Ivan Pomortsev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!