2010-11-02 124 views
2

在笨,我有以下的模型多維數組

function get_item_history($id) 
{ 
    //from metadata_history get item_id and corresponding metadata 
    $this->db->from('metadata_history')->where(array('id'=>$id, 'current_revision'=> "TRUE")); 
    $query = $this->db->get(); 
    $result = $query->result_array(); //store this in an array 

    // loop through the array 
    foreach($result as $key => $row) 
    { 
    $array = array('item_id'=>$row['item_id'], 'current_revision'=> "TRUE"); 
    $this->db->from('history')->where($array); 
    $query = $this->db->get(); 
    $row['items'] = $query->result_array(); // 
    $result[$key] = $row; 
    } 

    return $result; 
} 

的問題是,這會導致多個查詢SQL表顯著增加了執行時間(分頁是不是一種選擇)

我希望能夠將第一個查詢結果作爲一個數組傳遞給第二個查詢,這樣我就只需要一個數據庫,然後從結果中重建一個數組。

我應該如何重寫這段代碼(第二部分)?它會更快(我想是這樣)?

編輯

重建從結果數組是什麼flummoxing我。

http://www.phpbuilder.com/board/showthread.php?t=10373847

這就是我可能想,但我沒有跳

回答

2

您可以在這裏使用內部查詢。正是出於這種理想的狀況 -

function get_item_history($id) 
{ 

// Here the above requirement can be achieved in a single query. 

$sql = "select * from history h 
where h.item_id IN (select item_id from metadata_history mh where mh.id = $id 
AND mh.current_revision = TRUE) AND h.current_revision = TRUE"; 

$result = $this->db->query($sql); 

//Return whichever column of result you want to return or process result if you want. 

$result; 
} 
+0

謝謝,請將您的建議用於我的解決方案 – 2010-11-03 04:45:15

2

您應該使用連接來做到這一點。它將卸載查詢到服務器的執行。我不能給你太多詳細不知道你的數據庫是如何構成的,但檢查出的文檔上的連接:

http://dev.mysql.com/doc/refman/5.0/en/join.html

http://www.webdesign.org/web-programming/php/mysql-join-tutorial.14876.html

http://www.keithjbrown.co.uk/vworks/mysql/mysql_p5.php

+0

我在考慮更多的http://www.phpbuilder.com/board/showthread.php?t=10373847。雖然 – 2010-11-02 03:40:48

2

另一種選擇是將在循環中執行你的小程序,並將查詢執行移至foreach之外:

// loop through the array 
foreach($result as $key => $row) 
{ 
     $array = array('item_id'=>$row['item_id'], 'current_revision'=> "TRUE"); 
     $this->db->or_where($array); 
} 

$query = $this->db->get(); 
$row['items'] = $query->result_array(); // 
$result[$key] = $row; 
+0

嘗試了你的方法,返回結果作爲'單'維數組(真的不單)我需要一個多維數組。 – 2010-11-02 03:53:40

+0

啊對不起,我專注於減少數據庫調用,然後你可以遍歷該數組並構建你的md數組。 – 2010-11-02 03:58:05

+0

您可以告訴我如何重建陣列,以便它會返回類似於初始代碼的結果 – 2010-11-02 04:30:06

0

好了,這花了一些工作,我也不得不做一些調整,在我看來

所以這個問題可以分解爲兩個主要組件

1)通過item_id

通過第一查詢的結果作爲數組使用 where_in

2)重新排序/重新組合第一陣列的結果,第二個

我前面的代碼在做第二部分隱含

因此,這裏是我做過什麼(限制,偏移,排序已經被切出,以提高可讀性 - )

function get_item_history($id) 
{ 
//from metadata_history get item_id and corresponding metadata 
$this->db->from('metadata_history')->where(array('id'=>$id, 'current_revision'=> "TRUE")); 
$query = $this->db->get(); 
$result_query1 = $query->result_array(); //store this in an array 




foreach ($result_query1 as $key-> $row){ 
$result[$row['item_id']]['meta_info'] = $row; //the first query contains meta info, that must be passed to the view 
$selected_id_array[] = $row['item_id']; //Create a array to pass on to the next query 
$result[$row['item_id']]['items'] = array(); //declare an array which will hold the results of second query later 
} 


$this->db->select('h.*'); 
$this->db->from('history h'); 
$this->db->where_in('h.item_id', $selected_id_array); 
$this->db->where(array('h.current_revision' => 'TRUE')); 
$query = $this->db->get(); 

$row = $query->result_array(); 


     foreach ($row as $key => $datarow) { 

     $result[$datarow['item_id']]['items'][] = $datarow; //populate the array we declared earlier with results from second query 
} 




return $result; // Now this variable holds an array which is indexed by item id and contains the results of second query 'grouped' by item_id 
} 

這樣的查詢數量已削減從〜10到2. 在我的本地機器上,這節省了〜50毫秒/頁面,儘管我不確定這對大型數據庫會如何。

+0

PS大家的貢獻是值得讚賞的,我已將Alpesh標記爲正確答案。還有什麼建議? – 2010-11-03 04:40:19