当前位置:网站首页>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如下
边栏推荐
- Vscode automatically adds a semicolon and jumps to the next line
- Section 1: (3) logic chip process substrate selection
- R descriptive statistics and hypothesis testing
- ClickHouse(03)ClickHouse怎么安装和部署
- sublime使用技巧
- 全国气象数据/降雨量分布数据/太阳辐射数据/NPP净初级生产力数据/植被覆盖度数据
- Field data acquisition and edge calculation scheme of CNC machine tools
- Why do many people misunderstand technical debt
- Appium practice | make the test faster, more stable and more reliable (I): slice test
- Analyse approfondie de kubebuilder
猜你喜欢
随机推荐
窗口可不是什么便宜的东西
STM32F103ZE+SHT30检测环境温度与湿度(IIC模拟时序)
JS also exports Excel
offer如何选择该考虑哪些因素
Leetcode minimum difference in student scores
深入解析Kubebuilder
Introduction to namespace Basics
Leetcode notes
批量归一化(标准化)处理
A line of R code draws the population pyramid
Gavin teacher's perception of transformer live class - rasa project actual combat e-commerce retail customer service intelligent business dialogue robot microservice code analysis and dialogue experim
Chapter 9 Yunji datacanvas was rated as 36 krypton "the hard core technology enterprise most concerned by investors"
Analysis -- MySQL statement execution process & MySQL architecture
Factor analysis r practice (with R installation tutorial and code)
ServiceMesh主要解决的三大痛点
Time complexity & space complexity
App embedded H5 --- iPhone soft keyboard blocks input text
Flex layout and usage
STM32封装ESP8266一键配置函数:实现实现AP模式和STA模式切换、服务器与客户端创建
npm ERR! 400 Bad Request - PUT xxx - “devDependencies“ dep “xx“ is not a valid dependency name









