【问题标题】:Fastest way to find sum of column from table2 and show it with table1从 table2 中找到列总和并用 table1 显示的最快方法
【发布时间】:2020-04-27 18:13:05
【问题描述】:

我有两张桌子,

table1
=======================================
pid    pname           coldate      col4
---------------------------------------
1      Tesitng Name1   2019-01-01    self
2      Tesitng Name2   2020-01-01    self
3      Tesitng Name3   2020-03-01    self2
4      Tesitng Name4   2020-04-04    self2
5      Tesitng Name5   2020-04-05    self3

table1 中的 pid 有唯一键

table2 //which have more than 600k rows
=======================================
billid   rate            pid
---------------------------------------
1        30               1
2        50               1
3        40               1
4        20               2
5        60               2
6        10               3

///在table2 billid中有唯一的key

我尝试显示 table1 的所有行以及 table2 的 rate 列的总和 where table1.pid=table2.pid

结果应该是这样的

   table1
    =======================================================
    pid    pname           coldate       col4     total
    -------------------------------------------------------
    1      Tesitng Name1   2019-01-01    self      120
    2      Tesitng Name2   2020-01-01    self       80

我正在使用此查询,它对我有用,但需要很长时间,请告诉我最快的方法

SELECT t1.*,
          (SELECT sum(rate) as total FROM table2 where pid=t1.pid) as total
          FROM table1 t1 WHERE t1.coldate BETWEEN '2020-01-0' AND '2020-04-01'
          AND t1.col4 Like 'self' ORDER BY t1.pid DESC;

我正在使用 php 和 mysql..

【问题讨论】:

    标签: mysql sql group-by mysql-workbench query-optimization


    【解决方案1】:

    试试这个:

    SELECT 
        t1.*
        , ttl.total
    FROM table1 t1 
        inner join 
            (SELECT pid, sum(rate) as total 
            FROM table2 
            GROUP BY pid) as ttl
                on ttl.pid=t1.pid
    WHERE 
        t1.coldate BETWEEN '2020-01-01' AND '2020-04-01'
        AND t1.col4 = 'self' 
    ORDER BY t1.pid DESC;
    

    【讨论】:

    • 感谢您的关注我正在尝试您的回答:)
    • ttl中没有pid
    • 没有pid,因为我们没有选择表2的pid
    • 它有一个问题,它没有显示table1中哪个pid不在table2中的行
    【解决方案2】:

    您使用相关子查询的方法非常好,并且可能是最快的方法。

    为了提高性能,您需要在table2(pid, rate) 上建立索引。

    您还可以考虑在table1(col4, coldate, pid) 上创建索引,并重写col4 上的条件以使用显式相等而不是不带通配符的模式匹配(尽管可能数据库已经优化了它):

    SELECT 
        t1.*,
        (SELECT sum(rate) as total FROM table2 where pid=t1.pid) as total
    FROM table1 t1 
    WHERE 
        t1.coldate BETWEEN '2020-01-01' AND '2020-04-01'
        AND t1.col4 = 'self' 
    ORDER BY t1.pid DESC;
    

    最后,您还可以在SELECT 子句中枚举table1 中的所有列,并将它们添加到索引中,希望它能够覆盖 - table1(col4, coldate, pid, name)

    SELECT 
        t1.pid,
        t1.name,
        t1.coldate,
        t1.col4,
        (SELECT sum(rate) as total FROM table2 where pid=t1.pid) as total
    FROM table1 t1 
    WHERE 
        t1.coldate BETWEEN '2020-01-01' AND '2020-04-01'
        AND t1.col4 = 'self' 
    ORDER BY t1.pid DESC;
    

    【讨论】:

    • 感谢您的关注,但此查询需要很长时间,我正在等待更多答案:)
    • 很抱歉,但它需要相同的时间:(
    • @SmartDeveloper:你有两个推荐的索引吗?
    • 我错过了什么吗?为什么这需要相关的子查询?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-12
    • 2017-11-21
    • 1970-01-01
    • 1970-01-01
    • 2022-01-28
    相关资源
    最近更新 更多