SQL GUID与整数 [英] SQL GUID Vs Integer

查看:94
本文介绍了SQL GUID与整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始了一项新工作,并注意到所有SQL表都将GUID数据类型用作主键.

I have recently started a new job and noticed that all the SQL tables use the GUID data type for the primary key.

在我之前的工作中,我们使用整数(Auto-Increment)作为主键,在我看来,使用它要容易得多.

In my previous job we used integers (Auto-Increment) for the primary key and it was a lot more easier to work with in my opinion.

例如,假设您有两个相关的表;例如,产品和产品类型-我可以轻松地交叉检查两个表的'ProductTypeID'列中的特定行,以快速将数据映射到我的头部,因为它易于存储数字(2,4,45等),而不是(E75B92A3- 3299-4407-A913-C5CA196B3CAB).

For example, say you had two related tables; Product and ProductType - I could easily cross check the 'ProductTypeID' column of both tables for a particular row to quickly map the data in my head because its easy to store the number (2,4,45 etc) as opposed to (E75B92A3-3299-4407-A913-C5CA196B3CAB).

让我感到沮丧的是,我想了解表之间的关系,可惜没有数据库图:(

The extra frustration comes from me wanting to understand how the tables are related, sadly there is no Database diagram :(

很多人说GUID更好,因为您可以例如使用NewID()在C#代码中定义唯一标识符,而无需SQL SERVER这样做-这还使您可以临时知道ID是什么. ...但是我已经看到仍然可以检索下一个自动递增的整数".

A lot of people say that GUID's are better because you can define the unique identifer in your C# code for example using NewID() without requiring SQL SERVER to do it - this also allows you to know provisionally what the ID will be.... but I've seen that it is possible to still retrieve the 'next auto-incremented integer' too.

DBA承包商报告说,如果我们使用Integer类型而不是GUIDS,则查询速度可能会提高30%.

A DBA contractor reported that our queries could be up to 30% faster if we used the Integer type instead of GUIDS...

为什么GUID数据类型存在,它真正提供了什么优势?...即使某些专业人士选择了GUID,也必须有一些很好的理由来说明为什么要实施它?

推荐答案

在某些情况下,GUID可以用作身份字段:

GUIDs are good as identity fields in certain cases:

  • 当您有多个SQL实例(不同的服务器)并且以后需要合并不同的更新而不会影响参照完整性
  • 断开连接的客户端会创建数据-通过这种方式,他们可以创建数据而不必担心ID字段已被占用

GUID生成为全局唯一,这就是为什么它们适用于此类情况的原因.

GUIDs are generated to be globally unique, which is why they are suited for such scenarios.

这篇关于SQL GUID与整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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