当前位置:网站首页>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如下
边栏推荐
- JS 的 try catch finally 中 return 的执行顺序
- 九章云极DataCanvas公司获评36氪「最受投资人关注的硬核科技企业」
- 装饰器基础学习02
- What is Web3
- 一文搞懂常见的网络I/O模型
- App embedded H5 --- iPhone soft keyboard blocks input text
- Section 1: (3) logic chip process substrate selection
- Batch normalization (Standardization) processing
- Organize five stages of actual attack and defense drill
- Weebly mobile website editor mobile browsing New Era
猜你喜欢
Ansible中的inventory主机清单(预祝你我有数不尽的鲜花和浪漫)
《四》表单
Section 1: (3) logic chip process substrate selection
Liste des hôtes d'inventaire dans ansible (je vous souhaite des fleurs et de la romance sans fin)
指针与数组在函数中输入实现逆序输出
Programmers go to work fishing, so play high-end!
Salesforce 容器化 ISV 场景下的软件供应链安全落地实践
【二叉树】二叉树寻路
Mysql database (basic)
sublime使用技巧
随机推荐
高手勿进!写给初中级程序员以及还在大学修炼的“准程序员”的成长秘籍
Servicemesh mainly solves three pain points
Ansible reports an error: "MSG": "invalid/incorrect password: permission denied, please try again“
记录一次压测经验总结
Chapter 9 Yunji datacanvas company won the highest honor of the "fifth digital finance innovation competition"!
Programmers go to work fishing, so play high-end!
How to design API interface and realize unified format return?
基于Bevy游戏引擎和FPGA的双人游戏
Thread和Runnable创建线程的方式对比
A row of code r shows the table of Cox regression model
谈谈讲清楚这件事的重要性
【Android Kotlin协程】利用CoroutineContext实现网络请求失败后重试逻辑
Introduction to namespace Basics
批量归一化(标准化)处理
PLC Analog output analog output FB analog2nda (Mitsubishi FX3U)
Leetcode notes
JS variable case
Ansible中的inventory主机清单(预祝你我有数不尽的鲜花和浪漫)
Analyse approfondie de kubebuilder
Oracle - views and sequences