当前位置:网站首页>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'];
}
}
}
边栏推荐
- What "real skills" should a million year old cloud native developer master? Alibaba, Tencent, meituan and byte decrypt together
- [QT] Q multithreaded development - Analysis of multithreaded application examples (Mandelbrot)
- Web侧防御指南
- [Jianzhi offer] 57 And are two numbers of S
- Market Research - current situation and future development trend of sickle cell therapy Market
- Dynamic memory allocation (malloc calloc realloc free)
- Market Research - current situation and future development trend of herringbone gear Market
- Les trois principaux points de douleur traités par servicemesh
- [shutter] shutter gesture interaction (small ball following the movement of fingers)
- 《乔布斯传》英文原著重点词汇笔记(十)【 chapter eight】
猜你喜欢
[shutter] shutter resource file use (import resource pictures | use image resources)
How to write a good program when a big book speaks every day?
540. Single element in ordered array
Task and privilege level protection
[staff] Sibelius 7.5.1 score software installation (software download | software installation)
New feature of go1.18: introduce new netip Network Library
Lightgbm principle and its application in astronomical data
The difference between include < > and include ""
Etcd raft protocol
Ransack组合条件搜索实现
随机推荐
APP页面分享口令Rails实现
《Just because》阅读感受
一周生活
[shutter] shutter opens a third-party application (url|launcher plug-in search and installation | url| launcher plug-in official example | open browser | open a third-party application)
Market Research - current market situation and future development trend of intravenous injection (IV) bottles
From "bronze" to "King", there are three secrets of enterprise digitalization
服务可见可观测性
U++ 学习笔记 ----松弛
[leetcode] sword finger offer 04 Search in two-dimensional array
:last-child 不生效解决
[leetcode] sword finger offer 11 Rotate the minimum number of the array
ServiceMesh主要解决的三大痛点
Market Research - current situation and future development trend of sickle cell therapy Market
影视随摘
Market Research - current situation and future development trend of anterior cruciate ligament (ACL) reconstruction Market
The source code of the daily book analyzes the design idea of Flink and solves the problems in Flink
How to write a good program when a big book speaks every day?
Utilisation de simpletk - 4. Question étrange
腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?
[shutter] shutter application theme (themedata | dynamic modification theme)