In-Memory OLTP - Part 1
Watch the Video
You can watch the short video on In-Memory OLTP on Youtube.
Watch Here
Overview
Introduced in SQL Server 2014 and greatly enhanced with SQL Server 2016 is In-Memory OLTP. In-Memory OLTP is a memory optimized database engine
intergraded with SQL Server.
It's designed to improve performance of data workloads and transaction processing for high data ingestion, contention read/write, and low latency
workloads. High data ingestion workloads are, for example, ETL (Extract, Transform, Load) processes. Read/Write workloads are workloads with enormous read
and write requirements. Lastly, low latency workloads are applications that require specific duration times such as banking transactions.
In-Memory OLTP is available in the following SQL Server Products.
- SQL Server 2014
- SQL Server 2016
- SQL Server vNext CTP1.1
- Azure SQL Database with Premium
Memory-optimized tables
Memory-optimized tables store all their data in memory with no need to put anything in cache from disk. This mechanism eliminates locks and latches and provides
great performance.
Things to note:
- Entire table is stored in memory
- Can store just schema (non-durable data) or data and schema (durable)
- A second copy is maintained on disk if durable is chosen
- Data in the table is read and written to memory
- Default is durable (SCHEMA_AND_DATA)
- Row versioning DOES NOT use tempdb
Durable vs Non-Durable
In memory-optimized tables there are two options that you can choose from when creating a table. The table below is a point
of reference to understanding the difference between Durable and Non-Durable Memory-Optimized Tables
Type |
Description |
Decision Point |
Clause |
Durable |
Transactions are written to memory and also to the transaction log |
- Need to recover data on server failure or failover?
|
DURABILITY=SCHEMA_AND_DATA |
Non-Durable |
Transactions are written to memory but not written to the transaction log |
- Data is stored in another source and recovery is not needed? (ex. ETL process)
- Want to eliminate all I/O operations?
|
DURABILITY=SCHEMA_ONLY |
Querying In-Memory Optimized Tables
Microsoft recommends that the best method to benefit from memory-optimized tables is to use Natively-complied stored procedures.
These stored procedures executed without the need for compilation or interpretation because this is done during compile time and not
run time. Below is an example of a natively complied code on the 'dbo.Employee' table in the 'employeedb' database.
USE [employeedb]
GO
-- SQL Server 2016 SP1 supports CREATE OR ALTER statements
-- Replace it if you are SQL Server 2016 and below with 'CREATE' or 'ALTER'
CREATE OR ALTER PROCEDURE [dbo].[usp_UpdateEmployeeSalary]
WITH NATIVE_COMPILATION,
SCHEMABINDING,
EXECUTE AS OWNER
AS
BEGIN ATOMIC WITH ( TRANSACTION ISOLATION LEVEL=SNAPSHOT,LANGUAGE = N'us_english')
UPDATE [dbo].[Employee]
SET Salary = Salary + (Salary * 0.05)
END;
|
What's New in SQL Server 2016 and In-Memory OLTP
There were some limitation with In-Memory OLTP when first introduced in SQL Server 2014. In SQL Server 2016,
Microsoft invested some time in eliminated some of the limitations. The list below are changes that were made
to In-Memory OLTP in SQL Server 2016.
- Maximum Memory changed from 256 GB to 2TB recommended
- All collations are now supported
- ALTER a memory-optimized table after its created is supported
- Parallel execution plans on memory optimized tables
- TDE - Transparent Data Encryption
- (max) Data Type
- Indexes with NULLable columns
- DML Triggers