簡體   English   中英

多維數組

[英]MultiDimensional Arrays

在Codeigniter中,我有以下模型

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表的多個查詢大大增加了執行時間(不可分頁)

我希望能夠將第一個查詢結果作為數組傳遞給第二個查詢,這樣我就只能一次訪問數據庫,然后根據結果重建一個數組。

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

編輯

從結果中重建數組真是困擾我。

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

這可能是我想要的,但是沒有成功

您應該使用JOIN做到這一點。 它將查詢的執行工作卸載到服務器。 在不了解數據庫結構的情況下,我無法為您提供更多詳細信息,但請查看有關JOIN的文檔:

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

另一種選擇是在循環中進行操作,並將查詢執行移至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;

您可以在此處使用內部查詢。 理想的情況是-

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

好的,這需要一些工作,而且我還必須做一些調整

因此,問題可以分為兩個主要部分

1)使用where_in將第一個查詢的結果作為數組傳遞給第二個where_in

2)通過item_id對第一個數組的結果重新排序/重新item_id

我之前的代碼隱式地做第二個組件

所以這就是我所做的(限制,偏移量,順序已被切除以提高可讀性)

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毫秒/頁,盡管我不確定這對大型數據庫的影響。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM