PHP优化多个更新并选择查询

时间:2018-11-30 18:33:59

标签: php mysql

我有一个php API,在其中检查不同的条件,并在此基础上获取行的计数并更新同一表中的计数。

以下是我正在编写查询的条件:(注意:1个用户可以有多个广告系列)

1)对于给定的用户(Uid),对于类型为印象的特定广告系列,我正在获取印象数并在表中对其进行更新

2)对于给定的用户(Uid),对于类型为动作的特定广告系列,我正在获取动作计数并更新表中的计数

//To get the Impression/Action/Lead count
        $Impressionarr = [];
        $Actionarr = [];

//IMPRESSION COUNT
            $imp_qry = "select count(*) as ImpressionCount from ClicksAndImpressions where Uid = 101642 and CampaignID =100 and Type='Impression' ;";
            $impData = $this->getClicksAndImpressionsTable()->CustomQuery($imp_qry);
            if($impData[0]['ImpressionCount'] != '' || $impData[0]['ImpressionCount'] !=NULL ){
                $impr_update = "UPDATE ClicksAndImpressions SET ImpressionCount = ". $impData[0]['ImpressionCount'] ." where Uid = 101642 and CampaignID =100 ;";

            }else{
                echo '----not Present';
            }

//Impression count
              foreach($impData as $key=>$data)
              {
                  $data['ImpressionCount'];
                  $Impressionarr[] = $data; 
              }

//ACTION COUNT
            $action_qry = "select count(*) as ActionCount from ClicksAndImpressions where Uid = 101617 and CampaignID =81 and Type = 'Action';";
            $actionData = $this->getClicksAndImpressionsTable()->CustomQuery($action_qry);
            if($actionData[0]['ActionCount'] != '' || $actionData[0]['ActionCount'] !=NULL ){
                $action_update = "UPDATE ClicksAndImpressions SET ActionCount = ". $actionData[0]['ActionCount'] ." where Uid = 101617 and CampaignID =81 ;";
                $actionData = $this->getClicksAndImpressionsTable()->CustomUpdate($action_update); //print_r($actionData);exit;
            }else{
                echo '----not Present';
            }


//Action count
                foreach($actionData as $key=>$data)
                {
                    $data['ActionCount'];
                    $Actionarr[] = $data; 
                }

//Trying to combine the 3 arrays- but 2nd and 3rd not merging into 1st
$combine = array_merge($CampaignDetailsarr,$Impressionarr,$Actionarr);

1)是否可以避免上述重复-在更新查询中还是在多个for循环中。如果可以的话,如何根据我的条件优化上述条件

ie)对于

if( Uid = 123 for some campaignId = 11 and if type is 'Impression')

      -> Update query for impression


else if(Uid = 123 for campaignId= 22 and if type = 'something else')

    -> Update query for something else

2)我已经有一个数组1。我需要将我的2个新数组(Impressionarr [],Actionarr [])合并到第一个数组中,但是下面是即时消息的获取方法。

预期:

enter image description here

出现:

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以通过在数据库中创建一个存储过程并使用所需的数据来调用它来加快速度。据我所知,您要做的大多数逻辑都可以在存储过程中完成。