您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)怎樣進(jìn)行ThinkPHP中網(wǎng)站性能優(yōu)化研究,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
輕易不要用多表查詢,如下代碼以及模擬數(shù)據(jù)測(cè)試結(jié)果能讓你清晰地認(rèn)識(shí)到多表查詢,join,left join,inner join的可怕之處,能不用就不要用
/** * 獲取持有偶像幣總估值 * @param $uid * @return mixed * @author zzl zzl@ourstu.com * 時(shí)間:2019.06.18 */ public function getHasCoinExpect($uid){ $tag='_User_total_tnk_'.$uid; $total=S($tag); if($total===false){ $total=0; //獲取持有偶像幣估值 $page=1;//分頁(yè)查詢,防止某個(gè)用戶擁有太多種偶像幣,如管理員 $row=5000; do{ //測(cè)試idol_coin_has表40W條記錄,用戶10W條記錄,單次循環(huán)耗時(shí)0.1s左右 $idol_has_list=$idol_uids=$idol_price_list=array(); $price=$has_num=0; $idol_has_list=$this->where(array('uid'=>$uid,'has_num'=>array('gt',0)))->page($page,$row)->order('has_num desc,idol_uid asc')->getField('idol_uid,has_num'); $idol_uids=array_keys($idol_has_list); $idol_price_list=D('Idol/Idol')->where(array('uid'=>array('in',$idol_uids),'single_price'=>array('gt',0),'status'=>1))->getField('id,uid,single_price');//這里增加single_price字段,初始化時(shí)為0,有交易時(shí)改變 foreach ($idol_price_list as $vo){ $price=$vo['single_price']; $has_num=$idol_has_list[$vo['uid']]; $total=bc_add(bc_mul($has_num,$price,4),$total,4); } unset($vo); $page++; }while(count($idol_has_list)==$row); S($tag,$total,60); //測(cè)試idol_coin_has表40W條記錄,要查詢用戶的偶像幣持有條數(shù)有10W條記錄,總耗時(shí)1.5~2.5s之間 } return $total; //如下注釋方案,采用inner join方式 測(cè)試idol_coin_has表40W條記錄,用戶10W條記錄,直接崩潰,無(wú)響應(yīng),所以舍棄該方案 /*if(1||$total===false){ $total=0; //獲取持有偶像幣估值 $page=1;//分頁(yè)查詢,防止某個(gè)用戶擁有太多種偶像幣,如管理員 $row=500; G('d'); $sql="select table1.uid,table1.idol_uid,table1.has_num,table2.single_price from __PREFIX__idol_coin_has as table1 inner join __PREFIX__idol_info as table2 on table1.uid={$uid} AND table1.has_num>0 AND table2.single_price>0 AND table1.idol_uid = table2.uid ORDER BY table1.has_num desc,table2.single_price desc "; do{ G('a'); $sql_do=$sql."limit ".($page-1)*$row.",".$row; $list=$this->query($sql_do); foreach ($list as $val){ $total=bc_add(bc_mul($val['has_num'],$val['single_price'],4),$total,4); } unset($val); dump($total); dump($page); $page++; G('b'); dump(G('a','b')); }while(count($list)==$row); dump('e'); dump(G('d','e'));exit; S($tag,$total,60); }*/ }
對(duì)于大批量的操作,千萬(wàn)不要在foreach中出現(xiàn)sql查詢,如下代碼:
一條一條save的方案,每條操作都要0.01s左右,10W條的話,需要100s,是當(dāng)前方案耗時(shí)的10倍,所以這里采用刪除老記錄,批量新增新記錄的方式操作
/** * 重新計(jì)算排名 * @return boolean * @author 鄭鐘良(zzl@ourstu.com) * @date slf */ private function _calculateRanking() { $heatModel=D('idol_heat'); $map['status']=1; $order='heat desc,uid asc'; $page=1; $row=5000; $rank=1; $this->startTrans(); do{ //用9W條數(shù)據(jù)測(cè)試,如果每條都需要改,單次循環(huán)需要0.55s,全部執(zhí)行完需要11s左右 //用9W條數(shù)據(jù)測(cè)試,如果有1W條數(shù)據(jù)需要改,單次循環(huán)需要0.15s,全部執(zhí)行完需要3s左右 //實(shí)際使用環(huán)境中,每次改變不會(huì)超過(guò)十分之一,而且總用戶量很難達(dá)到10W,所以,該方案可行 $heatList=$heatModel->where($map)->order($order)->page($page,$row)->select(); $rank_list=$del_ids=array(); foreach ($heatList as $val){ if($val['last_rank']!=$val['rank']||$val['change_rank']!=$rank-$val['rank']||$val['rank']!=$rank){ $val['last_rank']=$val['rank']; $val['change_rank']=$rank-$val['rank']; $val['rank']=$rank; $rank_list[]=$val; $del_ids[]=$val['uid']; } $rank++; } unset($val); if(count($del_ids)){ //一條一條save的方案,每條操作都要0.01s左右,10W條的話,需要100s,是當(dāng)前方案耗時(shí)的10倍,所以這里采用刪除老記錄,批量新增新記錄的方式操作 $res=$heatModel->where(array('uid'=>array('in',$del_ids)))->delete(); $res1=$heatModel->addAll($rank_list); }else{ $res=1; $res1=1; } if(!$res||!$res1){ break; } $page++; }while(count($heatList)==$row); if($res&&$res1){ if($this->commit()){ return true; } }else{ $this->rollback(); } return false; }
上述就是小編為大家分享的怎樣進(jìn)行ThinkPHP中網(wǎng)站性能優(yōu)化研究了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。