目錄
需求
導出幾十萬左右的數據導excel表
問題
- 使用phpexcel等插件,碰到數據量大很慢,可能能花半個小時以上
- 數據量大查詢慢
- 內存不足
- 執行超時
解決
- 使用原生csv導出
- 設置腳本超時和內存,進行加大內存,不限制超時時間
- 進行分頁查詢
/**
* 分批導出
* @param Request $request
*/
public function bpOut(Request $request){
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
ini_set("memory_limit", "2048M");
set_time_limit(0);
$lot = $request->get('lot');
$type = $request->get('type');
if($type == 'increment'){
$filename ="訂單日增量表格_".$lot.".csv";
$count = DB::table('orders_temporary')
->where('lot', $lot)
->Where(function ($query) {
$query->where(function ($query){
$query->where('status', 3);
})->orWhere(function ($query) {
$query->where('status', 2);
});
})->distinct('order_sn')->orderBy('id','asc')->count();
}else{
$filename ="上傳結果表格_".$lot.".csv";
$count = $orders = DB::table('orders_temporary')->where('lot', $lot)->count();
}
$limit = 10000;//限制查詢
$page =ceil($count/$limit);//頁數
header('Content-Description: File Transfer');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="'. $filename .'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
$fp = fopen('php://output', 'a');//打開output流
ob_clean();
$title = ['訂單編號', '旺旺ID', '用戶暱稱', '手機號', 'id號', '信息', '原因'];
mb_convert_variables('GBK', 'UTF-8', $title);
fputcsv($fp, $title);
for ($i=0; $i<$page; $i++){
$offset = $i*$limit;
if($type == 'increment'){
$dbs = DB::table('orders_temporary')
->where('lot', $lot)
->Where(function ($query) {
$query->where(function ($query){
$query->where('status', 3);
})->orWhere(function ($query) {
$query->where('status', 2);
});
})->distinct('order_sn')->offset($offset)->orderBy('id','asc')->limit($limit)->get()->toArray();
}else{
$dbs = DB::table('orders_temporary')->where('lot', $lot)->offset($offset)->orderBy('id','asc')->limit($limit)->get()->toArray();
}
foreach ($dbs as $db){
$db->order_sn= $this->numToString($db->order_sn);
$db->wangwang_id = $db->wangwang_id;
$db->username = $db->username;
$db->phone = $db->phone;
$db->id_number = $this->numToString($db->id_number);
$db->status_messgae = $db->status_messgae;
$db->message = $db->message;
$data=[$db->order_sn,$db->wangwang_id,$db->username,$db->phone,$db->id_number,$db->status_messgae,$db->message];
mb_convert_variables('GBK', 'UTF-8', $data);
fputcsv($fp, $data);
}
unset($dbs);
}
ob_flush();
flush();
fclose($fp);
exit();
}