之前解释的代码,我来解释数据运行查询在嵌套循环
所以我在此格式有数据的结构
=>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