从excel文件Laravel 5更快地插入数据库记录

时间:2022-12-20 15:38:29

I am making a module which you upload records on a database from an excel file. Those are just phone numbers. So here's my code:

我正在制作一个模块,您可以从excel文件上传数据库中的记录。这些只是电话号码。所以这是我的代码:

        $file = Input::file('file');
        Excel::load($file, function($reader) {
            // Getting all results
            $results = $reader->get()->toArray();
            //var_dump($results);exit;
            foreach ($results as $key => $value) {
                $phone = new Phone();
                $phone->msisdn          =  $value['msisdn'];
                $phone->save();
            }
        });

I'm using https://github.com/Maatwebsite/Laravel-Excel to read the excel file. It works fine, 20,000 records uploads in 20mins I guess, is there a way to to it or upload it faster? I know that it depends also in the server but is there other factors? I'm using MySQL

我正在使用https://github.com/Maatwebsite/Laravel-Excel来读取excel文件。它工作正常,20分钟上传20,000条记录,我想,有没有办法可以上传或上传速度更快?我知道这也取决于服务器,但还有其他因素吗?我正在使用MySQL

Thanks

谢谢

2 个解决方案

#1


0  

From their documentation possibly chunk the results as you are using such a large file. That way you can read in chunks of the file rather than loading the whole file into memory.

从他们的文档中可能会在使用如此大的文件时将结果分块。这样你就可以读取文件的块而不是将整个文件加载到内存中。

http://www.maatwebsite.nl/laravel-excel/docs/import#chunk

http://www.maatwebsite.nl/laravel-excel/docs/import#chunk

#2


0  

You could try this faster alternative (https://github.com/rap2hpoutre/fast-excel):

您可以尝试这种更快的替代方案(https://github.com/rap2hpoutre/fast-excel):

(new FastExcel)->import($file, function ($line) {
    $phone = new Phone();
    $phone->msisdn = $value['msisdn'];
    $phone->save();
});

#1


0  

From their documentation possibly chunk the results as you are using such a large file. That way you can read in chunks of the file rather than loading the whole file into memory.

从他们的文档中可能会在使用如此大的文件时将结果分块。这样你就可以读取文件的块而不是将整个文件加载到内存中。

http://www.maatwebsite.nl/laravel-excel/docs/import#chunk

http://www.maatwebsite.nl/laravel-excel/docs/import#chunk

#2


0  

You could try this faster alternative (https://github.com/rap2hpoutre/fast-excel):

您可以尝试这种更快的替代方案(https://github.com/rap2hpoutre/fast-excel):

(new FastExcel)->import($file, function ($line) {
    $phone = new Phone();
    $phone->msisdn = $value['msisdn'];
    $phone->save();
});