Get 50k rows faster with subqueries – Laravel 5.6

The below query is to get the candidate’s details from the table which has 50k rows. Including jobs, regions, and employment types. A candidate has basic details with employment type, jobs, regions are in another table with foreign key relation.

$candidates =  DB::table('candidates')
->join('role_users', function($join) use($datas) {
$join->on('candidates.user_id', '=', 'role_users.user_id');
$join->where('role_users.role_id', 6);

if (isset($datas->login_time) && $datas->login_time != "") {
$type = $datas->login_time;
$dateRanges = getDateRanges($datas->login_time);
if ($type == 1) {
} else if ($type == 6) {
$join->whereDate('role_users.login_at', '<', $dateRanges['start']);
} else if ($type != 1 && $type != 6) {
$join->whereBetween('role_users.login_at', [$dateRanges['start'], $dateRanges['end']]);
->join('candidate_statuses', 'candidates.candidate_status_id', '=', '')
->join('employment_types', 'candidates.employment_types_id', '=', '')
'candidates.full_name as name',
'employment_types.title AS employment_type',
DB::raw("(SELECT GROUP_CONCAT(candidate_jobs.job_id SEPARATOR ',') FROM candidate_jobs WHERE candidate_jobs.candidate_id = as job_ids"),
DB::raw("(SELECT GROUP_CONCAT( SEPARATOR ',') FROM candidate_regions INNER JOIN regions ON WHERE candidate_regions.candidate_id = as regions"),
->where(function ($query) use($datas, $request, $regCandidates, $jobCandidates, $status){

if ($datas->employment_types) {
$query->whereIn('candidates.employment_types_id', $datas->employment_types);
if ($status) {
$query->whereIn('candidates.candidate_status_id', $status);
if ($datas->access === 1 || $datas->access=== 0) {
$access = strval($datas->access);
$query->where('candidates.has_access', $access);
if ($datas->candidates) {
$query->whereIn('', $datas->candidates);
if ($regCandidates) {
$query->whereIn('', $regCandidates);
if ($jobCandidates) {
$query->whereIn('', $jobCandidates);
if ($request->search) {
$query->where('candidates.full_name', 'like', "%{$request->search}%");
$query->orWhere('', 'like', "%{$request->search}%");
$query->orWhere('candidates.post_code', 'like', "%{$request->search}%");
$query->orWhere('candidates.candidate_code', 'like', "%{$request->search}%");
$query->orWhere('candidates.formatted_mobile_number', 'like', "%{$request->search}%");
->where('candidates.candidate_status_id', '!=' , 6)
->where('candidates.is_deleted', $request->is_deleted)

if (count($candidates) > 0) {
$candidates->map(function ($candidate) {
$job_ids = $candidate->job_ids != null
? explode(',', $candidate->job_ids)
: [];
$candidate->jobs = [];
$candidate->has_access = $candidate->has_access == 1 ? true : false;
$candidate->login_at = $candidate->login_at ? date('d-m-Y h:i A', strtotime($candidate->login_at)) : 'Never logged in';
$jobs = DB::table('jobs')->whereIn('id', $job_ids);
$candidate->jobs = $jobs->get(['name', 'color']);
$candidate->csvjobs = $jobs->pluck('name')->all();
$candidate->regions = $candidate->regions != null
? explode(',', $candidate->regions)
: [];
return $candidate;

It takes 2/ 3 seconds to get the result in my local machine but in production takes too long time. Can anyone please help?

from Newest questions tagged laravel-5 – Stack Overflow

Related Posts

Codeigniter : Parse error: syntax error, unexpected ‘const’ (T_CONST), expecting variable (T_VARIABLE) in Laravel project

I’m getting following error: **Parse error: syntax error, unexpected ‘const’ (T_CONST), expecting variable (T_VARIABLE)** Note : It’s working in local but facing issue in production server. private…

Firebase receive notification while tab is active or on focus

What i want is to be able to perform an action when a user receives a notification while the browser is open and tab is active or…

Laravel’s alias loader does not find class

We have a legacy project that we cannot update and we need to make some changes in symfony’s Response.php in vendor. We have solved this by copying…

Laravel 5 – generic document management

I have a system where you can create different types of unique documents. For instance, one document is called Project Identified and this expects certain inputs. Originally,…

Laravel Nova limit the results in indexQuery

I ran intro a situation where I need to limit the results of a resource to only 3 results. To be more specific, based on the logged…

Auditoria en laravel 5.8 [closed]

Cómo puedo automatizar el registro de actividades de un usuario en laravel? Si un usuario ingresa a un app de laravel, debo guardar toda su actividas, a…

Leave a Reply

Your email address will not be published.