Mysql 从表2中找到列和并与表1一起显示的最快方法

Mysql 从表2中找到列和并与表1一起显示的最快方法,mysql,sql,group-by,mysql-workbench,query-optimization,Mysql,Sql,Group By,Mysql Workbench,Query Optimization,我有两张桌子 table1 ======================================= pid pname coldate col4 --------------------------------------- 1 Tesitng Name1 2019-01-01 self 2 Tesitng Name2 2020-01-01 self 3 Tesitng Name3 2020-03-

我有两张桌子

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
在表1中,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
///在表2中,billid具有唯一密钥

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的“速率之和”列显示table1的所有行,其中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结合使用。

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

对于性能,您需要在
表2(pid,rate)
上设置索引

您也可以考虑索引<代码>表1(COL4、COLDATE、PID),并重写<代码> COL4中的条件,使用显式相等而不是没有通配符的模式匹配(尽管可能数据库确实已经优化了):

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

试试这个:

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;

感谢您的关注,但此查询需要花费大量时间,我正在等待更多答案:)抱歉,但它占用的时间相同:(@SmartDeveloper:您有两个建议的索引吗?我是否遗漏了什么?为什么这需要一个相关的子查询?感谢您的关注,我正在尝试您的答案:)TTL中没有pid。没有pid是因为我们没有选择表2中的pid。有一个问题,它没有显示表2中没有pid的表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;