【发布时间】:2018-10-19 06:41:59
【问题描述】:
我正在尝试上传包含大约 10,000 条记录的 .csv 或 .xls 文件。对于第一次尝试,下面的函数/代码读取 csv 文件并能够在大约 2 分钟内将大约 1600 行/记录插入到 transactions 表中, 没有代码行:set_time_limit( 0); 我得到了:最大执行超时错误
第二次尝试,现在使用代码:set_time_limit(0);这一次,在大约 11 分钟内插入了 10,000 条记录。 >
我现在的问题是:
1.如何快速读取 csv/xls 文件并将其 >= 10,000 的记录/行插入 Laravel 应用程序的数据库中?
2.实践,'set_time_limit(0)',我这里用过,好用吗?有没有更好的方法可以解决这个问题?
3.在读取和插入发生之前,我是否可以确定完成操作/执行所需的时间/最长时间?
public function importDataSet(Request $request)
{
if(Input::hasFile('file')){
$file = Input::file('file');
$uname = uniqid();
$oname = $file->getClientOriginalName();
$filename = $uname.$oname;
//create a folder, data-sets, if not exist in the public folder
$filelocation = 'data-sets';
$filepath=$file->move($filelocation, $filename);
ini_set('auto_detect_line_endings', TRUE);
$header = '';
$rows = array_map('str_getcsv', file($filepath));
$header = array_shift($rows);
$csv = array();
set_time_limit(0);//reset maximum execution time to infinity for this function
foreach ($rows as $row) {
$csv[] = $row;//Is not used
//eturn $rows;
$transaction = new Transaction;
$transaction->dataset_id = 1;
$transaction->itemsets=json_encode($row);
$transaction->save();
}
}else{
//noting here
}
return 'Success';
}
【问题讨论】:
-
不知道具体是如何工作的,但这需要数据队列。
-
你应该试着把它变成一个可排队的工作,并返回
202作为请求本身的状态码
标签: laravel csv import upload xls