我的控制器中有这段代码
/* 1 */
$header = DB::table('rekap_cmd')->select('date','uniqueid')->groupBy('date')->orderBy('date', 'DESC')->get();
$header = json_decode(json_encode($header), true);
foreach($header as $names => $specs){
/* 2 */
$datax = DB::table('rekap_cmd')->where('uniqueid', $specs['uniqueid'])->get();
$datax = json_decode(json_encode($datax), true);
foreach($datax as $namess => $specss) {
$datetime = new DateTime($specss['date']);
$header[$names]['datetime']['date'] = $datetime->format('d-m-Y');
$header[$names]['datetime']['time'] = $datetime->format('H:i');
}
}
/* 3 */
$data = DB::table('group_symbol')->select('symbol','alias')->where('for_surya', 1)->groupBy('alias')->get();
$data = json_decode(json_encode($data), true);
$odd='odd';
foreach($data as $names => $specs) {
if($odd=='odd'){
$odd='even';
}
else{
$odd='odd';
}
foreach($header as $namess => $specss) {
/* 4 */
$datax = DB::table('rekap_cmd')->where('uniqueid', $specss['uniqueid'])->where('symbol', $specs['alias'])->first();
$data[$names]['data'] = [];
$data[$names]['data']['class'] = $odd;
if($datax){
$data[$names]['data']['buy'] = ($datax->buy)/100;
$data[$names]['data']['sell'] = ($datax->sell)/100;
}
else{
$data[$names]['data']['buy'] = null;
$data[$names]['data']['sell'] = null;
}
}
}
说明:
1 =它将显示2507行(在PHPMyAdmin中显示结果只需0.086秒)
2 =它将平均显示12行(在PHPMyAdmin中显示结果只需0.014秒)
3 =它将显示30行(在PHPMyAdmin中显示结果只需0.024秒)
4 =它将显示1行(在PHPMyAdmin中仅显示0.000..s以显示结果)
但最后这段代码总共耗费了 18分钟!
有人可以解释为什么运行这么长时间?有人建议做得更好吗?
任何帮助都将不胜感激。