The uniqueidentifier datatype is a 16-byte binary value used as a globally uniqueidentifier (GUID). Developers use it in database models for assigning identifiers that must be unique in a network of many computers at many sites. Also, developers exploit this uniqueidentifier data type inappropriately, assuming this is always the best way to enforce uniqueness in their data model.
Because GUID values are randomly generated, it is a bad idea to use them as clustered primary keys.
A non-believer ? We’ll show you how GUID keys introduce fragmentation into your nice database. If you are not familiar with the term fragmentation then think of it as the chaos in a library that results from not re-shelving returned books: long queueing durations, resource locking situations, unhappy customers.
For the demo we need two tables, both have an integer identity and a uniqueidentifier column. In the first table the primary key is clustered on the integer identity column, in the second table the primary key is on the GUID column.
CREATE TABLE tblID (
ID int identity(1, 1) NOT NULL,
GUIDValue uniqueidentifier NOT NULL,
CONSTRAINT PK_tblID PRIMARY KEY CLUSTERED(ID) WITH (FILLFACTOR = 100))
CREATE TABLE tblGUID (
ID int identity(1, 1) NOT NULL,
GUIDValue uniqueidentifier NOT NULL,
CONSTRAINT PK_tblGUID PRIMARY KEY CLUSTERED(GUIDValue) WITH (FILLFACTOR = 100))
Next, we’ll insert 1 million rows in both tables. To do this very fast, we’ll use the following custom function:
/* ——
Function : fnc_NumberRange
Date Created : 19/03/2015 – 15:45
Created By : Ronald Clauw (Kohera)
Description : This function returns a ranged table.
Usage : SELECT Seq FROM dbo.fnc_NumberRange(- 11, 20)
Date Modified :
Modified By :
Description :
—- */
CREATE FUNCTION [dbo].[fnc_NumberRange]
(@Minimum bigint, @Maximum bigint)
RETURNS TABLE
AS
RETURN
WITH
CTE01 AS
(SELECT Value FROM (VALUES (NULL), (NULL), (NULL), (NULL), (NULL), (NULL), (NULL), (NULL), (NULL), (NULL)) AS TenNullValues(Value)), — 10
CTE02 AS
(SELECT x.Value FROM CTE01 AS x CROSS APPLY CTE01 AS y), — 100
CTE03 AS
(SELECT x.Value FROM CTE02 AS x CROSS APPLY CTE02 AS y), — 10.000
CTE04 AS
(SELECT x.Value FROM CTE03 AS x CROSS APPLY CTE03 AS y), — 100.000.000
CTE05 AS
(SELECT x.Value FROM CTE04 AS x CROSS APPLY CTE04 AS y), — 10.000.000.000.000.000
CTE06 AS
(SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS RowNumber FROM CTE05)
SELECT TOP(ABS(@Maximum) – @Minimum + 1) @Minimum + RowNumber – 1 AS Seq
FROM CTE06
Now, we’ll insert 1 million rows in both tables.
INSERT INTO tblID (GUIDValue)
SELECT NEWID() from dbo.fnc_NumberRange(1, 1000000)
INSERT INTO tblGUID (GUIDValue)
SELECT NEWID() from dbo.fnc_NumberRange(1, 1000000)
A lot faster than using a loop, isn’t it ?
In SQL Server Management Studio, we look at the fragmentation percentage on both the clustered indexes (right-click the index in de Object Explorer pane > Properties > Fragmentation) :
tblGUID : 0.67%
tblID : 0.42%
Our function realizes the insertion of the 1 million rows in one batch in an initially empty table: rows get neatly ordered according to the indexed column. Almost no fragmentation there.
Let’s insert a new batch of 10 000 records in each table.
declare @MaxID int
SET @MaxID = (SELECT MAX(ID) FROM tblID)
INSERT INTO tblID (GUIDValue)
SELECT NEWID() from dbo.fnc_NumberRange(@MaxID + 1, @MaxID + 10000)
INSERT INTO tblGUID (GUIDValue)
SELECT NEWID() from dbo.fnc_NumberRange(@MaxID + 1, @MaxID + 10000)
Time to diagnose the health of our tables : let’s check the level of fragmentation again. Bam !
tblGUID : 95.55%
tblID : 0.41%
Fragmentation on the GUID clustered table is skyrocketing. What does it mean : chaos at the library !
You can insert as many batches of additional rows as you want: fragmentation on the GUID clustered index will stay high. Another drawback is that your 16-byte large GUID is part of every other non- clustered index defined on that table. And last but not least, there will be a lot of page splits.
Conclusion : Consider using the IDENTITY property when global uniqueness is not required, or when having a serially incrementing key is preferred. If you need a uniqueidentifier data type for the right reasons, then you can add it to a non-clustered index, but even then heavy fragmentation will occur.
Cookie | Duration | Description |
---|---|---|
ARRAffinity | session | ARRAffinity cookie is set by Azure app service, and allows the service to choose the right instance established by a user to deliver subsequent requests made by that user. |
ARRAffinitySameSite | session | This cookie is set by Windows Azure cloud, and is used for load balancing to make sure the visitor page requests are routed to the same server in any browsing session. |
cookielawinfo-checkbox-advertisement | 1 year | Set by the GDPR Cookie Consent plugin, this cookie records the user consent for the cookies in the "Advertisement" category. |
cookielawinfo-checkbox-analytics | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics". |
cookielawinfo-checkbox-functional | 11 months | The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional". |
cookielawinfo-checkbox-necessary | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary". |
cookielawinfo-checkbox-others | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other. |
cookielawinfo-checkbox-performance | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance". |
CookieLawInfoConsent | 1 year | CookieYes sets this cookie to record the default button state of the corresponding category and the status of CCPA. It works only in coordination with the primary cookie. |
elementor | never | The website's WordPress theme uses this cookie. It allows the website owner to implement or change the website's content in real-time. |
viewed_cookie_policy | 11 months | The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data. |
Cookie | Duration | Description |
---|---|---|
__cf_bm | 30 minutes | Cloudflare set the cookie to support Cloudflare Bot Management. |
pll_language | 1 year | Polylang sets this cookie to remember the language the user selects when returning to the website and get the language information when unavailable in another way. |
Cookie | Duration | Description |
---|---|---|
_ga | 1 year 1 month 4 days | Google Analytics sets this cookie to calculate visitor, session and campaign data and track site usage for the site's analytics report. The cookie stores information anonymously and assigns a randomly generated number to recognise unique visitors. |
_ga_* | 1 year 1 month 4 days | Google Analytics sets this cookie to store and count page views. |
_gat_gtag_UA_* | 1 minute | Google Analytics sets this cookie to store a unique user ID. |
_gid | 1 day | Google Analytics sets this cookie to store information on how visitors use a website while also creating an analytics report of the website's performance. Some of the collected data includes the number of visitors, their source, and the pages they visit anonymously. |
ai_session | 30 minutes | This is a unique anonymous session identifier cookie set by Microsoft Application Insights software to gather statistical usage and telemetry data for apps built on the Azure cloud platform. |
CONSENT | 2 years | YouTube sets this cookie via embedded YouTube videos and registers anonymous statistical data. |
vuid | 1 year 1 month 4 days | Vimeo installs this cookie to collect tracking information by setting a unique ID to embed videos on the website. |
Cookie | Duration | Description |
---|---|---|
ai_user | 1 year | Microsoft Azure sets this cookie as a unique user identifier cookie, enabling counting of the number of users accessing the application over time. |
VISITOR_INFO1_LIVE | 5 months 27 days | YouTube sets this cookie to measure bandwidth, determining whether the user gets the new or old player interface. |
YSC | session | Youtube sets this cookie to track the views of embedded videos on Youtube pages. |
yt-remote-connected-devices | never | YouTube sets this cookie to store the user's video preferences using embedded YouTube videos. |
yt-remote-device-id | never | YouTube sets this cookie to store the user's video preferences using embedded YouTube videos. |
yt.innertube::nextId | never | YouTube sets this cookie to register a unique ID to store data on what videos from YouTube the user has seen. |
yt.innertube::requests | never | YouTube sets this cookie to register a unique ID to store data on what videos from YouTube the user has seen. |
Cookie | Duration | Description |
---|---|---|
WFESessionId | session | No description available. |