SA0050B : Do not create clustered index on UNIQUEIDENTIFIER columns

Avoid using a clustered index on a UNIQUEIDENTIFIER column to prevent performance issues caused by fragmentation.

Description

In SQL Server, a clustered index organizes the rows of a table based on the index key. Creating a clustered index on a UNIQUEIDENTIFIER column may cause performance degradation because the GUIDs are randomly generated, leading to fragmented data storage, increased page splits, and potentially decreased I/O efficiency.

For example:

SQL
1-- Example of a problematic clustered index
2CREATE TABLE ExampleTable (
3    Id UNIQUEIDENTIFIER PRIMARY KEY CLUSTERED,
4    Name NVARCHAR(100)
5);

This example might cause issues because the Id column as a clustered index can lead to data fragmentation due to its random nature, affecting overall query performance and storage efficiency.

  • Increased page splits due to random inserts can degrade performance.

  • The storage layout may become fragmented, requiring more frequent maintenance operations such as index defragmentation.

How to fix

This section provides strategies to resolve performance issues caused by creating a clustered index on a UNIQUEIDENTIFIER column.

Follow these steps to address the issue:

  1. Evaluate your data model to consider whether a different column might be better suited for the clustered index, such as an integer-based column that increments sequentially.

  2. If you must use a GUID, modify the data generation strategy for the UNIQUEIDENTIFIER column by using the NewSequentialId() function to create sequential identifiers instead of random ones, which can help reduce fragmentation.

  3. Recreate the clustered index on the chosen column to optimize the storage and improve query performance.

For example, using the NewSequentialId() function:

SQL
1CREATE TABLE ExampleTable (
2    Id UNIQUEIDENTIFIER DEFAULT NEWSEQUENTIALID() PRIMARY KEY CLUSTERED,
3    Name NVARCHAR(100)
4);

Scope

The rule has a Batch scope and is applied only on the SQL script.

Parameters

Rule has no parameters.

Remarks

The rule requires Analysis Context. If context is missing, the rule will be skipped during analysis.

Effort To Fix
20 minutes per issue.
Categories

Design Rules, Bugs

Additional Information

There is no additional info for this rule.

Example Test SQL
SQL
 1CREATE TABLE cust
 2(
 3 CustomerID uniqueidentifier PRIMARY KEY DEFAULT newid(),
 4 Company varchar(30) NOT NULL,
 5 ContactName varchar(60) NOT NULL, 
 6 Address varchar(30) NOT NULL, 
 7 City varchar(30) NOT NULL,
 8 StateProvince varchar(10) NULL,
 9 PostalCode varchar(10) NOT NULL, 
10 CountryRegion varchar(20) NOT NULL, 
11 Telephone varchar(15) NOT NULL,
12 Fax varchar(15) NULL
13)
14
15CREATE TABLE cust2
16(
17 CustomerID uniqueidentifier DEFAULT newid(),
18 Company varchar(30) NOT NULL,
19 ContactName varchar(60) NOT NULL, 
20 Address varchar(30) NOT NULL, 
21 City varchar(30) NOT NULL,
22 StateProvince varchar(10) NULL,
23 PostalCode varchar(10) NOT NULL, 
24 CountryRegion varchar(20) NOT NULL, 
25 Telephone varchar(15) NOT NULL,
26 Fax varchar(15) NULL
27)
28
29CREATE CLUSTERED INDEX IX_CustomerID ON cust2 (CustomerID)

Analysis Results
  Message Line Column
1 SA0050B : Do not create clustered index on UNIQUEIDENTIFIER columns. 3 1
2 SA0050B : Do not create clustered index on UNIQUEIDENTIFIER columns. 29 47
See Also

Other Resources