Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

public function getdataforadset() { while (true) { $ad =…

  1. User

    public function get_data_for_adset() { while (true) { ad=RedisDB::getlPop(self::FBADSETRDS);if(!ad = RedisDB::get_lPop(self::FB_ADSET_RDS); if(!ad) break; if(!empty(ad['errNum']) && ad['errNum'] >= 3){ log_message('error','重新执行3次未成功:'.json_encode(ad)); continue; } // 时间 starTime = date('Y-m-d',strtotime(ad[′starttime′]));ad['start_time'])); endTime = date('Y-m-d',strtotime('-1 day', time())); if(!empty(ad[′endtime′]))ad['end_time'])) endTime = date("Y-m-d",strtotime(ad[′endtime′]));//请求参数拼接ad['end_time'])); // 请求参数拼接 url = self::JAVA_URL."/fb/adset/".ad[′adsetid′]."/insights";ad['adset_id']."/insights"; header = ['t:'.self::JAVA_TOKEN]; params=array(′id′=>params = array( 'id' => ad['adset_id'], 'accessToken' => this−>accesstoken,′insights′=>["fields"=>′accountid,campaignid,adsetid,adid,accountname,campaignname,adsetname,adname,impressions,clicks,spend,reach,actions,uniqueactions′,"timerange"=>["since"=>this->access_token, 'insights' => [ "fields" => 'account_id,campaign_id,adset_id,ad_id,account_name,campaign_name,adset_name,ad_name,impressions,clicks,spend,reach,actions,unique_actions', "time_range" => ["since"=>starTime,"until"=>endTime],"reqAll"=>true,"timeincrement"=>1,"level"=>′adset′,′breakdowns′=>endTime], "reqAll" => true, "time_increment" => 1, "level" => 'adset', 'breakdowns' => ad['breakdown'] == 1 ? 'country' : '' ], ); infoJson=curlpostjson(infoJson = _curl_post_json(url,params,params,header[0]); info=jsondecode(info = json_decode(infoJson,true);

            if($info['code'] != 200){
                log_message('error','请求java失败,错误信息:'.$infoJson);
                $errContent = json_decode($info['content'],true);
                if($errContent['error']['code'] == 4 || $errContent['error']['code'] == 80000){
                    $ad['errNum'] += 1;
                    log_message('error','广告系列请求数据量过大,重新插入队列'.json_encode($ad));
                    // sleep(300);
                    RedisDB::rpush(self::FB_ADSET_RDS,serialize($ad));
                }
                continue;
            }
            if(count($info['content']) == 0){
                continue;
            }
            $insert = [];
            $upd = [];
            foreach ($info['content'] as $content) {
                $unique_actions = array_column($content['unique_actions'],'value','action_type');                
                $actions = array_column($content['actions'],'value','action_type');
                // 月份
                $month = date("Ym",strtotime($content['date_start']));
                // 类型
                if($ad['isExcel'] != 1){
                    $name = explode('_',$content['campaign_name']);
                    $type = strtolower($name[3]) == 'new' ? 2 : 1;                    
                }
                $insert[$month][] = [
                    'biz_date'          => date("Ymd",strtotime($content['date_start'])),
                    'type'              => $type ?? 3,
                    'report_rule_id'    => $ad['rule_id'],
                    'account_id'        => $content['account_id'],
                    'account_name'      => $content['account_name'],
                    'campaign_id'       => $content['campaign_id'],
                    'campaign_name'     => $content['campaign_name'],
                    'country'           => $content['country'] ?? '',
                    'adset_id'          => $content['adset_id'] ?? '',
                    'adset_name'        => $content['adset_name'] ?? '',
                    'ad_id'             => $content['ad_id'] ?? '',
                    'ad_name'           => $content['ad_name'] ?? '',
                    'impression'        => $content['impressions']  ?? 0,
                    'clicks'            => $content['clicks']  ?? 0,
                    'uo_click'          => $unique_actions['link_click']  ?? 0,
                    'spend'             => $content['spend']  ?? 0,
                    'link_click'        => $actions['link_click']  ?? 0,
                    'reach'             => $content['reach'] ?? 0,
                    'install'           => $actions['mobile_app_install'] ?? 0,
                    'unique_purchases'  => $unique_actions['omni_purchase'] ?? 0,
                ];
    
                // name补充
                $upd[$month] = [
                    'ad_name' => $content['ad_name'],
                    'adset_name' => $content['adset_name'],
                    'campaign_name' => $content['campaign_name'],
                    'account_name' => $content['account_name'],
                ];
            }
            
            if (!empty($insert)) {
                // 关闭再重启mysql
                $this->MonthDb_Z->db->close();
                $this->MonthDb_Z->db->initialize();
                $monthDb = $this->MonthDb_Z;
                foreach ($insert as $month => $add) {                    
                    $monthTable = $this->lk_ksh_fb.$month;
                    $monthDb->is_kshFbMonth_table($monthTable);
                    $monthDb->delete($monthTable, [
                        'biz_date >='    => date("Ymd", strtotime($starTime)),
                        'biz_date <='    => date("Ymd", strtotime($endTime)),
                        'report_rule_id' => $ad['rule_id'],
                        'adset_id'       => $ad['adset_id']
                    ]);
                    $monthDb->add_batch($monthTable, $add);
                }
            }
            if(!empty($upd)){
                // 关闭再重启mysql
                $this->MonthDb_Z->db->close();
                $this->MonthDb_Z->db->initialize();
                $monthDb = $this->MonthDb_Z;
                foreach ($upd as $month => $update) {                    
                    $monthTable = $this->lk_ksh_fb.$month;
                    if(!$monthDb->is_table($monthTable)){
                        continue;
                    }
                    $monthDb->update($monthTable,['adset_id'=>$ad['adset_id']],$update);
                }
            }
            unset($insert,$upd);
        }
    }怎么提升这个方法的效率
    
  2. Assistant

    有以下几种方式可以提升这个方法的效率:

    1. 使用多线程或多进程处理:多线程或多进程处理可以充分利用计算机的多核处理能力,同时处理多个任务,从而提高处理效率。

    2. 批量数据处理:不要一次处理一条数据,而是尽可能的批量处理多条数据,这样可以减少数据库操作的次数,从而提高处理效率。

    3. 减少数据库操作:尽可能的减少数据库的操作次数,例如批量插入、批量更新等操作。

    4. 使用缓存:可以使用缓存来存储一些常用的数据,从而避免重复从数据库中读取,提高处理效率。

    5. 优化数据库结构:可以对数据库进行优化,例如增加索引、分表等操作,从而提高查询和操作的效率。