当前位置:网站首页>PHP optimizes SQL queries in foreach
PHP optimizes SQL queries in foreach
2022-07-02 22:39:00 【If time comes again】
describe
The data to be queried needs to be associated 3 Tables , First use the first two tables left join After querying, use foreach Query the contents of the third table . But in foreach There is too much data in it , It will be very slow and sometimes collapse
An optimization method
stay foreach Find out all the data in the third table
Code :
# 1. Joint query of two tables
$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'];
}
}
}
边栏推荐
- SimpleITK使用——3. 常见操作
- "New programmer 003" was officially launched, and the cloud native and digital practical experience of 30+ companies such as Huawei and Alibaba
- Commodity information management system (C language document version)
- Market Research - current market situation and future development trend of aircraft wireless intercom system
- Destroy in beforedestroy invalid value in localstorage
- Kubernetes resource object introduction and common commands (4)
- Scrcpy this software solves the problem of sharing mobile screen with colleagues | community essay solicitation
- [QT] Q multithreaded development - Analysis of multithreaded application examples (Mandelbrot)
- php实现根据输入的年龄查询出生日期符合的数据
- 100 important knowledge points that SQL must master: using cursors
猜你喜欢

数学建模——图与网络模型及方法(一)
![[shutter] shutter application life cycle (foreground state resumed | background state paused | inactive | component separation state detached)](/img/4c/c8dae41fc2eb18b5153cf36861fc7d.jpg)
[shutter] shutter application life cycle (foreground state resumed | background state paused | inactive | component separation state detached)

Share how to make professional hand drawn electronic maps

《ActBERT》百度&悉尼科技大学提出ActBERT,学习全局局部视频文本表示,在五个视频-文本任务中有效!
![[shutter] shutter gesture interaction (small ball following the movement of fingers)](/img/5a/a8dad8a0943645c980cc4fe7cb55d4.gif)
[shutter] shutter gesture interaction (small ball following the movement of fingers)

LightGBM原理及天文数据中的应用

sql service 截取字符串
![[ODX studio edit PDX] -0.1- how to quickly view the differences in supported diagnostic information between variant variants (service, sub function...)](/img/2b/f31b81cedf37ca187bcaa20dfe0b83.png)
[ODX studio edit PDX] -0.1- how to quickly view the differences in supported diagnostic information between variant variants (service, sub function...)

《Just because》阅读感受

分享一下如何制作专业的手绘电子地图
随机推荐
Micro service gateway selection, please accept my knees!
Attack and defense world PWN question: Echo
Introduction to database system Chapter 1 short answer questions - how was the final exam?
Unity publishes a method of webgl playing sound
Based on asp Net (used mobile phone sales management system) +asp Net+c # language +vs2010+ database can be used for course design and post design learning
Learn computer knowledge from scratch
[autosar-dcm] - 4.3-how UDS $22 and $2e services read and write NVM data
php优化foreach中的sql查询
"New programmer 003" was officially launched, and the cloud native and digital practical experience of 30+ companies such as Huawei and Alibaba
U++ 学习笔记 ----松弛
[ODX studio edit PDX] -0.1- how to quickly view the differences in supported diagnostic information between variant variants (service, sub function...)
Market Research - current situation and future development trend of cell-based seafood market
Market Research - current market situation and future development trend of genome editing mutation detection kit
Technological Entrepreneurship: failure is not success, but reflection is
Market Research - current market situation and future development trend of marine wet exhaust hose
Tencent three sides: in the process of writing files, the process crashes, and will the file data be lost?
100 important knowledge points that SQL must master: using cursors
[staff] Sibelius 7.5.1 score software installation (software download | software installation)
Perceptron model and Application
【C 题集】of Ⅴ