Advertisement

TrimwebsolutionsTrimwebsolutions

Problem and Solutions

Query Optimization, changing the queries in the loop into a single processing query in SQL ?

By M.K. Verma · 4 May 2022

Query Optimization, changing the queries in the loop into a single processing query in SQL ?

Try this:

$applicantIds = \DB::select()->from( 'applicants' )->where( 'created_at', 0 )->execute();
$applicantApps = \DB::select( 'applicant_id', 'created_at' )->from( 'applicant_apps' )->where( 'applicant_id', 'in', $applicantIds)->execute();

$statement = '';
foreach ($applicantApps as $applicantApp) 
{
   $applicantId = $applicantApp['applicant_id'];
   $applicantCreatedAt = $applicantApp['created_at'];
   $statement .= "update applicants set created_at='$applicantCreatedAt' where id = '$applicantId';"
}

if ($statement) {
   \DB::query($statement)->execute();
}

Two queries only will get you this.