惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

有赞技术团队
有赞技术团队
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Stack Overflow Blog
Stack Overflow Blog
Recent Announcements
Recent Announcements
Google DeepMind News
Google DeepMind News
G
Google Developers Blog
N
News | PayPal Newsroom
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Apple Machine Learning Research
Apple Machine Learning Research
SecWiki News
SecWiki News
L
LINUX DO - 最新话题
Schneier on Security
Schneier on Security
Y
Y Combinator Blog
小众软件
小众软件
A
Arctic Wolf
博客园 - 【当耐特】
Google Online Security Blog
Google Online Security Blog
P
Palo Alto Networks Blog
T
The Blog of Author Tim Ferriss
Martin Fowler
Martin Fowler
云风的 BLOG
云风的 BLOG
C
Cybersecurity and Infrastructure Security Agency CISA
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Simon Willison's Weblog
Simon Willison's Weblog
S
Securelist
N
News and Events Feed by Topic
腾讯CDC
The Last Watchdog
The Last Watchdog
Google DeepMind News
Google DeepMind News
The GitHub Blog
The GitHub Blog
T
Tor Project blog
Hugging Face - Blog
Hugging Face - Blog
NISL@THU
NISL@THU
D
DataBreaches.Net
雷峰网
雷峰网
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Microsoft Azure Blog
Microsoft Azure Blog
月光博客
月光博客
Security Archives - TechRepublic
Security Archives - TechRepublic
MongoDB | Blog
MongoDB | Blog
AWS News Blog
AWS News Blog
Engineering at Meta
Engineering at Meta
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy International News Feed
C
CERT Recently Published Vulnerability Notes
T
The Exploit Database - CXSecurity.com
F
Full Disclosure
H
Help Net Security
爱范儿
爱范儿
K
Kaspersky official blog

博客园 - 阿福

TypeScript forms authentication failed the ticket supplied was invalid错误 (Windows Server 2008 + IIS 7.5) jQuery Mobile 1.1.1 RC1发布 HashSet<T> vs List<T> WCF Data Services 5.0 RTM发布 EF Power Tools Beta 2发布 Entity Framework 5性能方面的注意事项 jQuery Mobile 1.1.0 RC2发布 源码+幻灯片:学习HTML5/jQuery/ASP.NET MVC/EF Code First的绝佳资源Account at a Glance项目 使用Autofac在ASP.NET Web API上实现依赖注入 在Windows Azure上开发ASP.NET程序与在Windows Sever上有何不同 充分利用缓存来提高网站性能 ASP.NET MVC 4, ASP.NET Web API, ASP.NET Web Pages v2 (Razor)全部开源,并接受来自社区的贡献(contributions) EF5 beta2通过NuGet发布 Getting Started with HTML5 开发HTML5应用你需要了解的 WCF入门资源 jquery history plugin, url hash Run Tasks in an ASP.NET Application Domain ASP.NET 2.0: 生成Excel报表 VS 2008 hot-fix终于出来了 Images; How to create an HTTP handler to dynamically resize images and change quality. ASP.NET 2.0: Add build-in paging feature to repeater/为repeater添加内置分页功能 Tip/Trick ASP.NET 2.0: DropDownList DataBind ASP.NET 2.0: Forms Authentication Across domains Ajax类库、框架、工具包完全列表 怀旧啊 Search Box ASP.NET 2.0: Site Maps Checking All CheckBoxes in a GridView Efficient Data Paging and Sorting with ASP.NET 2.0 and SQL 2005 JavaScript Calendar Cool MSDN ASP.NET 2.0 GridView Control Article Do I have to cry for you Google无法访问 扯淡 狗*! Atlas Control Toolkit and Source Code for the Build-in Asp.Net 2.0 Providers Great New Advanced Article on ASP.NET 2.0 Master Pages ASP.NET 2.0:通过SqlDataSource绑定数据到普通控件 通过客户端扩展实现固定GridView表头功能 不使用ISAPI或IIS wildcard实现不带扩展名URL的转向 VS 2005 Web Application Project & Atlas Control Toolkit & CSS Control Adapter ASP.NET 2.0 Language Swithcer and Theme Swicher 多语言转换和多样式主题转换 How to use virtual path providers to dynamically load and compile content from virtual paths in 
不要用把无序GUID既作为主键又作为聚集索引
阿福 · 2012-04-18 · via 博客园 - 阿福

我一直想当然的认为用GUID做主键没什么大不了,不就是比int多了12位而已吗?而且现在都是SQL Server 2008, 2012时代了,应该更不是问题了吧?而且微软很多项目也是用GUID做主键啊?Sharepoint, ASP.NET SQL Server Membership Provider默认的表等等。而且还有许多而且......

果真这样吗?直到我读了这两篇文章后GUIDs as PRIMARY KEYs and/or the clustering key 和 THAT'S NOT THE POINT!!!,结论令我很吃惊,甚至是“震撼”。

确切的讲,这种糟糕的结果不仅仅是使用GUID作为主键的原因,更主要的,我们通常在把它作为主键的同时还把它作为聚集索引。因为SQL Server默认就是这样的,我们总是接受默认的。注:这里我们是针对随机无序的GUID,如SQL Server的NEWID(), 客户端生成的如.NET的 Guid.NewGuid()。如果是顺序的GUID,如SQL Server NEWSEQUENTIALID()方法生成的,则不会有聚集索引的问题(但长度带来的问题依然存在)。事实是谁会用顺序的GUID呢?我今天才知道有这个方法。

总结两篇文章作者的结论,无序GUID作为主键以及作为聚集索引所带来的问题包括:

  1. 空间的浪费以及由此带来的读写效率的下降。 
  2. 更主要的,存储的碎片化(fragmentation)以及由此带来的读写效率严重下降。

所以,尽量避免用GUID(无序或有序)做主键,不要用无序GUID做聚集索引

我很好奇难道微软的开发人员也会犯这么低级的错误吗?我打开了ASP.NET SQL Server Membership Provider默认的表查看了一下,发现这些表虽然主键是GUID,但聚集索引不是默认的主键,如aspnet_Applications聚集索引是LoweredApplicationName字段,aspnet_Users聚集索引是ApplicationId和LoweredUserName连个字段。