当前位置:网站首页>ThinkPHP关联预载入with
ThinkPHP关联预载入with
2022-07-06 22:50:00 【天青色在等你】
关联查询的预查询载入功能,主要解决了N+1次查询的问题,有效提高性能。
$list = User::select([1,2,3]);
foreach($list as $user){
// 获取用户关联的profile模型数据
dump($user->profile);
}
上面这部分代码会执行4次查询
使用关联预载入
$list = User::with(['profile'])->select([1,2,3]);
foreach($list as $user){
// 获取用户关联的profile模型数据
dump($user->profile);
}
如果使用关联预查询功能,就可以变成2次查询(对于一对一关联来说,如果使用withJoin方式只有一次查询),有效提高性能。
$list = User::with(['profile'])->select([1,2,3]);
foreach($list as $user){
// 获取用户关联的profile模型数据
dump($user->profile);
}
主要是因为with查询改为了in条件查询,一次性查出了3条profile模型数据
执行sql如下
边栏推荐
- Analyse approfondie de kubebuilder
- Chapter 9 Yunji datacanvas company has been ranked top 3 in China's machine learning platform market
- 九章云极DataCanvas公司摘获「第五届数字金融创新大赛」最高荣誉!
- Local tool [Navicat] connects to remote [MySQL] operation
- In depth analysis of kubebuilder
- What is JVM? What are the purposes of JVM tuning?
- 3.基金的类型
- Ansible中的inventory主机清单(预祝你我有数不尽的鲜花和浪漫)
- Markdown编辑器
- Weebly移动端网站编辑器 手机浏览新时代
猜你喜欢
AttributeError: module ‘torch._C‘ has no attribute ‘_cuda_setDevice‘
01 machine learning related regulations
Windows are not cheap things
01机器学习相关规定
Flex layout and usage
【Android Kotlin协程】利用CoroutineContext实现网络请求失败后重试逻辑
《二》标签
MySQL数据库(基础篇)
Operand of null-aware operation ‘!‘ has type ‘SchedulerBinding‘ which excludes null.
If you‘re running pod install manually, make sure flutter pub get is executed first.
随机推荐
Servicemesh mainly solves three pain points
2.证券投资基金的概述
JS variable
Structure actual training camp | after class homework | module 6
接口间调用为什么要用json、fastjson怎么赋值的、fastjson [email protected]映射关系问题
JS input and output
批量归一化(标准化)处理
What work items do programmers hate most in their daily work?
App embedded H5 --- iPhone soft keyboard blocks input text
Code source de la fonction [analogique numérique] MATLAB allcycles () (non disponible avant 2021a)
PLC Analog output analog output FB analog2nda (Mitsubishi FX3U)
Sublime tips
Field data acquisition and edge calculation scheme of CNC machine tools
What is Web3
In depth analysis of kubebuilder
U++4 接口 学习笔记
When knative meets webassembly
What is JVM? What are the purposes of JVM tuning?
九章云极DataCanvas公司获评36氪「最受投资人关注的硬核科技企业」
Analysis -- MySQL statement execution process & MySQL architecture