The Cost of GUIDs as Primary Keys

简介:
< DOCTYPE html PUBLIC -WCDTD XHTML TransitionalEN httpwwwworgTRxhtmlDTDxhtml-transitionaldtd>

form:http://www.informit.com/articles/article.aspx?p=25862&redir=1
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.



本文转自 netcorner 博客园博客,原文链接: http://www.cnblogs.com/netcorner/archive/2008/08/08/2912112.html  ,如需转载请自行联系原作者


目录
打赏
0
0
0
0
100
分享
相关文章
|
4月前
|
PRIMARY KEY
【11月更文挑战第15天】
55 4
Primary Key Optimization
表的主键是在重要查询中使用的列或列集,并附有索引以确保快速查询性能。由于不允许NULL值,所以能进行NOT NULL优化。在InnoDB存储引擎下,表数据被物理组织,实现基于主键的高效查找与排序。对于大型且重要的表,若无明显主键,可创建自动增量列作为唯一标识符,在外键联接时作为指向其他表行的指针。
MySQL - key、primary key、unique key、index 区别(二)
MySQL - key、primary key、unique key、index 区别(二)
1157 0
MySQL - key、primary key、unique key、index 区别(一)
MySQL - key、primary key、unique key、index 区别(一)
483 0
MySQL - key、primary key、unique key、index 区别(一)
MYSQL性能调优02_Explain概述、详解id、select_type、table、type、possible_keys、key、key_len、ref、rows、Extra列(一)
MYSQL性能调优02_Explain概述、详解id、select_type、table、type、possible_keys、key、key_len、ref、rows、Extra列(一)
179 0
MYSQL性能调优02_Explain概述、详解id、select_type、table、type、possible_keys、key、key_len、ref、rows、Extra列(一)
MYSQL性能调优02_Explain概述、详解id、select_type、table、type、possible_keys、key、key_len、ref、rows、Extra列(五)
MYSQL性能调优02_Explain概述、详解id、select_type、table、type、possible_keys、key、key_len、ref、rows、Extra列(五)
204 0
MYSQL性能调优02_Explain概述、详解id、select_type、table、type、possible_keys、key、key_len、ref、rows、Extra列(五)
MYSQL性能调优02_Explain概述、详解id、select_type、table、type、possible_keys、key、key_len、ref、rows、Extra列(二)
MYSQL性能调优02_Explain概述、详解id、select_type、table、type、possible_keys、key、key_len、ref、rows、Extra列(二)
190 0
MYSQL性能调优02_Explain概述、详解id、select_type、table、type、possible_keys、key、key_len、ref、rows、Extra列(二)
MYSQL性能调优02_Explain概述、详解id、select_type、table、type、possible_keys、key、key_len、ref、rows、Extra列(四)
MYSQL性能调优02_Explain概述、详解id、select_type、table、type、possible_keys、key、key_len、ref、rows、Extra列(四)
234 0
MYSQL性能调优02_Explain概述、详解id、select_type、table、type、possible_keys、key、key_len、ref、rows、Extra列(四)
MYSQL性能调优02_Explain概述、详解id、select_type、table、type、possible_keys、key、key_len、ref、rows、Extra列(三)
MYSQL性能调优02_Explain概述、详解id、select_type、table、type、possible_keys、key、key_len、ref、rows、Extra列(三)
194 0
MYSQL性能调优02_Explain概述、详解id、select_type、table、type、possible_keys、key、key_len、ref、rows、Extra列(三)
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等