как оптимизировать запрос, который выполняется в цикле foreach - PullRequest
0 голосов
/ 25 февраля 2019

Я написал запрос, но он занимает 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();

}
...