在 Postgres 中批量更新

Bulk update in Postgres

我需要根据另一条记录更新一条 table 条记录。

我试过了

update      currencies
set         priority_order= t2.priority_order
from        currencies t1
inner join  currencies1 t2
on          t1.id = t2.id

但给出错误(相同的查询适用于 MySQL 和 SQL 服务器)。

然后我尝试了以下:

update      currencies
set         priority_order= (select 
                            priority_order 
                            from currencies1 
                            where 
                            currencies.id=currencies1.id
                           )

它正在工作,但速度很慢,我也需要为一些大的 table 做。

有什么想法吗?

在 Postgres 中,这看起来像:

update currencies t1
    set priority_order = t2.priority_order
    from currencies1 t2
    where t1.id = t2.id;
UPDATE currencies dst
   SET priority_order = src.priority_order
  FROM currencies src
 WHERE dst.id = src.id
   -- Suppress updates if the value does not actually change
   -- This will avoid creation of row-versions
   -- which will need to be cleaned up afterwards, by (auto)vacuum.
AND  dst.priority_order IS DISTINCT FROM src.priority_order
        ;

测试(10K 行,缓存预热后),对更新的源和目标使用相同的table:

CREATE TABLE
INSERT 0 10000
VACUUM
Timing is on.
cache warming:
UPDATE 0
Time: 16,410 ms
zero-rows-touched:
UPDATE 0
Time: 8,520 ms
all-rows-touched:
UPDATE 10000
Time: 84,375 ms

通常你很少会看到没有行受影响的情况,也不会看到所有行都受影响的情况。但是只有 50% 的行被触及,查询速度仍然是原来的两倍。 (加上在 查询之后减少的真空工作