2012-08-10 163 views
0

我正在使用PHP/MySQL每个产品过去6,3,1个月的平均月销售额快速计算

我有大约2,000个产品。我必须计算每个产品过去6,3,1个月的平均月销售额并一次显示在页面中...

目前我有以下代码(每个产品的循环) :

$past_month_sales = $this->SalesPerProduct->getSalesForMonthYear($acc_code, 
                   $item_code, $past_month[0], 
                   $past_month[1]); 

$past_two_months_sales = $this->SalesPerProduct->getSalesForMonthYear($acc_code, 
                    $item_code, $past_two_months[0], 
                    $past_two_months[1]); 

$past_three_months_sales = $this->SalesPerProduct->getSalesForMonthYear($acc_code, 
                    $item_code, $past_three_months[0], 
                    $past_three_months[1]); 

$past_four_months_sales = $this->SalesPerProduct->getSalesForMonthYear($acc_code, 
                    $item_code, $past_four_months[0], 
                    $past_four_months[1]); 

$past_five_months_sales = $this->SalesPerProduct->getSalesForMonthYear($acc_code, 
                    $item_code, $past_five_months[0], 
                    $past_five_months[1]); 

$past_six_months_sales = $this->SalesPerProduct->getSalesForMonthYear($acc_code, 
                    $item_code, $past_six_months[0], 
                    $past_six_months[1]); 

//for past 3 months 
if($past_month_sales == 0 
    || $past_two_months_sales == 0 
    || $past_three_months_sales == 0){ 

    $past_three_sales_ave = "n/a"; 

}else{ 

    $past_three_sales_ave = round(($past_month_sales 
         + $past_two_months_sales 
         + $past_three_months_sales) 
         /3); 
} 

//for past 6 months 
if($past_month_sales == 0 
    || $past_two_months_sales == 0 
    || $past_three_months_sales == 0 
    || $past_four_months_sales == 0 
    || $past_five_months_sales == 0 
    || $past_six_months_sales == 0){ 

    $past_six_sales_ave = "n/a"; 

}else{ 
    $past_six_sales_ave = round(($past_month_sales 
         + $past_two_months_sales 
         + $past_three_months_sales 
         + $past_four_months_sales 
         + $past_five_months_sales 
         + $past_six_months_sales) 
         /6); 
} 

但上面的代码是非常缓慢的,甚至100产品采取年龄的负荷......

我getSalesForMonthYear功能是这样的:

function getSalesForMonthYear($account_code, $item_code, $month, $year){ 
    $sql = "select 
       SalesPerProduct.sales_value 
      from 
       sales_per_products as SalesPerProduct 
      where 
       account_code = '{$account_code}' and 
       item_code = '{$item_code}' and 
       month = '{$month}' and 
       year = '{$year}'"; 

    $val = $this->query($sql); 

    if(empty($val[0]['SalesPerProduct']['sales_value'])){ 
     $val = 0; 
    }else{ 
     $val = $val[0]['SalesPerProduct']['sales_value']; 
    } 
    return $val; 
} 

任何想法如何这款C一个很快? TIA!

回答

1

你不需要更新每次点击发生的数据,因此,建立一个缓存表保存数据并更新每隔一段时间你想

+0

在网页,保存旧的平均销售和开始您计算日期和结束日期,那么当你想获得一个新的,用这三个数字来计算 – 2012-08-10 08:57:07

+0

强烈同意。一个月过去了,销售数字是历史性的,因此应该自动计算并随后进行缓存,甚至可以汇总在自己的表中。 – Cups 2012-08-10 09:09:54

0

你应该看看到SQL命令,如AVG

http://www.w3schools.com/sql/sql_func_avg.asp

它总是好做在数据库方面的计算。没有你写的代码会比提取它之前做得更快。

看到你有索引你的数据库的好,所以它知道该怎么做,何时!

那是你第一次做的!

我认为这会为你解决很多问题。

如果您希望再进一步,您可以检查一下。

http://www.sqlteam.com/article/intro-to-user-defined-functions-updated http://www.w3schools.com/sql/sql_view.asp

您可以创建一个提取一个SQL查询所有平均通话功能和视图。不需要多个连接。每个连接都会有一些开销,所以你可以通过在数据库端做所有事情再次传递!

0

也许使用一个通用的功能,不检查每一个月。 并尝试使用索引几个月,如果你不使用它sales_value

/* 
    $account_code - account 
    $item_code - item 
    $start_month - starting month - NOTE: this is integer: 1,2,....,10,11,12 
    $months - number of months to query 
    $year - the year 
    and SUM to auto calculate the sales_value 
*/ 
function getSalesForMonths($account_code, $item_code, $start_month, $months, $year){ 
    $addQuery = ''; 
    $finalQuery = ''; 

    for($i = 1; $i<=$months; $i++){ 
     $addQuery .= 'month='.($start_month+$i); 
     if($i<$months){ $addQuery .= ' OR '; } 
    } 

    if($months > 1){ 
     $finalQuery = '('.$addQuery.')'; 
    } else { 
     $finalQuery = $addQuery; 
    } 

    $sql = "select 
      SUM(SalesPerProduct.sales_value) 
     from 
      sales_per_products as SalesPerProduct 
     where 
      account_code = '{$account_code}' and 
      item_code = '{$item_code}' and 
      ".$finalQuery." and 
      year = '{$year}'"; 

    $val = $this->query($sql); 

    if(empty($val[0]['SalesPerProduct']['sales_value'])){ 
     $val = 0; 
    }else{ 
     $val = $val[0]['SalesPerProduct']['sales_value']; 
    } 
    return $val; 
} 
相关问题