简体   繁体   English

SQL身份增量和种子

[英]SQL Identity Increment and Seed

I'm using Microsoft SQL Server for my database. 我正在使用Microsoft SQL Server作为我的数据库。

There is an ID column in my table. 我的表中有一个ID列。 In my usage, rows are deleted and re-created many times. 在我的使用中,行被删除并重新创建多次。 So, if I use Identity Increment and Identity Seed of SQL Server, ID numbers will be very big after some time... 因此,如果我使用SQL Server的身份增量和身份种子,一段时间后ID号会非常大......

It is not important that every new row have an ID that is bigger than other rows. 每个新行的ID都大于其他行并不重要。 Just it must be unique. 它必须是独一无二的。

How to do that in SQL Server? 如何在SQL Server中执行此操作? Should I disable Automatic Increment and set ID manually? 我应该禁用自动增量并手动设置ID吗? how? 怎么样?

I'm using C# and SQL Server Express. 我正在使用C#和SQL Server Express。

I'm not sure why you care if the numbers are big or small (hopefully users don't have sentimental value or place any kind of meaning on identity values), but one way to avoid exhaustion problems is to use a BIGINT. 我不确定你为什么关心数字是大还是小(希望用户没有感情价值或对身份价值有任何意义),但避免用尽问题的一种方法是使用BIGINT。 I forget the exact numbers but if you generate something like 1000 IDs a second it would take 80 years or so to hit the limit - and you can double this if you start at the negative boundary. 我忘记了确切的数字,但是如果你每秒生成1000个ID就需要80年左右才能达到极限 - 如果从负边界开始,你可以加倍。 Yes BIGINT is 8 bytes instead of 4 but this is still much smaller and more usable than a GUID. 是BIGINT是8个字节而不是4个字节但是它仍然比GUID小得多且可用。 And if you combine this with data compression, you won't require any more storage than an INT until well after you've used 2 billion numbers. 如果你将它与数据压缩相结合,那么在你使用了20亿个数字之后,你将不再需要任何存储而不是INT。

Don't over-engineer this, and don't make the mistake that an identity value's size or value should mean something. 不要过度设计这一点,也不要误认为身份值的大小或值应该意味着什么。 This is a surrogate value generated for internal identification and efficiency only. 这是仅为内部识别和效率生成的替代值。 If you're telling users about this value, there's something not right. 如果你告诉用户这个价值,那就有些不对劲了。

you can try with this code 你可以试试这个代码

--Disable identity SET IDENTITY_INSERT YourSchema.YourTable OFF GO --Disable identity SET IDENTITY_INSERT YourSchema.YourTable OFF GO

Unless you want to mess about a lot, ie only add thingies in a stored proc, either using select Max(ID) or a table with next_ID in it, coping with multi-user access etc, you are stuck with this in 2005 (SQL 2012 introduces sequences, apparently). 除非你想搞砸很多东西,即只在存储过程中添加东西,使用select Max(ID)或其中包含next_ID的表,处理多用户访问等,你在2005年就会遇到这种情况(SQL 2012年引入了序列。

Other option, is renumber the ids and reseed the identity, but that's a lot of work as well. 其他选项是重新编号ID并重新设置身份,但这也是很多工作。

Besides big numbers aren't a problem, they only take up four bytes, same as small ones. 除了大数字不是问题,它们只占用四个字节,与小字节相同。 Rolling over is an issue of course, but it's going to take a while to do that ! 滚动是一个问题当然,但这需要一段时间才能做到!

You could flip over to a GUID, which will scale, but they make for a very inefficient index, so unless you are going to have more than 2^31 records not worth it just for this either. 你可以翻转到一个可以扩展的GUID,但是它们会产生一个效率非常低的索引,所以除非你有超过2 ^ 31个记录,否则它们也不值得。

As someone else mentioned Bigint would give you greater range, but of course even bigger numbers. 正如其他人所说,Bigint会给你更大的范围,当然还有更大的数字。 :( :(

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

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