如何优化在foreach循环中运行的查询

时间:2019-02-25 08:03:12

标签: php sql optimization query-optimization laravel-5.6

我已编写查询,但是这需要10秒钟的时间,因此,我想优化代码。我的代码是:

foreach($arrActiveJobsList as $user_id => $arrData) {
    $userId = [$user_id];
    $objCollectJobs = collect($arrData);
    $arrJobs = $objCollectJobs->groupBy('status')->toArray();
    $JobSubmissionQry = \App\Models\Ats\JobSubmission::query();
    $JobSubmissionQry->where([['job_submissions.record_status','=',0], 
    ['job_submissions.submission_status','>',0]])->where('jobs.record_status','=',0)->whereIn('job_submissions.submitted_by',$userId)->Where('job_submissions.job_id','>',0)->WhereBetween(DB::raw('CAST(submission_at as date)'),[$startDate,$endDate]);

    $JobSubmissionQry->leftJoin('hire.jobs', function($join) {
    $join->on('jobs.id', '=', 'job_submissions.job_id');
  });              
   $total_sub_query = clone($JobSubmissionQry);             
   $total_sub_query->where(function ($total_sub_query)  {
    $total_sub_query->WhereNull('jobs.client')->orWhere('jobs.client','=','')->orWhere('jobs.client','=','0');
});
   $nInternalSubmissions = $total_sub_query->count();
  $total_user_submission_query = clone($JobSubmissionQry);                      
  $no_of_application_jobs = $total_user_submission_query->select('job_id','expected_pay')->get();
  $resume_sourced = \App\Models\Ats\Candidate::where([['record_status',0],['is_resource_candidate',0]])->whereIn('created_by',$userId)->WhereBetween(DB::raw('CAST(created_at as date)'),[$startDate,$endDate])->count();
     $nTotalInterivews = \App\Models\Ats\Interview::whereIn('interviews.created_by',$userId)->where('job_id','>',0)->WhereBetween(DB::raw('CAST(interview_date as date)'),[$startDate,$endDate])->count();

}

0 个答案:

没有答案