之前解釋的代碼,我來解釋數據運行查詢在嵌套循環
所以我在此格式有數據的結構
=>Department
==>Category
===>Sub-Category
存在具有多個類別的多個部門,這些類別有子類別。
有可能是具有相同名稱但不同的部門和子類別和類別相同的一個多類。
有表包含所有的數據和產品。我有一個適當的提取數據,並把在
Departments
Categories => fk is Departments id
Subcategories => fk is Categories id
一表我正在插入的部門
然後在大類首先我必須得到主管部門的ID,然後在插入類表中的數據:
public function insertDataInCategory($data)
{
if(!empty($data)){
//pass data from hierarchy
foreach ($data as $dkey => $drow) {
//get id
$this->db->select('id');
$this->db->from('departments');
$this->db->WHERE('name',$dkey);
$query = $this->db->get();
//getting department it to identify category
$did = $query->result_array();
//adding category from department
foreach ($drow as $ckey => $crow) {
$cat_content = array(
'department_id' => $did[0]['id'] ,
'name' => $ckey ,
'description' => ''
);
$this->db->insert('categories', $cat_content);
}
}
}
}
在子類第一,我必須得到主管部門的ID爲類別之後,我確定這個子類都屬於這一類。 因此,在這裏運行的3個查詢正在增加執行時間。
public function insertDataInSubcategory($data)
{
//pass data from hierarchy
if(!empty($data)){
//department foreach
foreach ($data as $dkey => $drow) {
//category each
$this->db->select('id');
$this->db->from('departments');
$this->db->WHERE('name',$dkey);
$query = $this->db->get();
//getting department it to identify category
$did = $query->result();
foreach ($drow as $ckey => $crow) {
//get id
$this->db->select('id');
$this->db->from('categories');
$this->db->WHERE('name',$ckey);
$this->db->WHERE('department_id',$did[0]->id);
$query = $this->db->get();
//getting id of category of sub-category
$cid = $query->result_array();
//sub-category foreach
foreach ($crow as $skey => $srow) {
$scat_content = array(
'category_id' => $cid[0]['id'] ,
'name' => $skey ,
'description' => ''
);
$this->db->insert('sub_categories', $scat_content);
}
}
}
}
}
insertDataInCategory和insertDataInSubcategory時間的功能急劇增加。請告訴我如何減少它的時間。和適當的方法插入到表
什麼是傳遞給'insertDataInCategory($數據)'$ data'的內容'和'insertDataInSubcategory($數據)'?請顯示可能傳遞的值的結構和簡短示例。 – DFriend