Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to populate a table displaying statistics. I have a list of campaign id's have been placed into an array using the following query:

$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'];
}

Once I have all of my id's, I am using a foreach loop to make 5 queries per id gathering all the table data.

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'];
    }
}

The issue is that this process takes 4-5 seconds to perform over 40-50 id records. I placed some timers after each query to confirm the queries were performing correctly and they were. I ran OPTIMIZE and double checked to be sure everything was properly indexed. As far as I can tell, the issue is not in mysql.

The only thing I could find is that there is a .1-.5 second delay after each loop cycle. When trying to run 40-50 rows, this definitely begins to add up.

My question: Is there a better and faster way to fetch this data? Is there something else I should be checking to speed the process up?

The only solution I could think of was to run one query for each statistic (5 total queries) fetching the data for all id and placing them in a array for later display. I'm not sure how that could be done or if it's even possible. It seems to me I would have to run a separate query for each id, but I'm hoping I'm wrong.

Any help will be greatly appreciated!!

share|improve this question
Do those queries in the loop execute correctly?? They all (except for the last one) seem to be missing GROUP BY. Furthermore, in all but the fourth query you do not need to include the campaign_id in selection; including a constant value should be quicker - and not require GROUP BY then. – Aleks G Aug 31 '12 at 16:43
yes and they're very fast (.0001xxxx seconds). – john Aug 31 '12 at 16:46
campaign_id is present so the query pulls records for only that campaign id – john Aug 31 '12 at 16:48
also, the queries are only fetching a count and/or single record, so there is nothing to GROUP BY – john Aug 31 '12 at 16:52

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.