[英]Fast compute average monthly sales for the past 6, 3, 1 months per product
我正在使用PHP / MySQL
我有大約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;
}
知道如何快速嗎? TIA !!!
您不需要每次單擊都更新數據,因此建立一個緩存表來保存數據並在每個時間段更新
您應該查看像AVG這樣的SQL命令
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查詢中提取所有avg調用的函數。 無需多個連接。 每個連接的啟動都會有一些開銷,依此類推,您可以通過在數據庫端進行所有操作來再次傳遞開銷!
也許使用一般功能,而不是每個月檢查一次。 並嘗試使用月份和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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.