laravel wherehas has 查询非常慢 怎么优化喃?

select * from users where exists (select * from posts where users.id=posts.user_id and start_at >= ‘2022-09-09’)

users表非常大于100万 posts表非常小于1000条

使用 wherehas has 查询非常慢 怎么优化喃? 使用的是下面的这个模型查询

User::query()->whereHas('posts',function ($query){
            $query->where('start_at','>=','2022-09-09');
        })->get();
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
最佳答案
whereIn('id', Post::select(['user_id']);
1年前 评论
tiantian10000 (楼主) 1年前
Imuyu (作者) 1年前
讨论数量: 10

尝试修改为id in(select )试试

1年前 评论
tiantian10000 (楼主) 1年前
select * from posts p left join user u on p.user_id = u.id
1年前 评论
tiantian10000 (楼主) 1年前
whereIn('id', Post::select(['user_id']);
1年前 评论
tiantian10000 (楼主) 1年前
Imuyu (作者) 1年前

in 适用于左边大表,右边小表 exists 适用于左边小表,右边大表。

1年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!