SQL Server General Index Naming Convention
SQL Server General Index Naming Convention easy to follow:
- PK_ for primary keys
- PK_ Primary Keys, which are logical constructs and not necessarily physical constructs, get tagged with a PK_ prefix. In most systems that I design the PK_ isn’t a clustered index.
- IX_ for non clustered non unique indexes
- IX_ standard single or even multi-column indexes used for seeks or intersection just get prefixed with an IX_ (for IndeX). In cases where I need a composite index (i.e., multiple columns) but still explicitly ‘tolerate’ key/bookmark lookups, I’ll still call my seek/intersection/composite indexes IXes.
- UK_ for unique keys
- UK_ for unique key index
- UX_ for unique indexes
- UX_ for unique index.
- CLIX_ for Clustered Index (non PK)
- CLIX_ (non-PK) Clustered Index name is prefixed with CLIX (for CLustered IndeX)
Additionally, I like to code all Create Indexes in SQL 2012 and above with an if not exists for source code addition so that scripts won't fail if the index should already exist and to throw an error if there is a problem during creation.
If using Enterprise Edition then include ,ONLINE = ON However, when creating an index with online = on, you will still have potential for blocking at the start and end of the online index operation, which means you will still create blocking issues and large indexes should still be created in off-peak hours.
IF NOT EXISTS (
SELECT 1
FROM sys.indexes
WHERE [name] = 'Index_NAME'
AND object_id = OBJECT_ID('dbo.index_name')
)
BEGIN
CREATE NONCLUSTERED INDEX
[IX_table_column]
ON [dbo].[table] ([column1] ASC)
INCLUDE
(
[Column2],
[Column3]
)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON -- ,ONLINE = ON
);
IF @@Error <> 0
THROW 51000, 'Unexpected error', 1;
END;
GO
Comments
Post a Comment