Может кто-нибудь помочь мне оптимизировать этот запрос в laravel это занимает слишком много времени ....
$arDiagnosisData = $this->connectionObj->table("diagnosis")
->select(
array(DB::raw("ABS(TIMESTAMPDIFF(MINUTE, '$vrCreatedAt', created_at)) AS timeDifference"),
'id', 'uid', 'name','notes','assessment','isItLocked','startDate','discontinuedOnDateTime','created_at','updated_at',
'updatedAtTimezone','createdAtTimezone','createdByUid'
))
->whereIn('id',function($query) use($userId){
$query->select(DB::raw('MAX(diagnosis.id)'))
->from('diagnosis AS diagnosis')
->where('diagnosis.uid',$userId)
->groupBy('diagnosis.firstParentId');
})
->where(function ($query) use ($dateOfPlanToShowOn) {
$query->where(DB::raw('CAST(discontinuedOnDateTime AS DATE)'),'>',$dateOfPlanToShowOn);
$query->orWhere('discontinuedOnDateTime',NULL);
})
->whereNull('deletedOnDateTime')
->whereNotNull('name')
->count();