Memory-Optimized Tables with In-Memory OLTP
This lesson explains how In-Memory OLTP removes locking and latching, the index and durability options of memory-optimized tables, and where they are available.
Learning outcomes
After completing this lesson, you will be able to:
- Explain how memory-optimized tables handle concurrency.
- Distinguish between hash and nonclustered indexes and between the two durability options.
- Identify the service tiers that support In-Memory OLTP in Azure SQL Database.
How In-Memory OLTP works
A memory-optimized table keeps all its rows in memory. It uses multi-version optimistic concurrency without locks or latches, so high-concurrency inserts and updates do not contend on pages the way disk-based tables do. Write conflicts are detected at commit, and the losing transaction fails and must be retried. Durable tables also log changes and write checkpoint files, so data survives a restart.
Memory-optimized tables need indexes declared at creation or added with ALTER TABLE: hash indexes for point lookups by equality, with a BUCKET_COUNT of about one to two times the number of distinct key values, and nonclustered (range) indexes for range scans and ordered retrieval. Natively compiled stored procedures (WITH NATIVE_COMPILATION, SCHEMABINDING) are compiled to machine code and give the largest gains for short, frequent transactions.
In-Memory OLTP
Durability and table types
DURABILITY = SCHEMA_AND_DATA keeps data across restarts. DURABILITY = SCHEMA_ONLY keeps only the table definition: data is not logged and is lost on restart, which makes it very fast for transient data such as session state, caching or ETL staging. Memory-optimized table types can replace temporary tables and table variables, removing tempdb contention for procedures that pass many rows.
CREATE TABLE dbo.ShoppingCart
(
CartID int IDENTITY(1,1) PRIMARY KEY NONCLUSTERED HASH WITH (BUCKET_COUNT = 1000000),
UserID int NOT NULL,
CreatedUtc datetime2(3) NOT NULL,
INDEX IX_ShoppingCart_User NONCLUSTERED (UserID)
)
WITH (MEMORY_OPTIMIZED = ON, DURABILITY = SCHEMA_AND_DATA);Availability and limitations
In-Memory OLTP is available in all editions of SQL Server since 2016 SP1 and in Azure SQL Managed Instance Business Critical. In Azure SQL Database it requires the Premium (DTU) or Business Critical (vCore) tier, so it is not available in General Purpose, including the free offer. The amount of memory for in-memory data is limited by the service objective. Some features do not apply to memory-optimized tables, including the vector data type, JSON indexes and change event streaming.
Where In-Memory OLTP runs
Session state
A web application stores shopping session state in a disk-based table that receives 20,000 inserts and updates per second, and latch waits on the last page limit throughput. Session data can be lost on a database restart because users would simply sign in again. The database runs in Azure SQL Database Business Critical. What should the developer do?
Create the session table as a memory-optimized table with DURABILITY = SCHEMA_ONLY and a hash index on the session key. Memory-optimized tables remove the page latch contention, and SCHEMA_ONLY avoids logging because the data does not need to survive restarts. SCHEMA_AND_DATA would also remove contention but would keep logging cost that the requirement does not need. A columnstore index does not address singleton writes, and the Business Critical tier makes In-Memory OLTP available.
What happens to the rows of a SCHEMA_ONLY memory-optimized table when the database restarts?
Show answer
They are lost; only the table definition survives, because the data is not logged or checkpointed.
Exam Tips
Remember for the exam
- Transient data that can be lost on restart: DURABILITY = SCHEMA_ONLY.
- Equality lookups: hash index; range scans: nonclustered index.
- Azure SQL Database In-Memory OLTP: Premium or Business Critical only.
- Memory-optimized table types replace table variables without tempdb contention.