在嵌套循环中运行查询

时间:2017-10-28 12:46:11

标签: php mysql codeigniter

在解释代码之前,我必须解释数据结构

所以我有这种格式的数据

=>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时间的功能正在急剧增加。请告诉我如何减少时间。以及插入表格的适当方法

2 个答案:

答案 0 :(得分:1)

问题是您多次运行查询会增加执行时间。运行单个查询会减少您的时间。

试用此代码:

public function insertDataInCategory($data)
{   
    if(!empty($data)){
        //pass data from hierarchy
        foreach ($data as $dkey => $drow) {
            //get id
            //adding category from department
            foreach ($drow as $ckey => $crow) {
                   $query = "INSERT INTO categories ";
                   $query.= "(department_id, name , description)";
                   $query.= "VALUES ((SELECT id FROM departments WHERE name = '$dkey') , '$ckey' , '')";
                   $this->db->query($query);
            }
        }
    }
}

public function insertDataInSubcategory($data)
{   
    //pass data from hierarchy
    if(!empty($data)){
        //department foreach
        foreach ($data as $dkey => $drow) {
            foreach ($drow as $ckey => $crow) {
                //sub-category foreach
                foreach ($crow as $skey => $srow) {
                   $query = "INSERT INTO sub_categories ";
                   $query.= "(category_id , name , description)";
                   $query.= "VALUES ((SELECT id FROM categories WHERE name = '$ckey' && department_id = (SELECT id FROM departments WHERE name = '$dkey')) , '$skey' , '')";
                   $this->db->query($query);
                }       
            }
        }
    }
}

现在只设置了一个连接,它将更快地插入记录。

答案 1 :(得分:0)

请修改功能,如下所述。它将解决您的嵌套循环问题。这里我假设$ this-> db-> insert返回插入的行id。如果它没有返回id,那么你需要使用select查询获取它并将其存储在提到的变量中。

 public function insertDataInDepartment($data)
 {
     if(!empty($data)){
         //pass data from hierarchy
         foreach ($data as $dkey => $drow) {
                 $dept_content = array(
                'name' => $dkey ,
                'description' => '' ,
                'is_active' => 'y'
             );
              $intInsertId = $this->db->insert('departments', $dept_content);
           insertDataInCategory($drow , $intInsertId);
         }
     }
 }

 public function insertDataInCategory($drow, $deptId)
 {   
      foreach ($drow as $ckey => $crow) {
          $cat_content = array(
               'department_id' => $did[0]['id'] ,
               'name' => $ckey ,
               'description' => ''
          );
          $intCatId = $this->db->insert('categories', $cat_content);
          insertDataInSubcategory($crow, $intCatId);
     }
 }

 public function insertDataInSubcategory($data)
 {   
      foreach ($crow as $skey => $srow) {
           $scat_content = array(
                'category_id' => $cid[0]['id'] ,
                'name' => $skey ,
                'description' => ''
           );
           $this->db->insert('sub_categories', $scat_content);
     }
 }