博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【转载】GUID vs INT Debate
阅读量:6983 次
发布时间:2019-06-27

本文共 4111 字,大约阅读时间需要 13 分钟。

I recently read a blog post on what was better using GUIDs or Integer values. This is been an age long debate and there are advocates in both camps stressing on the disadvantages of the other. Well both implementations have their advantages and disadvantages. At the outset, I shall mention that the answer to this debate is: IT DEPENDS! J

It is highly dependent on your database design, migration needs and overall architecture.  There is a good reason why SQL Server replication uses GUIDs to track the changes to the replicated articles. So, it is not that the usage to GUIDs is necessarily a bad practice. SQL Server Books Online lists the following disadvantages for  data type:

  • The values are long and obscure. This makes them difficult for users to type correctly, and more difficult for users to remember.
  • The values are random and cannot accept any patterns that may make them more meaningful to users.
  • There is no way to determine the sequence in which uniqueidentifier values were generated. They are not suited for existing applications that depend on incrementing key values serially.
  • At 16 bytes, the uniqueidentifier data type is relatively larger than other data types, such as 4-byte integers. This means indexes that are built using uniqueidentifier keys might be relatively slower than indexes using an int key.

If you are using  function in SQL Server, then this generates random UUIDs which have a huge domain but the chances of GUID collisions are always there though the probability is very slim in nature. If you are using NEWID function to generate uniqueidentifiers as row identifiers in your table, then you need to think again! Uniqueness of the row should be enforced using a Unique or Primary Key constraint on the table.  function uses identification number of the computer network card plus a unique number from the CPU clock to generate the uniqueidentifier (). So the chance of getting a globally unique value is practically guaranteed as long as the machine has a network card. Moreover, possibility of a GUID collision while using NewSequentialID is virtually impossible.

 

Given that you have a beefy server, the above time difference would not make much of a difference unless and until you only have a high number of concurrent INSERT workload on the server or during a Data Load operation which would cause a significant impact. What is interesting to note is that the fragmentation on the tables after the first batch of 1 million inserts.

Object Name

Index Name

Pages

Average Record Size

Extents

Average Page Density

Logical Fragmentation

Extent Fragmentation

tblGUID

cidx_tblGUID

9608

51.89

1209.00

69.27

99.14

0.25

tblSeqGUID

cidx_tblSeqGUID

6697

51.89

845.00

99.39

0.76

0.12

tblBigINT

cidx_tblBigINT

5671

43.89

714.00

99.95

0.48

0.14

tblINT

cidx_tblINT

5194

39.89

653.00

99.62

0.37

0.15

 

If you look at the above data, you will see that the random GUIDs have 99% logical fragmentation in the tables. This is due to the random nature of the GUIDs generated which end up causing high number of page splits in the database.

--------------

原文地址:

上面的表格说明,普通GUID 会发生很大的页分裂情况,这在一个表反复修改的情况下,可能会明显影响查询速度。

那么怎么生成有序的GUID呢?下面提供一种方法:

using System;using System.Runtime.InteropServices;namespace System{    public static class GuidEx    {        [DllImport("rpcrt4.dll", SetLastError = true)]        private static extern int UuidCreateSequential(out Guid guid);        private const int RPC_S_OK = 0;        ///         /// Generate a new sequential GUID. If UuidCreateSequential fails, it will fall back on standard random guids.        ///         /// 
A GUID
public static Guid NewSeqGuid() { Guid sequentialGuid; int hResult = UuidCreateSequential(out sequentialGuid); if (hResult == RPC_S_OK) { return sequentialGuid; } else { //couldn't create sequential guid, fall back on random guid return Guid.NewGuid(); } } }}

详细的内容,请看讨论。

 

    本文转自深蓝医生博客园博客,原文链接:http://www.cnblogs.com/bluedoctor/p/5109434.html,如需转载请自行联系原作者

你可能感兴趣的文章
4G+宽带高歌猛进:移动双线虐杀联通
查看>>
带你了解超大规模数据中心究竟有何不同?
查看>>
用Python实现每秒处理120万次HTTP请求
查看>>
Android单元测试 - 几个重要问题
查看>>
DNS服务器不能响应的四大解决办法
查看>>
美国税局再遭攻击:原是偷来的社会安全号码作祟
查看>>
如何在Kali Linux中安装Google Chrome浏览器
查看>>
勒索软件防不胜防? 要先从了解它开始
查看>>
大数据精准医疗解读遗传密码 未来医疗健康的变革
查看>>
神经网络基础:七种网络单元,四种层连接方式
查看>>
2014末,Surface Pro 3叫好不叫座只是价格问题?
查看>>
Arimo利用Alluxio的内存能力提升深度学习模型的结果效率(Time-to-Result)
查看>>
代号“沙尘暴”:黑客剑指日本关键基础设施
查看>>
光纤光缆市场需求高于预期 我国将迎来流量经济
查看>>
晶科能源与森源电气签订300MW光伏组件供货协议
查看>>
中国电信发布转型升级战略:构建一横四纵生态圈
查看>>
全渠道的核心是渠道协同和数据整合
查看>>
“小会话,大学问” - 如何让聊天机器人读懂对话历史?| 论文访谈间 #03
查看>>
让问答更自然 - 基于拷贝和检索机制的自然答案生成系统研究 | 论文访谈间 #02...
查看>>
首航节能:光热行业刚起步 子公司处于亏损状态
查看>>