What is the IDENTITY_CACHE Option in SQL Server?
The IDENTITY_CACHE option, is a new database scoped configuration option in SQL Server 2017 and Azure SQL Database. This option is set to ON by default. The concept of identity caching in SQL Server is that the Database Engine stores in cache a series of values for identity columns and use them whenever needed in order to be faster. However, in the case where an identity column is being populated with values and an unexpected SQL Server restart or failover takes place, then after the process takes place again, there will be a gap between the identity column values before the restart/failover and the values right after the restart/failover.
If however, you disable this option by setting it to OFF, you can avoid gaps in identity column values in cases of unexpected SQL Server restarts or fail-overs.
This is similar to the Trace Flag 272 setting with the difference that the IDENTITY_CACHE option is within the database scope and not within the server scope.
A Simple Example
Let’s see an example that will help you better understand this very useful new feature.
First, let’s create a demo database and a table with an identity column named “id”.
CREATE DATABASE DemoDB; GO USE DemoDB; GO CREATE TABLE tblIDTest( id INT IDENTITY(1,1) PRIMARY KEY, code varchar(20) ); GO
Running the Example with IDENTITY_CACHE Enabled (Default)
Now, let’s insert 5 records and check the table contents:
--Insert some sample data INSERT INTO tblIDTest (code) VALUES ('code1'),('code2'),('code3'),('code4'),('code5'); GO --Check identity values SELECT * FROM tblIDTest; GO
Next, we try to insert another 5 records but before commit them we simulate an unexpected server shutdown:
BEGIN TRAN --Insert some sample data INSERT INTO tblIDTest (code) VALUES ('code6'),('code7'),('code8'),('code9'),('code10'); GO --You will have to execute 2 times the SHUTDOWN command for this example SHUTDOWN WITH NOWAIT; GO
After restarting the instance we try again the insert the above 5 records:
--Insert some sample data INSERT INTO tblIDTest (code) VALUES ('code6'),('code7'),('code8'),('code9'),('code10'); GO --Check identity values SELECT * FROM tblIDTest; GO
As you can see in the results below, there is a gap in the identity values:
Learn more tips like this! Enroll to our Online Course!
Check our online course titled “Essential SQL Server Development Tips for SQL Developers” (special limited-time discount included in link).
Sharpen your SQL Server database programming skills via a large set of tips on T-SQL and database development techniques. The course, among other, features over than 30 live demonstrations!
Running the Example with IDENTITY_CACHE Disabled
Now, let’s re-run the scenario but right before we start, let’s disable the IDENTITY_CACHE option for our demo database:
USE DemoDB; GO ALTER DATABASE SCOPED CONFIGURATION SET IDENTITY_CACHE = OFF; GO
OK, here we go again. Let’s truncate the sample table and insert again the first 5 records and check the results:
So far, so good. All 5 records have correct identity values.
Recommended Source of Knowledge: My new eBook titled “Developing with SQL Server“
Now, like in the first example, let’s try to insert another 5 records but before commit them we restart the SQL Server instance:
BEGIN TRAN --Insert some sample data INSERT INTO tblIDTest (code) VALUES ('code6'),('code7'),('code8'),('code9'),('code10'); GO --You will have to execute 2 times the SHUTDOWN command for this example SHUTDOWN WITH NOWAIT; GO
Now, right after the simulated “unexpected server shutdown”, let’s start SQL server and try again to insert the second set of 5 records and check the identity values to see if there are any gaps:
As you can see, this time, the sequence of identity values is continuous, even though there was an unexpected server shutdown.
This was an example of the IDENTITY_CACHE option’s usage in SQL Server 2017.
I hope you enjoyed the article!
More new features of SQL Server 2017 will be covered in subsequent articles.
Learn useful SQL Server Administration tips!
Check our online course on Udemy titled “Essential SQL Server Administration Tips” (discount included in link).
As a SQL Server DBA, you need to maintain secure, performant and efficient SQL Server databases and our course can definitely help you with that.
Featured Online Courses:
- AI Essentials: A Beginner’s Guide to Artificial Intelligence
- SQL Server 2022: What’s New – New and Enhanced Features
- Working with Python on Windows and SQL Server Databases
- Introduction to Azure Database for MySQL
- Boost SQL Server Database Performance with In-Memory OLTP
- Introduction to Azure SQL Database for Beginners
- Essential SQL Server Administration Tips
- SQL Server Fundamentals – SQL Database for Beginners
- Essential SQL Server Development Tips for SQL Developers
- Introduction to Computer Programming for Beginners
- .NET Programming for Beginners – Windows Forms with C#
- SQL Server 2019: What’s New – New and Enhanced Features
- Entity Framework: Getting Started – Complete Beginners Guide
- Data Management for Beginners – Main Principles
- A Guide on How to Start and Monetize a Successful Blog
Check our Related SQL Server Development Articles:
- How to Import and Export Data in SQL Server
- The set identity_insert Command in SQL Server
- The Import Flat File Wizard in SSMS v17.3
- Listing all Tables of a Linked Server’s Database
- How to Import and Export Unstructured Data in SQL Server – FileTables
- How to Import and Export Unstructured Data in SQL Server – FILESTREAM
- How to Import and Export Unstructured Data in SQL Server – The IMAGE Datatype
- Sequence Objects in SQL Server 2012 and later
- …more
Subscribe to our newsletter and stay up to date!
Subscribe to our YouTube channel (SQLNetHub TV)
Easily generate snippets with Snippets Generator!
Secure your databases using DBA Security Advisor!
Check our latest software releases!
Check our eBooks!
Rate this article:
Reference: SQLNetHub (https://www.sqlnethub.com)
© SQLNetHub
Artemakis Artemiou, a distinguished Senior Database and Software Architect, brings over 20 years of expertise to the IT industry. A Certified Database, Cloud, and AI professional, he earned the Microsoft Data Platform MVP title for nine consecutive years (2009-2018). As the founder of SQLNetHub and GnoelixiAI Hub, Artemakis is dedicated to sharing his knowledge and democratizing education on various fields such as: Databases, Cloud, AI, and Software Development. His commitment to simplicity and knowledge sharing defines his impactful presence in the tech community.