1

我正在尝试填充显示统计信息的表格。我使用以下查询将广告系列 ID 列表放入数组中:

$query = "SELECT `id` FROM `c_templates` ORDER BY `id`";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
    $id[] = $row['id'];
}

一旦我拥有了所有的 id,我将使用 foreach 循环对每个 id 进行 5 次查询,收集所有表数据。

foreach($id as $i){
    // sent
    $query = "SELECT `template`, COUNT(*) as count FROM `s_log` WHERE `template` = '".$i."' AND `time_sent` BETWEEN '".$start."' AND '".$stop."'";
    $result = mysql_query($query) or die(mysql_error());
    while($row = mysql_fetch_array($result)){
        $template[] = $row['template'];
        $count[] = $row['count'];
    }

    // opens
    $query = "SELECT COUNT(*) as count FROM `t_opens` WHERE `campaign_id` = '".$i."' AND `timestamp` BETWEEN '".$start."' AND '".$stop."'";
    $result = mysql_query($query) or die(mysql_error());
    while($row = mysql_fetch_array($result)){
        $opens[] = $row['count'];
    }

    // clicks   
    $query = "SELECT `campaign_id`, COUNT(*) as count FROM `t_analytics` WHERE `campaign_id` = '".$i."' AND `timestamp` BETWEEN '".$start."' AND '".$stop."'";
    $result = mysql_query($query) or die(mysql_error());
    while($row = mysql_fetch_array($result)){
        $click_count[] = $row['count'];
    }

    // conversions
    $query = "SELECT `conversion_value`, COUNT(*) as count FROM `t_analytics` WHERE `campaign_id` = '".$i."' AND `timestamp` BETWEEN '".$start."' AND '".$stop."' AND `conversion_value` > 0";
    $result = mysql_query($query) or die(mysql_error());
    while($row = mysql_fetch_array($result)){
        $conversion_value[] = $row['conversion_value'];
        $conversion_count[] = $row['count'];
    }

    // bounce rate
    $query = "SELECT COUNT(*) AS `ck` FROM `s_log` WHERE `time_sent` BETWEEN '".$start."' AND '".$stop."' AND `status` = 'hardbounce' OR `status` = 'softbounce' AND `template` = '".$i."'";
    $result = mysql_query($query) or die(mysql_error());
    while($row = mysql_fetch_array($result)){
        $bounce_count[] = $row['ck'];
    }
}

问题是这个过程需要 4-5 秒来执行超过 40-50id条记录。我在每个查询之后放置了一些计时器,以确认查询是否正确执行。我运行了 OPTIMIZE 并仔细检查以确保所有内容都已正确编入索引。据我所知,问题不在mysql中。

我唯一能找到的是每个循环周期后都有 0.1-0.5 秒的延迟。当尝试运行 40-50 行时,这肯定会开始增加。

我的问题:有没有更好更快的方法来获取这些数据?还有什么我应该检查的东西来加快这个过程吗?

我能想到的唯一解决方案是为每个统计信息(总共 5 个查询)运行一个查询,获取所有数据id并将它们放在一个数组中以供以后显示。我不确定如何做到这一点,或者是否有可能。在我看来,我必须为每个单独的查询运行一个单独的查询id,但我希望我错了。

任何帮助将不胜感激!!

4

0 回答 0