简体   繁体   English

SQL Server / ASP.NET MVC 4中PK int的大小

[英]Size of a PK int in SQL Server / ASP.NET MVC 4

Currently, my primary key data type is an int, not null , and is defined as following from the application: 当前,我的主键数据类型是int, not null ,并在应用程序中定义如下:

public virtual int Id { get; set; } 

I am a bit worried though, since int is restricted, at some point, it will not be possible to add new rows because of the size of the primary key. 不过,我有点担心,由于int受到限制,在某些时候,由于主键的大小,将无法添加新行。

How should this problem be approached? 应该如何解决这个问题? I was thinking about a) simply changing the data type to long, etc. or, b) if possible, remove the primary key since it is not used at any time in the application? 我在考虑a)只是将数据类型更改为long等,或者b)如果可能,请删除主键,因为在应用程序中任何时候都不使用它?

Don't remove your primary key, you need it to identify records in your database. 不要删除您的主键,您需要它来标识数据库中的记录。 If you expect to have more than int can handle, you can: 如果您希望拥有比int可以处理的更多功能,则可以:

  • Make it bigint 使其bigint
  • Make it uniqueidentifier 使其成为唯一标识符
  • Make it a composite key (made up of two or more fields) 使其成为复合键(由两个或多个字段组成)

Unless you're dealing with very small tables (10 rows), you never want to go without a primary key. 除非您要处理非常小的表(10行),否则永远不要没有主键。 The primary key dramatically affects performance as it provides your initial unique and clustered index. 主键会提供初始的唯一索引和聚簇索引,因此会极大地影响性能。 Unique indexes play a vital role in maintaining data integrity. 唯一索引在维护数据完整性方面起着至关重要的作用。 Clustered indexes play a vital role in allowing SQL Server to perform index and row seeks instead of scans. 聚集索引在允许SQL Server执行索引和行查找而不是扫描方面起着至关重要的作用。 Basically, does it have to load one row or all of the rows. 基本上,它必须加载一行还是全部行。

Changing the data-type will affect your primary index size, row size, as well the size of as any index placed on the table. 更改数据类型将影响您的主索引大小,行大小以及放置在表上的任何索引的大小。 Unless you're worried about exceeding 2,147,483,647 rows in the near future, I would stick with an INT. 除非您担心在不久的将来会超过2,147,483,647行,否则我将坚持使用INT。 Every data type has a restricted row count. 每种数据类型的行数都有限制。

Do you really think you'll get above 2,147,483,647 rows? 您是否真的认为会获得超过2,147,483,647行? I doubt it. 我对此表示怀疑。 I wouldn't worry about it. 我不会担心。

If you, at some point, begin to reach the limit, it should be trivial to change it to a bigint . 如果您在某个时候开始达到极限,那么将其更改为bigint应该很简单。

It depends on how big you're expecting this table to become - take a look at the reference page for SQL Server for supported ranges and you can answer the question about the need to change the data type of the PK for yourself. 这取决于您期望该表的大小- 请查看SQL Server的参考页以了解受支持的范围,然后您可以回答有关需要自己更改PK数据类型的问题。

If the key is really never used (not even as a foreign key) then culling it is entirely seemly. 如果确实从未使用过该密钥(甚至不用作外键),则完全将其剔除。

You should always have a primary key, so I wouldn't remove that. 您应该始终有一个主键,因此我不会删除它。 However, do you really think you're going to exceed to limit of 2,147,483,647 rows in your table? 但是,您是否真的认为您将要超出表中2,147,483,647行的限制?

If it's really a concern, you could just change your dataType to a bigint . 如果确实有问题,可以将dataType更改为bigint

Here is also a limits sheet on what SQL server can handle - that may help you get a fix on what you need to plan for. 这也是SQL Server可以处理的限制表 -可以帮助您解决需要计划的问题。

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

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