且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SQL表中主键ID之间的间隙

更新时间:2022-02-02 09:30:35

这是序列


重要:避免阻塞并发从相同序列中获得
个数字的交易,nextval操作永远不会回滚
;也就是说,一旦获取了值,就将其视为已使用,并且
将不再返回。即使周围的
事务稍后中止,或者调用查询最终不使用
值,也是如此。

Important: To avoid blocking concurrent transactions that obtain numbers from the same sequence, a nextval operation is never rolled back; that is, once a value has been fetched it is considered used and will not be returned again. This is true even if the surrounding transaction later aborts, or if the calling query ends up not using the value.

正如评论中指出的那样,序列中的间隔没有任何危害。如果出于某种原因删除表中的某些行,则会在主键值中造成缺口,并且通常不会为将它们重置为连续而烦恼。

As pointed out in the comments there's no harm in having gaps in sequences. If you delete some rows in your table for what ever reason you are creating gaps in your primary key values and you wouldn't normally bother with resetting them to make them sequential.

如果您坚持要创建无缝序列,请阅读此文章: http://www.varlena.com /GeneralBits/130.php ,并准备好慢速插入。

If you insist on creating a gapless sequence, read this article: http://www.varlena.com/GeneralBits/130.php and be prepared for slow inserts.