1

我需要根据另一个表记录更新一个表记录。

我试过了

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

但给出错误(MySQL 和 SQL Server 的查询工作相同)。

然后我在下面尝试:

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

它正在工作,但速度很慢,我也需要为一些大桌子做这件事。

有任何想法吗?

4

2 回答 2

2

在 Postgres 中,这看起来像:

update currencies t1
    set priority_order = t2.priority_order
    from currencies1 t2
    where t1.id = t2.id;
于 2015-07-08T10:45:49.747 回答
2
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 行,缓存预热后),对源和目标使用相同的表进行更新:

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% 的行被触及,查询仍然会快两倍。(加上查询减少的真空工作)

于 2015-07-08T17:23:08.137 回答