哟!
我有这个代码。基本上它应该根据位置和用户查找的位置返回可用作业列表...
我想弄清楚如何缩短这个的执行时间......
$cities = $this->getChildren($location->city_id);
foreach ($cities as $child) {
$grandChildren = $this->getGrandChildren($child, false);
foreach ($grandChildren as $grandChild) {
$cities[] = $grandChild;
}
}
$menu = $this->getPositionChildren($postion);
$menu[] = $postion->menu_id;
// var_dump($cities);
foreach ($cities as $city) {
$city_id = (!empty($city) ? "AND FIND_IN_SET('{$city}', town)" : '');
foreach ($menu as $key) {
$menu_id = (!empty($key) ? " AND FIND_IN_SET('{$key}', category_id)" : '');
$queries[] = "SELECT ponuka_id as id FROM " . TABLE_PREFIX . "ponuky WHERE 1 {$city_id} {$menu_id} AND `published` = '1' ORDER BY `date` DESC";
foreach ($queries as $query) {
$result = mysql_query($query);
if (!$result) {
return false;
}
while ($row = mysql_fetch_object($result)) {
if (!in_array($row->id, $this->_joblist, true)) {
$this->_joblist[] = $row->id;
}
}
}
}
上面的代码可以轻松运行超过1000个查询,您可以想象,这需要时间......很长一段时间...有关如何改进此问题的任何提示?
答案 0 :(得分:0)
代码现在看起来像这样,通过循环查询它运行得更快......
$cities = $this->getChildren($location->city_id);
foreach ($cities as $child) {
$grandChildren = $this->getGrandChildren($child, false);
foreach ($grandChildren as $grandChild) {
$cities[] = $grandChild;
}
}
// $cities = $this->getCityInfoFromArray($cities);
$menu = $this->getPositionChildren($postion);
$menu[] = $postion->menu_id;
var_dump($cities);
foreach ($cities as $city) {
$city_id = (!empty($city) ? "AND FIND_IN_SET('{$city}', town)" : '');
foreach ($menu as $key) {
$menu_id = (!empty($key) ? " AND FIND_IN_SET('{$key}', category_id)" : '');
$queries[] = "SELECT ponuka_id as id FROM " . TABLE_PREFIX . "ponuky WHERE 1 {$city_id} {$menu_id} AND `published` = '1' ORDER BY `date` DESC";
}
}
foreach ($queries as $query) {
$result = mysql_query($query);
if (!$result) {
return false;
}
while ($row = mysql_fetch_object($result)) {
if (!in_array($row->id, $this->_joblist, true)) {
$this->_joblist[] = $row->id;
}
}
}
答案 1 :(得分:0)
你可以这样试试。检查这是否适合您。
$city_id = ""; $city_id = "";
foreach ($cities as $city) {
$city_id .= (!empty($city) ? " AND FIND_IN_SET('{$city}', town)" : '');
foreach ($menu as $key) {
$menu_id .= (!empty($key) ? " AND FIND_IN_SET('{$key}', category_id)" : '');
}
}
$result = mysql_query("SELECT ponuka_id as id FROM " . TABLE_PREFIX . "ponuky WHERE 1 {$city_id} {$menu_id} AND `published` = '1' ORDER BY `date` DESC");