SQL服务器 - 的Guid VS.长 [英] SQL Server - Guid VS. Long

查看:118
本文介绍了SQL服务器 - 的Guid VS.长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到现在为止,我一直在使用C#的Guid = Guid.NewGuid();方法来生成一个唯一的ID可以存储作为ID字段在一些使用LINQ to SQL我的SQL Server数据库表。
我已经被告知,用于索引的原因,使用一个GUID是一个坏主意,我应该使用一个自动递增龙来代替。将使用长速度达到我的数据库事务?如果是这样,我怎么去有关生成唯一的ID是long类型?

Up until now i've been using the C# "Guid = Guid.NewGuid();" method to generate a unique ID that can be stored as the ID field in some of my SQL Server database tables using Linq to SQL. I've been informed that for indexing reasons, using a GUID is a bad idea and that I should use an auto-incrementing Long instead. Will using a long speed up my database transactions? If so, how do I go about generating unique ID's that are of type Long?

问候,

推荐答案

两者各有利弊,这完全取决于你如何使用他们的问题。

Both have pros and cons, it depends entirely on how you use them that matters.

马上蝙蝠,如果你需要标识符可以跨多个数据库工作,你需要的GUID。有一些技巧与龙(手动分配每个数据库不同的种子/增量),但这些不能很好地扩展。

Right off the bat, if you need identifiers that can work across several databases, you need GUIDs. There are some tricks with Long (manually assigning each database a different seed/increment), but these don't scale well.

至于索引的推移,龙会给更好的插入性能,如果聚簇索引(默认情况下主键聚集,但这是可以修改你的表),因为该表并不需要在每次插入后进行重组。

As far as indexing goes, Long will give much better insert performance if the index is clustered (by default primary keys are clustered, but this can be modified for your table), since the table does not need to be reorganized after every insert.

至于并发插入关心然而,龙(身份)列会慢一些则GUID - 标识列生成需要一系列的排他锁,以确保只有一行得到一个顺序号。在拥有众多用户插入多行的所有时间的环境中,这可能是一个性能命中。 GUID生成在这种情况下比较快。

As far as concurrent inserts are concerned however, Long (identity) columns will be slower then GUID - identity column generation requires a series of exclusive locks to ensure that only one row gets the next sequential number. In an environment with many users inserting many rows all the time, this can be a performance hit. GUID generation in this situation is faster.

存储明智的,一个GUID占据了龙(8字节比16)两倍的空间。然而,它取决于你行的整体大小,如果8个字节将会使在多少记录适合在一个叶,因此叶片的数目从磁盘的平均请求期间被拉noticable差

Storage wise, a GUID takes up twice the space of a Long (8 bytes vs 16). However it depends on the overall size of your row if 8 bytes is going to make a noticable difference in how many records fit in one leaf, and thus the number of leaves pulled from disk during an average request.

这篇关于SQL服务器 - 的Guid VS.长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆