将Excel导入到MySQL中花费的时间太长

时间:2018-08-08 22:08:57

标签: php mysql laravel

因此,我有一个表格,允许用户上传excel表格,然后系统会将excel数据导入到mySQL中。

但是,每次我使用AJAX提交表单时,它都会启动该过程,保存大约一半的数据,然后出现504网关错误。我已经将PHP配置超时更改为300,但仍能完成一半。我认为行数少于1000的Excel工作表不应花费5分钟以上的时间吗?

这是我的代码:

 public function postImportGroup(Request $request)
{

    if($request->hasFile('import_numbers')) {
       $file = $request->file('import_numbers');
       $file_extension = Input::file('import_numbers')->getClientOriginalExtension();
       $supportedExt = array('csv', 'xls', 'xlsx');

    if (!in_array_r($file_extension, $supportedExt)) {
        return response()->json([
            'status' => 'error',
            'msg' => 'Please make sure that the uploaded file is a valid CSV, XLS, XLSX sheet.',
        ]);
    }

   }

    $results = Excel::load($file)->get();

    $results = json_decode($results[0], true);

    $class = new DailyGroup();
    $class->title = $request->group_name;
    $class->user_id = Auth::guard('client')->user()->id;
    $class->entries = count($results);
    $class->save();

    foreach ($results as $r => $value) {
      //$data = array_values($value);
        //return $value["employee_number"];
      $group = new DailyGroupLocations();

      $address = $value["address"] . ',' . $value["city"] . ',' . $value["state"] . ',' . $value["zip"];
      $c = $value["country"];

      $file_contents = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($address) . '&components=country:' . urlencode($c) .'&sensor=false&key=xxx');
      $json_decode = json_decode($file_contents);


      if (isset($json_decode->results[0])) {  
        $group->lat = $json_decode->results[0]->geometry->location->lat;
        $group->lng = $json_decode->results[0]->geometry->location->lng;
    }
      $phone = preg_replace('/\D+/', '', $value["ph"]);
      $phone = '1'.$phone;

      $group->user_id = Auth::guard('client')->user()->id;
      $group->group_id = $class->id;
      $group->employee_number = $value["employee_number"];
      $group->work_date = $value["work_date"]["date"];
      $group->first_name = $value["name"];
      $group->last_name = $value["lastname"];
      $group->phone = $phone;
      $group->email = $value["email"];
      $group->job_number = $value["job_number"];
      $group->address = $value["address"];
      $group->city = $value["city"];
      $group->state = $value["state"];
      $group->zip = $value["zip"];
      $group->country = $value["country"];
      $group->job_name = $value["job_name"];
      $group->location = $value["location"];
      $group->shift_description = $value["shift_description"];
      $group->shift_start = $value["shift_start_time"];
      $group->shift_end = $value["shift_end_time"];
      $group->post_hours = $value["post_hours"];
      $group->save();
}

        return response()->json([
            'status' => 'success',
            'msg' => 'All data uploaded successfully. Please wait for tables to refresh.',
            //'url' => '/user/location/location-areas'
        ]);
}

我可以做些什么来优化它吗?我在foreach语句中遇到了很多事情吗?我可以使用任何提示或技巧吗?

1 个答案:

答案 0 :(得分:2)

它不是excel文件花时间...它的google地理编码调用,阻止了您的代码执行。

您可以给自己一个Google api密钥,以加快流程。

参考:https://developers.google.com/maps/documentation/javascript/get-api-key

您还应该检查地理编码呼叫的响应状态

$status = json_decode->results[0]->status;

可能的状态值:

  • 确定
  • ZERO_RESULTS
  • OVER_QUERY_LIMIT
  • INVALID_REQUEST
  • UNKNOWN_ERROR

如果可能的话,您可以考虑对数据集进行预地理编码,将lat和lng值与地址一起存储,这样,如果您期望或需要快速执行,就不必进行动态地理编码。