The Cost of GUIDs as Primary Keys

转载自:http://developmenttips.blogspot.com/2008/03/generate-sequential-guids-for-sql.html,可能有些童鞋因为墙看不到,先把原文贴在这里。
In this article, Jimmy Nilsson presents the pros and cons of using globally unique identifiers (GUIDs) as the datatype for primary keys in SQL Server 2000. In doing so, he shows you test results that hint of performance characteristics and introduces you to a special type of GUID that he invented, called COMBs, that solves what otherwise might give you a big throughput problem.

Natural or Surrogate Keys

When you do the physical design of a relational database, it’s very important to decide upon which style to use for the primary keys. Some people prefer to use natural keys—that is, keys that are found in the domain that the database models. Others prefer to use surrogate keys, which are constructed keys with no other purpose than to be just keys (and which are not found in the domain). An example of a natural key is a Social Security number. A value incrementing by 1 for each row is a typical example of a surrogate key.

Using natural keys is the traditional approach, in line with Codd’s original relational model. When you use them, you have only natural data that means something to users. This is good if users will ask ad hoc queries directly to the database in raw SQL. You can also often reduce the numbers of joins when using natural keys because you don’t have to go to a lookup table to convert an ID to a description. One more advantage is that you get the minimum number of constraints because you don’t have to protect the uniqueness of the natural keys separately. You already did this when you used them as primary keys.

Surrogate keys can be seen as a newer approach. This approach does not conflict with the relational model, but, in a way, it is a step closer to a more object-based approach in which each object has an ID and the structure of all IDs is of the same type. When you use surrogate keys, you often get smaller foreign keys, which reduces the size of the database. There is no risk of users changing the values of the primary keys, and the programming can be more consistent because all keys are of the same format.

NOTE

With cascading updates/deletes in SQL Server 2000, the problem of users changing the values of primary keys is not so great anymore because you don’t have to program the UPDATE of dependent rows manually.

That was a brief description of the different kinds of keys. Now let’s assume that we choose to use surrogate keys when we design a new database.

D:\QuickStart>dotnet build 还原完成(0.4) QuickStart 失败,出现 5 错误 (1.1 秒) D:\QuickStart\Package.wxs(4): error WIX0040: The Package/@UpgradeCode attribute's value, 'PUT-GUID-HERE', is not a legal Guid value. A Guid needs to be generated and put in place of 'PUT-GUID-HERE' in the source file. D:\QuickStart\Package.wxs(52): error WIX0230: The Component/@Guid attribute's value '*' is not valid for this component because it does not meet the criteria for having an automatically generated guid. Components using a Directory as a KeyPath or containing ODBCDataSource child elements cannot use an automatically generated guid. Make sure your component doesn't have a Directory as the KeyPath and move any ODBCDataSource child elements to components with explicit component guids. D:\QuickStart\Package.wxs(52): error WIX0330: The Component/@Id attribute was not found; it is required when there is no valid keypath to use as the default id value. D:\QuickStart\Package.wxs(60): error WIX0230: The Component/@Guid attribute's value '*' is not valid for this component because it does not meet the criteria for having an automatically generated guid. Components using a Directory as a KeyPath or containing ODBCDataSource child elements cannot use an automatically generated guid. Make sure your component doesn't have a Directory as the KeyPath and move any ODBCDataSource child elements to components with explicit component guids. D:\QuickStart\Package.wxs(60): error WIX0330: The Component/@Id attribute was not found; it is required when there is no valid keypath to use as the default id value. 在 1.8 秒内生成 失败,出现 5 错误 这是什么错误 这是什么错误
07-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值