我正在使用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!
在網頁,保存舊的平均銷售和開始您計算日期和結束日期,那麼當你想獲得一個新的,用這三個數字來計算 – 2012-08-10 08:57:07
強烈同意。一個月過去了,銷售數字是歷史性的,因此應該自動計算並隨後進行緩存,甚至可以彙總在自己的表中。 – Cups 2012-08-10 09:09:54