查询优化,将循环中的查询更改为单个处理查询

发布于 2025-01-26 01:15:30 字数 525 浏览 1 评论 0原文

如何优化这些查询是一两个?循环中的一个至少是一个。

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

foreach ($applicantIds as $applicantId) 
{
    $applicantApp = \DB::select( 'applicant_id', 'created_at' )->from( 'applicant_apps' )->where( 'applicant_id', $applicantId['id'] )->execute();

    \DB::update( 'applicants' )->value( 'created_at', $applicantApp[0]['created_at'] )->where( 'id', $applicantApp[0]['applicant_id'] )->execute();
        
}

How do I optimize these queries to be one or two? The one in the loop to be one at least.

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

foreach ($applicantIds as $applicantId) 
{
    $applicantApp = \DB::select( 'applicant_id', 'created_at' )->from( 'applicant_apps' )->where( 'applicant_id', $applicantId['id'] )->execute();

    \DB::update( 'applicants' )->value( 'created_at', $applicantApp[0]['created_at'] )->where( 'id', $applicantApp[0]['applicant_id'] )->execute();
        
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

離殇 2025-02-02 01:15:30

尝试以下操作:

$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();
}

两个查询只会为您带来这个问题。

如果需要,您可以在foreach块中添加一些逻辑。

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.

And you can add some logic in foreach block if you need.

<逆流佳人身旁 2025-02-02 01:15:30

II understand you, you can use singe query for this:

update applicants a
join applicant_apps aa on aa.applicant_id = a.id
set a.created_at = aa.created_at
where a.created_at = 0;

When you want to use Laravel syntax:

$db::table('applicants as a')
     ->where( $db::raw("`a`.`created_at`"), 0)
     ->join('applicant_apps as aa', 'aa.applicant_id', '=', 'a.id')
     ->update([ 'a.created_at' => $db::raw("`aa`.`created_at`") ])
     ;

I I understand you, you can use singe query for this:

update applicants a
join applicant_apps aa on aa.applicant_id = a.id
set a.created_at = aa.created_at
where a.created_at = 0;

When you want to use Laravel syntax:

$db::table('applicants as a')
     ->where( $db::raw("`a`.`created_at`"), 0)
     ->join('applicant_apps as aa', 'aa.applicant_id', '=', 'a.id')
     ->update([ 'a.created_at' => $db::raw("`aa`.`created_at`") ])
     ;

Online test

过度放纵 2025-02-02 01:15:30

@faesal带有您的答案,我将其更改为有一些数组作为ID

$applicantIds = \DB::select('id')->from( 'applicants' )->where( 'created_at', 0 )->execute()->as_array('id');

$applicantApps = \DB::select( 'applicant_id', 'created_at' )->from( 'applicant_apps' )->where( 'applicant_id', 'in', array_keys($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';";
}

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

@faesal with your answer I changed it to have some arrays as ID's

$applicantIds = \DB::select('id')->from( 'applicants' )->where( 'created_at', 0 )->execute()->as_array('id');

$applicantApps = \DB::select( 'applicant_id', 'created_at' )->from( 'applicant_apps' )->where( 'applicant_id', 'in', array_keys($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';";
}

\DB::query($statement)->execute();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文