arogachev / yii2-excel

ActiveRecord import and export based on PHPExcel for Yii 2 framework
Other
64 stars 25 forks source link

Performance #34

Open makroxyz opened 8 years ago

makroxyz commented 8 years ago

Import a CSV file of about 3,500 rows takes more than 5 minutes, and a quite load of memory. Why don't you fill a single model and then validate/save at the same time? In this way you don't have to keep in memory a bunch of ARs I mean:

/**
 * @inheritdoc
 */
protected function safeRun()
{
    parent::safeRun();
    $this->fillModels($this->_phpExcel->getActiveSheet()->getRowIterator());
    Yii::$app->db->transaction(function () {
        foreach ($this->_models as $model) {
            $model->load();
            $model->save();
        }
    });
    $this->trigger(self::EVENT_RUN);
}

to

Yii::$app->db->transaction(function () {
    $rows = $this->_phpExcel->getActiveSheet()->getRowIterator();
    $c = 1;
    foreach ($rows as $row) {
        if (PHPExcelHelper::isRowEmpty($row)) {
            break;
        }
        if ($c == 1) {
            if (!$this->_standardModels[0]->parseAttributeNames($row)) {
                throw new RowException($row, 'Attribute names must be placed in first filled row.');
            }
        } else {
            $model = new Model([
                'row' => $row,
                'standardModel' => $this->_standardModels[0],
            ]);
        $model->load();
        $model->save();
    }
        $c++;
    }
});