2010-11-02 125 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