繁体   English   中英

Laravel Eloquent 使查询执行变慢

[英]Laravel Eloquent Making Query Execution Slow

我正在使用 laravel eloquent 关系并且查询执行速度慢,假设我有两个表聊天( 100 万条记录
潜在客户( 50 万条记录
我想查询使用群组的聊天和潜在客户数据

 lead::select("chat_lead_id")->with([chats=>function($q){
    $q->select('chat_id',"group_id")->where("group_id"=>1)
    })

查询调试第一个查询:

result =select chat_lead_id from leads 

第二次查询

select chat_id,group_id from chats  where group_id =1 AND chat_id in (result)

请检查上面的调试,您可以看到 First Query将从数据库中获取 50 万行,这将需要很长时间,所以我应该怎么做才能加入查询? 使用 WhereHas 时,它会放置需要时间的内部查询,也使用 WhereHas 查询

select * from `gc_od_leads`
    where exists (select * from `gc_od_chat`
            where `gc_od_leads`.`leads_chat_id`
                = `gc_od_chat`.`chat_id`
              and `chat_group_id` = ?)

因为你有 50 万行,我认为你应该加入你的表:

 $values=lead::query()->join('chats','leads.chat_lead_id','=','chats.chat_id')
            ->where("group_id",=,1)->select(["chat_lead_id",'chat_id',"group_id"])->get();
where group_id =1 AND chat_id in (result)

需要这个复合索引: INDEX(group_id, chat_id)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM