当前位置:网站首页>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'];
}
}
}
边栏推荐
- Unity3D学习笔记4——创建Mesh高级接口
- Destroy in beforedestroy invalid value in localstorage
- Unity3d learning notes 4 - create mesh advanced interface
- PHP微信抢红包的算法
- Record the functions of sharing web pages on wechat, QQ and Weibo
- Learn computer knowledge from scratch
- Market Research - current market situation and future development trend of total nutrition products
- LxC terminal login method
- [shutter] shutter gesture interaction (small ball following the movement of fingers)
- 腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?
猜你喜欢

情感计算与理解研究发展概述

Sql service intercepts string
![[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)

"Actbert" Baidu & Sydney University of technology proposed actbert to learn the global and local video text representation, which is effective in five video text tasks!

Perceptron model and Application

Infrastructure is code: a change is coming

scrcpy这款软件解决了和同事分享手机屏幕的问题| 社区征文

What is it that makes you tremble? Those without fans can learn

腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?

New feature of go1.18: introduce new netip Network Library
随机推荐
《Just because》阅读感受
PHP wechat red packet grabbing algorithm
[shutter] shutter page life cycle (initialization period | createstate | initstate | update period | build | destroy period | dispose)
《乔布斯传》英文原著重点词汇笔记(十)【 chapter eight】
Market Research - current market situation and future development trend of total nutrition products
服务可见可观测性
[shutter] shutter application theme (themedata | dynamic modification theme)
Lightgbm principle and its application in astronomical data
Ransack组合条件搜索实现
Riding the wind of "cloud native" and stepping on the wave of "digitalization", new programmer 003 starts pre-sale
New feature of go1.18: introduce new netip Network Library
[QT] QT multithreading development - four methods to realize multithreading design
Unity发布WebGL播放声音的一种方法
佩服,竟然有人把高等数学这么晦涩难懂的科目,讲解得如此通俗易懂
LxC terminal login method
[leetcode] sword finger offer 04 Search in two-dimensional array
APP页面分享口令Rails实现
对象与对象变量
From personal heroes to versatile developers, the era of programmer 3.0 is coming
UE4 UI adaptive screen