2015-10-19 119 views
-2

喲!如何減少多個查詢的執行時間?

我有這段代碼。基本上,它應該返回基於位置和什麼位置的用戶看了...

我試圖找出如何縮短這個執行時間繳費作業列表...

$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個查詢,並且您可以想象,這需要時間......很長一段時間......有關如何改進此操作的任何提示?

+3

從循環中刪除您的查詢! –

+3

避免使用FIND_IN_SET()....一個更好的規範化數據庫可以正確索引,然後數據庫可以使用這些索引 –

+1

看起來你必須標準化你的數據庫 – Jens

回答

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; 
       } 
      } 
     } 
+0

您的查詢仍在循環中。如果您想要更好的性能和更少的CPU利用率,請嘗試徹底刪除它。 –

+0

恐怕這個是必要的,因爲每個查詢代表單個工作,然後將其放入工作列表數組...謝謝無論如何,你真的幫助。 – Kirito

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"); 
+0

這一個不工作...返回空數組,而不是數組作業... – Kirito