发布于 2017-06-01 10:20:15
在我看来,有两件事是很突出的:
$i参数和$this->downline_id_arr的使用。考虑做:
$children = array();
foreach($data as $row) {
$child_id = $row->id;
$children[$child_id] = array(/**/);
$children = array_merge($children, $this->getAllDownline($child_id);
}
return $childen;现在您不需要$i变量或$this->downline_id_arr。
考虑按级别查询,而不是:
function getAllDownlines($fathers) {
$data = "SELECT * FROM users WHERE father_id IN (/*fathers*/)";
$new_father_ids = array();
$children = array();
foreach ($data as $child) {
$children[$child->id] = array(/**/); // etc
$new_father_ids[] = $child->id;
}
$children = array_merge($children, $this->getAllDownlines($new_father_ids);
return $childen;
}通常,较少的查询速度更快,因此您应该可以看到更好的性能。
https://stackoverflow.com/questions/44303705
复制相似问题