2015-06-12 79 views
1

我對CI比較新,我讀到使用join可能會減慢報告速度,在我的數據庫中會有超過10萬條記錄。我已經在CI中寫了這兩個查詢,但沒有注意到很多差異。我想知道,如果我正在編寫這些查詢,或者如果有更好的方法來獲得這樣的查詢,不幸的是我不知道如何在結構樣例中發佈這些查詢。優化codeigniter查詢,加入或哪裏

function select_data_specific_customer($from, $to, $customer, $threshold = '') { 

     //die($from); 
     $currentuserid = $this->session->userdata('userid'); 
     $this->db->select('timestamp'); 
     $this->db->select('count'); 
     $this->db->select('price'); 
     $this->db->select('totalPrice'); 
     $this->db->select('reference'); 
     $this->db->select('customers_r'); 
     $this->db->select('userID_r'); 
     $this->db->from('logger'); 
     $this->db->from('users_customer'); 
     $this->db->where(array('users_customer.custo_id' => "logger.customers"), NULL, FALSE);  
     //users_customer contains connection between users and customers ( id userID custo_id checked) 
     $this->db->where(array('users_customer.userID' => $currentuserid), NULL, FALSE); 
     $this->db->where(array('users_customer.checked' => '1'), NULL, FALSE); 
     $this->db->where('cast(timestamp as date) BETWEEN "' . date('Y-m-d', strtotime(str_replace('-', '/', $from))) . '" and "' . date('Y-m-d', strtotime(str_replace('-', '/', $to))) . '"'); 


     $query = $this->db->get(); 

     if ($query->num_rows() > 0) { 
      foreach ($query->result() as $row) { 
       $data[] = $row; 
      } 

      return $data; 
     } 

     return false; 
    } 

這是第二個功能

function select_data_specific_customer($from, $to, $customer, $threshold = '') { 

     //die($from); 
     $currentuserid = $this->session->userdata('userid'); 
     $this->db->select('timestamp'); 
     $this->db->select('count'); 
     $this->db->select('price'); 
     $this->db->select('totalPrice'); 
     $this->db->select('reference'); 
     $this->db->select('customers_r'); 
     $this->db->select('userID_r'); 
     $this->db->from('logger'); 
     $this->db->join("users_customer", "users_customer.custo_id=logger.customers", 'inner'); 
     $this->db->where(array('users_customer.userID' => $currentuserid), NULL, FALSE); 
     $this->db->where(array('users_customer.checked' => '1'), NULL, FALSE); 
     $this->db->where('cast(timestamp as date) BETWEEN "' . date('Y-m-d', strtotime(str_replace('-', '/', $from))) . '" and "' . date('Y-m-d', strtotime(str_replace('-', '/', $to))) . '"'); 


     $query = $this->db->get(); 

     if ($query->num_rows() > 0) { 
      foreach ($query->result() as $row) { 
       $data[] = $row; 
      } 

      return $data; 
     } 

     return false; 
    } 

我想知道是否有寫相同的查詢,因爲此時的作用都正在約6秒鐘,顯示更優化的方式。 結果可能會增加到超過200k以後,並且時間可能會增加以顯示它。

任何幫助將不勝感激。

+0

你爲什麼不進行分頁? –

+0

客戶不希望分頁,因爲這些數據將被加載,然後作爲結算髮送給客戶。所以他想獲得完整的數據顯示然後複製粘貼到電子郵件或在Excel中進行進一步操作 –

+0

我替換爲每個... data []通過返回$ query-> result_array();它直接返回一個數組。顯然這並沒有改變任何東西:( –

回答

0
return $this->db->query("your query here"); 

你嘗試上面的查詢和使用*來選擇