2012-08-10 124 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; 
} 
相關問題