I have three tables in MySQL
wi_indv_training(trn_id,ind_id,is_deleted)
wi_trn_org(trn_id,grp_id,is_deleted)
wi_indv_org(ind_id,grp_id,is_deleted)
Here, Multiple groups can be assigned to a training which is handled by wi_trn_org table. Multiple Individuals can be assigned to many groups. And Many Individuals are involved in training. Here, we are provided individuals are already associated with groups. Now while Inserting a group to training, individuals in that group must be associated with the training. There might already be the group and individuals(some or all) in the given training flagged with is_deleted=yes.
Now,While Inserting, I want to flag the deleted group and individual in training with is_deleted=no, if the data is already in respective tables else the data should be inserted as new data. For This I have implemented following approach which is very slow in performance;
foreach ($grp_id as $key => $value) {
# code...
$rs=$this->db->pdoQuery("SELECT * FROM wi_trn_org
WHERE trn_id='$trn_id'
AND grp_id='$value'")->results();
if(count($rs)>0)
{
$this->db->pdoQuery("UPDATE wi_trn_org SET is_deleted=0
WHERE trn_id='$trn_id' AND grp_id='$value'");
$grp_indv=$this->db->pdoQuery("SELECT ind_id
FROM wi_indv_org
WHERE grp_id='$value'
AND is_deleted=0")->results();
foreach ($grp_indv as $ke => $va)
{
$ind_id=$va['ind_id'];
$rows=$this->db->pdoQuery("SELECT * FROM wi_indv_training
WHERE trn_id='$trn_id'
AND ind_id='$ind_id'")->results();
if(count($rows)>0)
{
$this->db->pdoQuery("UPDATE wi_indv_training
SET is_deleted=0,indv_source='Group'
WHERE trn_id='$trn_id'
AND ind_id='$ind_id'");
}
else
{
$this->db->insert("wi_indv_training",
array("ind_id"=>$ind_id,
"indv_source"=>'Group',
"trn_id"=>$trn_id,
"attendance"=>"yes",
"active"=>"yes"));
}
}
}
else
{
$this->db->insert("wi_trn_org",array('trn_id'=>$trn_id,'grp_id'=>$value));
$grp_indv=$this->db->pdoQuery("SELECT ind_id
FROM wi_indv_org
WHERE grp_id='$value'
AND is_deleted=0")->results();
foreach ($grp_indv as $ke => $va)
{
$ind_id=$va['ind_id'];
$rows=$this->db->pdoQuery("SELECT *
FROM wi_indv_training
WHERE trn_id='$trn_id'
AND ind_id='$ind_id'")->results();
if(count($rows)>0)
{
$this->db->pdoQuery("UPDATE wi_indv_training
SET is_deleted=0,
indv_source='Group'
WHERE trn_id='$trn_id'
AND ind_id='$ind_id'");
}
else
{
$this->db->insert("wi_indv_training",
array("ind_id"=>$ind_id,
"indv_source"=>'Group',
"trn_id"=>$trn_id,
"attendance"=>"yes",
"active"=>"yes"));
}
}
}
}
Can anyone help regarding the best algorithm for performance enhancement?