Yii2 batchInsert eat all server memory

If I insert millions of rows from 100 batch inserts than each iteration of memory_usage gets larger and then php memory error occurs. It is related to the Yii insert command because if I comment out the insert operation, then the memory_usage is stable.

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

      

I tried to disable debug mode and it didn't help.

+3


source to share


1 answer


I think the problem is with the Yii2 logger. Just try using something like this:

general / 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 your controller include the following code at the beginning:

Yii::setLogger(new EmptyLogger());

      

add it to your apps of course:



use common\components\EmptyLogger;

      

after all you will get something like this:

Prefixes \ 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 this helps. But this is actually not the best solution. Just a hack.

+2


source







All Articles