当前位置:网站首页>php优化foreach中的sql查询
php优化foreach中的sql查询
2022-07-02 22:01:00 【if时光重来】
描述
要查询的数据需关联3个表,先将前两个表使用left join的方式查询后在使用foreach查询第三个表中的内容。但是在foreach里面数据量太多,会非常慢有时还会崩
优化方法
在foreach外将第三张表数据全部查出
代码:
# 1.两个表联查
$list = Db::query("select a.id,a.organ_name,b.basctype,b.bascidno,b.bascname,b.bascaddress from tablea a left join tableb b on a.id=b.aid where a.createtime >= '2022-06-01' ");
if($list){
$new_table3_list = [];
$table3_list = Db::query("select aid,name,tel from tablec where name is null");
if($table3_list){
foreach($table3_list as $v){
$new_table3_list[$v['aid']] = [
'name'=>$v['name'],
'tel'=>$v['tel'],
];
}
}
foreach($list as $key=>$val){
if(!empty($new_table3_list[$val['id']])){
$val['name']=$new_table3_list[$val['id']]['name'];
$val['tel']=$new_table3_list[$val['id']]['tel'];
}
}
}
边栏推荐
- Une semaine de vie
- Unity3D学习笔记4——创建Mesh高级接口
- UE4 UI自适应屏幕
- 《Just because》阅读感受
- Market Research - current situation and future development trend of anti-counterfeiting label market
- 情感计算与理解研究发展概述
- 服务可见可观测性
- Servicemesh mainly solves three pain points
- Meibeer company is called "Manhattan Project", and its product name is related to the atomic bomb, which has caused dissatisfaction among Japanese netizens
- Market Research - current market situation and future development trend of aircraft audio control panel system
猜你喜欢
An overview of the development of affective computing and understanding research
Get off work on time! Episode 6 of Excel Collection - how to split and count document amounts
Official announcement! The golden decade of new programmers and developers was officially released
建立自己的网站(22)
Struct, bit segment, enumeration, union
开发者分享 | HLS, 巧用AXI_master总线接口指令的定制并提升数据带宽-面积换速度...
"New programmer 003" was officially launched, and the cloud native and digital practical experience of 30+ companies such as Huawei and Alibaba
New feature of go1.18: trylock, which has been tossed n times
SimpleITK使用——4. 奇怪的問題
Kubernetes resource object introduction and common commands (4)
随机推荐
SimpleITK使用——4. 奇怪的问题
How to write a good program when a big book speaks every day?
基于ASP.net的手机销售管理系统(二手手机销售管理系统)+ASP.NET+C#语言+VS2010+数据库可以用于课设、毕设学习
Market Research - current market situation and future development trend of night vision goggles for pilots
SimpleITK使用——3. 常见操作
[shutter] shutter page life cycle (initialization period | createstate | initstate | update period | build | destroy period | dispose)
Market Research - current situation and future development trend of anti-counterfeiting label market
Gee: (II) resampling the image
How to center the positioned text horizontally and vertically
Leetcode theme [array] -169- most elements
[Jianzhi offer] 57 And are two numbers of S
Kubernetes resource object introduction and common commands (4)
服务可见可观测性
Tencent three sides: in the process of writing files, the process crashes, and will the file data be lost?
数据库系统概论第一章简答题-期末考得怎么样?
Attack and defense world PWN question: Echo
App page sharing password rails implementation
【AUTOSAR-DCM】-4.3-UDS $22和$2E服务如何读取和写入NVM数据
Promise optimized callback hell
开发者分享 | HLS, 巧用AXI_master总线接口指令的定制并提升数据带宽-面积换速度...