Postgres 更新缓慢

Slow Update in Postgres

正在尝试更新 440 万行中的一列 table。使用以下版本,我已经能够将查询时间从 30 多分钟减少到 14 分钟:

update t_settlement x
    set dt_ad_decode = y.decode
    from (Select dt_amountdescription_1to1, dt_decode as decode
          from d_amountdescription_1to1
         ) y
    where x."AmountDescription" = y.dt_amountdescription_1to1;

我相信一定有办法进一步改进这一点,如果有人能在这方面帮助我,我将不胜感激。

此致

索拉布

首先,为什么要使用子查询?更简单地写成:

update t_settlement s
    set dt_ad_decode = ad.dt_decode
    from d_amountdescription_1to1 ad
    where s."AmountDescription" = ad.dt_amountdescription_1to1;

这应该不会影响性能,但会简化查询。

接下来,您需要 d_amountdescription_1to1(dt_amountdescription_1to1) 上的索引,或者更好的是 d_amountdescription_1to1(dt_amountdescription_1to1, dt_decode):

create index idx_d_amountdescription_1to1_2
    on d_amountdescription_1to1(dt_amountdescription_1to1, dt_decode)