简体   繁体   English

是什么导致 SQL 服务器的序列号跳过?

[英]What caused SQL Server's Sequence number to skip?

I am using SQL Server's Sequence to generate running number for my document number on my "doc_no" column.我正在使用 SQL 服务器的序列为我的“doc_no”列上的文档编号生成运行编号。 A quick check revealed my table's Identity column number to skip is due to server restart and one way to prevent that is to disable the cache (on my local machine).快速检查显示要跳过的表的标识列号是由于服务器重新启动而导致的,防止这种情况的一种方法是禁用缓存(在我的本地计算机上)。 What about sequence?顺序呢? Is it the same way for Sequence?序列的方式是否相同? Is there any way to see the current sequence number in the Sequence itself?有没有办法在序列本身中查看当前序列号?

在此处输入图像描述

CREATE SEQUENCE [dbo].[PB_SEQ_DOCNO] 
 AS [int]
 START WITH 1
 INCREMENT BY 1
 MINVALUE -2147483648
 MAXVALUE 2147483647
 CACHE 
GO

The CACHE keyword (without an explicit cache size) uses the default server cache size - which is probably 1000 (just like for the IDENTITY columns). CACHE关键字(没有明确的缓存大小)使用默认的服务器缓存大小 - 可能是 1000(就像IDENTITY列一样)。 So SQL Server in the background caches the next 1000 sequence numbers.所以后台的SQL服务器缓存了接下来的1000个序号。 If your server crashes or unexpectedly restarts, those cached sequence numbers are "lost" (as in your case here, obviously).如果您的服务器崩溃或意外重新启动,则那些缓存的序列号将“丢失”(显然,在您的情况下)。

You can use a smaller cache size (by specifying eg CACHE 50 ) or you can even turn this off altogether ( NO CACHE ) - benefit is not/less gaps, drawbacks is slower performance when dishing out the sequence numbers.可以使用较小的缓存大小(通过指定例如CACHE 50 )或者您甚至可以完全关闭它( NO CACHE ) - 好处是没有/更少的间隙,缺点是在抛出序列号时性能较慢。

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

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