以下For循环代码中所需的查询优化

时间:2019-04-12 15:09:42

标签: mysql

我有一个表,其中包含超过50万条记录,并且通过以下代码扫描数据库,以查找特定条件(例如状态)的结果。

我必须遍历for loop中返回的结果,以更新与select语句中返回的该用户ID相关的其他表。

如何使此过程更快,更一致?

$query1=$this->db->get_where('binary_incoms',array('sent_to_wallet'=>0))->result(); 

foreach($query1 as $row)
{

    $uid=$row->uid;/**********ftched user id here **********/
    $star_array=['Silver','Gold','Diamond'.'Platinum','Emerald'];
    $star=$this->db->select('star')->get_where('user',array('uid'=>$uid))->row()->star;

    if(!empty($star) && in_array($star,$star_array))
    {

         if($this->is_greater_than_4($uid)=="yes")
         {  
             if($this->weekOfMonth(date("Y-m-d")) < 4)
             {   

                if($this->check_if_added_user_previous($uid)=='true'  || $this->check_if_added_user_current($uid)=='true')
                {
                    $amt=$row->amount;
                    $this->db->query("UPDATE `user_wallet` SET `binary_amt` = `binary_amt`+". floatval($amt).", `total` = `total`+". floatval($amt)." WHERE `uid` ='".$uid."'");
                    $this->db->where('id',$row->id);
                    $this->db->update('binary_incoms',array('sent_to_wallet'=>1));

                }
            }
             else
             {
                 if($this->check_if_added_user_current($uid)=='true')
                  {
                     $amt=$row->amount;
                    $this->db->query("UPDATE `user_wallet` SET `binary_amt` = `binary_amt`+". floatval($amt).", `total` = `total`+". floatval($amt)." WHERE `uid` ='".$uid."'");
                    $this->db->where('id',$row->id);
                    $this->db->update('binary_incoms',array('sent_to_wallet'=>1));
                  }

             }
        }
        else
        {
              $amt=$row->amount;
                $this->db->query("UPDATE `user_wallet` SET `binary_amt` = `binary_amt`+". floatval($amt).", `total` = `total`+". floatval($amt)." WHERE `uid` ='".$uid."'");
                $this->db->where('id',$row->id);
                $this->db->update('binary_incoms',array('sent_to_wallet'=>1));
        }
    }
    else
    {
            $amt=$row->amount;
            $this->db->query("UPDATE `user_wallet` SET `binary_amt` = `binary_amt`+". floatval($amt).", `total` = `total`+". floatval($amt)." WHERE `uid` ='".$uid."'");
            $this->db->where('id',$row->id);
            $this->db->update('binary_incoms',array('sent_to_wallet'=>1));


    }
}

0 个答案:

没有答案