<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Facility9 &#187; GUIDs</title>
	<atom:link href="http://facility9.com/tag/guids/feed/" rel="self" type="application/rss+xml" />
	<link>http://facility9.com</link>
	<description>Jeremiah Peschka - professional something or other</description>
	<lastBuildDate>Fri, 06 Jan 2012 15:00:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>GUIDs: Not Necessarily Unique</title>
		<link>http://facility9.com/2009/11/guids-not-necessarily-unique/</link>
		<comments>http://facility9.com/2009/11/guids-not-necessarily-unique/#comments</comments>
		<pubDate>Thu, 19 Nov 2009 18:00:07 +0000</pubDate>
		<dc:creator>Jeremiah Peschka</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[syndication]]></category>
		<category><![CDATA[AppDev]]></category>
		<category><![CDATA[GUIDs]]></category>

		<guid isPermaLink="false">http://facility9.com/?p=1169</guid>
		<description><![CDATA[So, you though that GUIDs were supposed to be unique, eh? You aren&#8217;t alone. A lot of people assume that GUIDs are unique. Hell, unique is in the name! On Friday, I had a bug report filed about a UNIQUE constraint violation. Seeing as how I had bigger fish to fry, and this functionality is&#8230;]]></description>
			<content:encoded><![CDATA[<p>So, you though that GUIDs were supposed to be unique, eh? You aren&#8217;t alone. A lot of people assume that GUIDs are unique. Hell, unique is in the name!</p>
<p>On Friday, I had a bug report filed about a UNIQUE constraint violation. Seeing as how I had bigger fish to fry, and this functionality is only rarely used, I put it aside until this morning when I took a closer look at the bug report and the code in question.</p>
<p>The table in question looks something like this:</p>
<pre lang='tsql'>
CREATE TABLE dbo.Widget
(
  WidgetId BIGINT IDENTITY(1,1) NOT NULL,
  -- other columns
  LegacyIdentifier VARCHAR(50) NOT NULL
);
</pre>
<p>Before I get yelled at for a VARCHAR(50) in my database let me assure that this is actually a 50 character random garbage string from our legacy COBOl application. Nobody got lazy and just made a VARCHAR(50)&#8230; this time.</p>
<p>LegacyIdentifier has a unique constraint on it because all of the Widgets need to be unique between the new system and the old system. In order to keep this uniqueness going I thought to myself, &#8220;I know, the U in GUID stands for Unique, I&#8217;ll use that since I&#8217;m not clustering on it.&#8221; I wrote a little piece of code that puts &#8220;New_&#8221; at the beginning of a GUID and then uses that string as the LegacyIdentifier. That was about 11 months ago. On Friday, we received an exception warning that the application had attempted to insert a duplicate value into the LegacyIdentifier column.</p>
<p>What now? Well, I&#8217;m not sure. I&#8217;m going to implement functionality to force the database to keep generating GUIDs and attempting inserts until it actually generates a unique GUID. </p>
<p>This solution doesn&#8217;t make me happy, though, because GUIDs are not unique. A GUID is, in theory, unique. There are, after all, 2^128 possible permutations, so the probability of the same GUID being generated twice are incredibly slim. However, it can happen. (SQL Server uses a V4 GUID algorithm, which is a pseudo-random number and, as such, is not truly random. Newer GUID algorithms use an SHA hash for randomness, but how random is random?)</p>
<p>What are we to do when we need a truly unique identifier? Frankly, I&#8217;m not sure. The thought crossed my mind of generating a random number and casting it to VARCHAR and appending it to the GUID. The statistical likelihood of generating two random values that are the same as a prior value is so astronomically low that I should probably buy lottery tickets if there is ever a collision. As it stands right now, I&#8217;ve opted to retry the insert until it actually succeeds (or for 5 times, after which the user is doomed).</p>
<p>So, there you have it, GUIDs are not guaranteed to be unique and you should plan accordingly.</p>
]]></content:encoded>
			<wfw:commentRss>http://facility9.com/2009/11/guids-not-necessarily-unique/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: basic
Database Caching 2/10 queries in 0.006 seconds using disk: basic
Object Caching 384/394 objects using disk: basic
Content Delivery Network via Amazon Web Services: CloudFront: d1kpgdt94igfig.cloudfront.net

Served from: facility9.com @ 2012-02-11 03:26:19 -->
