简体   繁体   English

Upsert选项:rowversion与datetime

[英]Upsert options: rowversion vs datetime

Many times I need to move the data of a large table (let's call it source) to a clone of it (let's call it target). 很多时候,我需要将一个大表的数据(称为源)移动到它的克隆(称为目标)。 Due to the large size, instead of just deleting/inserting all, I prefer to upsert. 由于尺寸很大,我宁愿向上插入,而不仅仅是删除/插入全部。

For easiness, let's assume an int PK col named "id". 为了简便起见,我们假设一个名为“ id”的int PK col。

Until now, in order to do this, I've used the datetime field dbupddate, existent on both tables, which holds the most recent time the row was inserted/updated. 到目前为止,为了做到这一点,我使用了两个表中都存在的datetime字段dbupddate,该字段保存了最近一次插入/更新行的时间。 This is done by using a trigger which, for any insert/updates, sets dbupddate to getdate(). 这是通过使用触发器完成的,该触发器对于任何插入/更新都将dbupddate设置为getdate()。

Thus, my run-of-the-mill upsert code until now looks something like: 因此,到目前为止,我的常规代码看起来像这样:

update t set (col1=s.col1,col2=s.col2 etc)
from source s
inner join target t on s.id=t.id and s.dbupddate>t.dbupddate

insert target 
select * from source s
where not exists (select 1 from target t where t.id=s.id)

Recently I stumbled on rowversion . 最近我偶然发现了rowversion I have read and understood up to an extent its function, but I'd like to know practically what benefits/drawbacks there are in case I change dbupddate to rowversion instead of datetime. 我已在某种程度上阅读并理解了它的功能,但是实际上我想知道在将dbupddate更改为rowversion而不是datetime时有什么好处/缺点。

Although datetime carries information that may be useful in some cases, rowversion is more reliable since system datetime is always at the risk of getting changed and losing accuracy. 尽管datetime包含在某些情况下可能有用的信息,但由于系统datetime始终有被更改和失去准确性的风险,因此rowversion更为可靠。 In your case, I personally prefer rowversion for its reliability. 就您而言,我个人更喜欢rowversion的可靠性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM