我正在使用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 !!!
答案 0 :(得分:1)
每次点击都不需要更新数据,因此建立一个缓存表来保存数据并更新所需的每个时间段
答案 1 :(得分:0)
您应该查看像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调用。无需多个连接。每个连接都会有一些开销等等,你可以通过在数据库端做一切来再次通过!
答案 2 :(得分:0)
可能使用一般功能而不检查每个月。 并尝试使用index和months_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;
}