【问题标题】:update statement in postgresql for multiple tables join, i want optimized update for below update statement, below statement is taking long timepostgresql 中用于多个表连接的更新语句,我想为下面的更新语句优化更新,下面的语句需要很长时间
【发布时间】:2020-08-05 05:17:27
【问题描述】:
update tabel1 t1 set column5 = t2.column1, column2 = 0
from table1 t
join table2 t2 on t2.column3 = t.column3
left join table3 t3 on t3.coulmn4 = t.column4
where t3.column5 is null

【问题讨论】:

    标签: sql postgresql join sql-update


    【解决方案1】:

    the manual 中所述:

    除非您打算自联接,否则不要将目标表作为 from_item 重复

    所以,不要在 FROM 子句中重复目标表:

    update table1 t1
       set column5 = t2.column1, 
           column2 = 0
    from table2 t2 
      left join table3 t3 on t3.column4 = t2.column4
    where t2.column3 = t1.column3 --<< this replaces the original JOIN to t1
      and t3.column5 is null
    

    【讨论】:

    • left join table3 t3 on t3.column4 = t.column4 这行怎么可能??没有表't'参考??
    • @PraveenKumarReddy:显然这是一个错字,应该是t2.column4
    • from table2 t2 left join table3 t3 on t3.column4 = t2.column4, 这里没有两列与表 2 和表 3 匹配,所以,我无法加入这两个表,所以请提供备用
    • 那一定是on t3.column4 = t1.column4。该查询希望确保更新的行在 table3 中没有匹配项,或者仅与 column5 为空匹配。 (NOT EXISTS 当然会更易读。)
    • @ThorstenKettner:但是 table2 和 table3 之间不会有连接。
    猜你喜欢
    • 1970-01-01
    • 2012-03-22
    • 2016-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-16
    相关资源
    最近更新 更多