whereHas性能调优——采用 where in 语法实现优化 查询关联

1,206次阅读
没有评论

LARAVEL HASIN
hasin 是一个基于 where in 语法实现的 Laravel ORM 关联关系查询的扩展包,部分业务场景下可以替代 Laravel ORM 中基于 where exists 语法实现的 has,以获取更高的性能。

Github (喜欢这个项目点个 star 吧,灰常感谢~)

简介
Laravel ORM 的关联关系非常强大,基于关联关系的查询 has 也给我们提供了诸多灵活的调用方式,然而某些情形下,has 使用了 where exists 语法实现

例如:

// User hasMany Post
User::has(‘posts’)->get();
select * from users where exists (select * from posts where users.id=posts.user_id)
exists 是对外表做 loop 循环,每次 loop 循环再对内表(子查询)进行查询,那么因为对内表的查询使用的索引(内表效率高,故可用大表),而外表有多大都需要遍历,不可避免(尽量用小表),故内表大的使用 exists,可加快效率。

但是当 A 表数据量较大的时候,就会出现性能问题,那么这时候用 where in 语法将会极大的提高性能

select * from users where users.id in (select posts.user_id from posts)
in 是把外表和内表做 hash 连接,先查询内表,再把内表结果与外表匹配,对外表使用索引(外表效率高,可用大表),而内表多大都需要查询,不可避免,故外表大的使用 in,可加快效率。

因此在代码中使用 has(hasMorph) 或者 hasIn(hasMorphIn) 应由数据体量来决定……

<?php
/**

  • SQL:
    • select * from users
  • where exists
  • (
  • select * from posts
  • where users.id = posts.user_id
  • )
  • limit 10 offset 0
    */
    $users = User::has(‘posts’)->paginate(10);

/**

  • SQL:
    • select * from users
  • where users.id in
  • (
  • select posts.user_id from posts
  • )
  • limit 10 offset 0
    */
    $users = User::hasIn(‘posts’)->paginate(10);
    Laravel ORM 十种关联关系案例 sql 输出可查看有道云笔记

环境
PHP >= 7.1
laravel >= 5.8
安装
composer require biiiiiigmonster/hasin
使用
此扩展方法 hasIn(hasMorphIn) 支持 Laravel ORM 中的所有关联关系,入参调用及内部实现流程与框架的 has(hasMorph) 完全一致,可安全使用或替换

hasIn

// hasIn
User::hasIn(‘posts’)->get();

// orHasIn
User::where(‘age’, ‘>’, 18)->orHasIn(‘posts’)->get();

// doesntHaveIn
User::doesntHaveIn(‘posts’)->get();

// orDoesntHaveIn
User::where(‘age’, ‘>’, 18)->orDoesntHaveIn(‘posts’)->get();
whereHasIn

// whereHasIn
User::whereHasIn(‘posts’, function ($query) {
$query->where(‘votes’, ‘>’, 10);
})->get();

// orWhereHasIn
User::where(‘age’, ‘>’, 18)->orWhereHasIn(‘posts’, function ($query) {
$query->where(‘votes’, ‘>’, 10);
})->get();

// whereDoesntHaveIn
User::whereDoesntHaveIn(‘posts’, function ($query) {
$query->where(‘votes’, ‘>’, 10);
})->get();

// orWhereDoesntHaveIn
User::where(‘age’, ‘>’, 18)->orWhereDoesntHaveIn(‘posts’, function ($query) {
$query->where(‘votes’, ‘>’, 10);
})->get();
hasMorphIn

Image::hasMorphIn(‘imageable’, [Post::class, Comment::class])->get();
嵌套关联
User::hasIn(‘posts.comments’)->get();

正文完
可以使用微信扫码关注公众号(ID:xzluomor)
post-qrcode
 0
评论(没有评论)

文心AIGC

2024 年 11 月
 123
45678910
11121314151617
18192021222324
252627282930  
文心AIGC
文心AIGC
人工智能ChatGPT,AIGC指利用人工智能技术来生成内容,其中包括文字、语音、代码、图像、视频、机器人动作等等。被认为是继PGC、UGC之后的新型内容创作方式。AIGC作为元宇宙的新方向,近几年迭代速度呈现指数级爆发,谷歌、Meta、百度等平台型巨头持续布局
文章搜索
热门文章
清库存!DeepSeek突然补全R1技术报告,训练路径首次详细公开

清库存!DeepSeek突然补全R1技术报告,训练路径首次详细公开

清库存!DeepSeek突然补全R1技术报告,训练路径首次详细公开 Jay 2026-01-08 20:18:...
2025最大AI赢家的凡尔赛年度总结,哈萨比斯Jeff Dean联手执笔

2025最大AI赢家的凡尔赛年度总结,哈萨比斯Jeff Dean联手执笔

2025最大AI赢家的凡尔赛年度总结,哈萨比斯Jeff Dean联手执笔 鹭羽 2025-12-24 09:1...
AI Coding新王登场!MiniMax M2.1拿下多语言编程SOTA

AI Coding新王登场!MiniMax M2.1拿下多语言编程SOTA

AI C++oding新王登场!MiniMax M2.1拿下多语言编程SOTA 克雷西 2025-12-24 ...
智能体落地元年,Agent Infra是关键一环|对话腾讯云&Dify

智能体落地元年,Agent Infra是关键一环|对话腾讯云&Dify

智能体落地元年,Agent Infra是关键一环|对话腾讯云&Dify 鹭羽 2025-12-23 1...
最新评论
ufabet ufabet มีเกมให้เลือกเล่นมากมาย: เกมเดิมพันหลากหลาย ครบทุกค่ายดัง
tornado crypto mixer tornado crypto mixer Discover the power of privacy with TornadoCash! Learn how this decentralized mixer ensures your transactions remain confidential.
ดูบอลสด ดูบอลสด Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others.
ดูบอลสด ดูบอลสด Pretty! This has been a really wonderful post. Many thanks for providing these details.
ดูบอลสด ดูบอลสด Pretty! This has been a really wonderful post. Many thanks for providing these details.
ดูบอลสด ดูบอลสด Hi there to all, for the reason that I am genuinely keen of reading this website’s post to be updated on a regular basis. It carries pleasant stuff.
Obrazy Sztuka Nowoczesna Obrazy Sztuka Nowoczesna Thank you for this wonderful contribution to the topic. Your ability to explain complex ideas simply is admirable.
ufabet ufabet Hi there to all, for the reason that I am genuinely keen of reading this website’s post to be updated on a regular basis. It carries pleasant stuff.
ufabet ufabet You’re so awesome! I don’t believe I have read a single thing like that before. So great to find someone with some original thoughts on this topic. Really.. thank you for starting this up. This website is something that is needed on the internet, someone with a little originality!
ufabet ufabet Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others.
热评文章
易烊千玺的华为绿手机,真的AI了

易烊千玺的华为绿手机,真的AI了

Failed to fetch content Read More 
AI狼人杀大决战!GPT、Qwen、DeepSeek大乱斗,人类高玩汗流浃背

AI狼人杀大决战!GPT、Qwen、DeepSeek大乱斗,人类高玩汗流浃背

AI狼人杀大决战!GPT、Qwen、DeepSeek大乱斗,人类高玩汗流浃背 鹭羽 2025-12-23 14...
长城首个VLA车型发布,魏建军回应「赌上姓氏造车」

长城首个VLA车型发布,魏建军回应「赌上姓氏造车」

长城首个VLA车型发布,魏建军回应「赌上姓氏造车」 贾浩楠 2025-12-23 13:57:25 来源:量子...