如何获取mysql中前10行的总和?

时间:2016-04-08 18:36:13

标签: mysql database query-optimization

是否可以获得相对于当前行的最后10行的值之和?

我为商店创建了一个数据库,其中包含一个名为purchase_details的表。该表的结构是:

+--------------------------------+---------------+-----+
| Field                          | Type          | Key |
+--------------------------------+---------------+-----+
| Trans_ID                       | int(11)       | PRI |
| Dealer_Name                    | varchar(40)   |     |
| Todays_Purchase                | double(18,10) |     |
| Total_Purchase_In_Last_10_Days | double(18,10) |     |
+--------------------------------+---------------+-----+

示例数据:

+----------+-------------+-----------------+--------------------------------+
| Trans_ID | Dealer_Name | Todays_Purchase | Total_Purchase_In_Last_10_Days |
+----------+-------------+-----------------+--------------------------------+
|        1 | Rahul       | 7769.1488285639 |                           NULL |
|        2 | Rahul       | 4158.5117578537 |                           NULL |
|        3 | Rahul       | 7200.1363099802 |                           NULL |
|        4 | Rahul       | 9338.8341269511 |                           NULL |
|        5 | Rahul       | 5897.7252866370 |                           NULL |
|        6 | Rahul       | 3266.6656585172 |                           NULL |
|        7 | Rahul       | 3188.0742696276 |                           NULL |
|        8 | Rahul       | 4270.5917314234 |                           NULL |
|        9 | Rahul       | 2604.3369713541 |                           NULL |
|       10 | Rahul       | 7908.6014441989 |                           NULL |
|       11 | Rahul       | 2693.4584823737 |                           NULL |
|       12 | Rahul       | 7945.7825034862 |                           NULL |
|       13 | Rahul       | 1904.1472157570 |                           NULL |
|       14 | Rajesh      | 7093.0478540344 |                           NULL |
|       15 | Rajesh      | 3219.3736989638 |                           NULL |
+----------+-------------+-----------------+--------------------------------+

我希望得到最近10笔交易中完成的购买总额,条件是至少应该有10笔交易总结。

预期产出:

+----------+-------------+-----------------+--------------------------------+
| Trans_ID | Dealer_Name | Todays_Purchase | Total_Purchase_In_Last_10_Days |
+----------+-------------+-----------------+--------------------------------+
|        1 | Rahul       | 7769.1488285639 |                   0.0000000000 |
|        2 | Rahul       | 4158.5117578537 |                   0.0000000000 |
|        3 | Rahul       | 7200.1363099802 |                   0.0000000000 |
|        4 | Rahul       | 9338.8341269511 |                   0.0000000000 |
|        5 | Rahul       | 5897.7252866370 |                   0.0000000000 |
|        6 | Rahul       | 3266.6656585172 |                   0.0000000000 |
|        7 | Rahul       | 3188.0742696276 |                   0.0000000000 |
|        8 | Rahul       | 4270.5917314234 |                   0.0000000000 |
|        9 | Rahul       | 2604.3369713541 |                   0.0000000000 |
|       10 | Rahul       | 7908.6014441989 |               55602.6263900000 |
|       11 | Rahul       | 2693.4584823737 |               50526.9360400000 |
|       12 | Rahul       | 7945.7825034862 |               54314.2067800000 |
|       13 | Rahul       | 1904.1472157570 |               49018.2176900000 |
|       14 | Rajesh      | 7093.0478540344 |                   0.0000000000 |
|       15 | Rajesh      | 3219.3736989638 |                   0.0000000000 |
+----------+-------------+-----------------+--------------------------------+

为此,我创建了一个mysql函数,它将Trans_ID和Dealer_Name作为参数,并将返回Todays_Purchase列的总和。

功能定义:

CREATE FUNCTION GET_TOTAL_PURCHASE(paramTransID INT, paramDealerName VARCHAR(40))
         RETURNS DOUBLE(18,10)
    READS SQL DATA
BEGIN
    DECLARE totalPurchase DOUBLE(18,10);
    SET totalPurchase = 0;
    SELECT SUM(Todays_Purchase)
        INTO totalPurchase
        FROM purchase_details
        WHERE Trans_ID > (paramTransID-10)
          AND Trans_ID <= paramTransID
          AND Dealer_Name = paramDealerName;
    RETURN totalPurchase;
END

更新Total_Purchase_In_Last_10_Days列的SQL查询是:

UPDATE purchase_details
SET Total_Purchase_In_Last_10_Days = GET_TOTAL_PURCHASE(Trans_ID, Dealer_Name);

上面的SQL工作正常,但执行时间太长。表中有超过一百万条记录,因此查询时间超过5分钟。锄头要改善吗?

1 个答案:

答案 0 :(得分:0)

派生信息(例如您要求的信息)在SELECTs中正确完成,而不是在表格中包含冗余代码。

如果一个用户提取他的信息,那么动态计算总和将相当便宜。你已经有了FUNCTION来做到这一点。

但是,你真的相信Trans_ID是连续的,没有间隙等吗?你的术语不一致:&#34; _Last_10_Days&#34; vs&#34;前10行&#34;与&#34; Trans _&#34;。 &#34; 10天&#34;如果存在差距,可能会很棘手。等