简体   繁体   中英

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:

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.

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?

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:

  • Make it 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. 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. 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. Every data type has a restricted row count.

Do you really think you'll get above 2,147,483,647 rows? 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 .

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.

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?

If it's really a concern, you could just change your dataType to a 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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