【问题标题】:Fast compute average monthly sales for the past 6, 3, 1 months per product快速计算每个产品过去 6、3、1 个月的平均月销售额
【发布时间】:2012-08-10 08:41:51
【问题描述】:

我正在使用 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;
}

知道如何快速吗?蒂亚!!!

【问题讨论】:

    标签: php mysql sum average


    【解决方案1】:

    你不需要每次点击都更新数据,所以建立一个缓存表来保存数据并在你想要的每个时间段更新

    【讨论】:

    • 如果您不在网络中使用它,请保存旧的销售平均值以及您计算的开始日期和结束日期,然后当您想要一个新的时,使用这三个数字来计算跨度>
    • 强烈同意。一旦一个月过去了,销售数字就是历史数据,因此应该自动计算然后缓存,甚至总结在自己的表格中。
    【解决方案2】:

    你应该看看像 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 查询中提取所有平均调用。无需多个连接。每个连接都会有一些启动开销等,您可以通过在数据库端执行所有操作再次绕过!

    【讨论】:

      【解决方案3】:

      也许使用一般功能,不要检查每个月。 并尝试使用月份的索引和 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;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-03-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多