Module 5 in Memory OLTP
Module 5 in Memory OLTP
Services
SQL Server
Performance Tuning and
Optimization
Module: In-
Memory OLTP
Module Overview
Microsoft
Services 4
In-Memory OLTP
Meanwhile RAM
cost continues
to drop
100000
10000
US$/GB
1000
100
10
1
8
15
22
29
36
43
50
57
64
71
78
85
92
99
106
113
120
127
134
141
148
155
162
169
176
183
190
197
204
Chart reference
http://www.gotw.ca/publications/concurrency-ddj.htm
Myths
• In-Memory OLTP is like DBCC PINTABLE
• In-Memory databases are new separate
products
• You can use In-Memory OLTP in an existing SQL
Server application with NO changes whatsoever
• Since tables are in memory, the data is not
durable or highly available; data is lost after a
server crash
In-Memory OLTP Pillars
High Efficient Hybrid engine
Custome
Benefits
Frictionless
performance business-logic and integrated
r
scale-up
data operations processing experience
Steadily declining
Stalling CPU clock Many-core
memory price, Non- TCO
rate processors
volatile RAM
In Memory OLTP Architecture
Client App
T1 T2 T3 T1 T2 T3
Persistence uses
Memory-
sequential IO optimized Table
Transaction
Data Filegroup
Log
Filegroup
Scalability
ostress.exe -S. –E
-dAdventureWorks2012
-Q"EXEC Demo.usp_DemoInsertSalesOrders
@use_inmem = <0,1>, @order_count=100000
@usenative=<0,1>
"
–n<varies>
Workload Types
• In-Memory OLTP is a good fit for:
• Performance-critical OLTP (think order processing or trading)
• High data-input rate (nicknamed “Shock Absorber”)
• In-Memory OLTP as components of ETL
• Session state management
• Read scale
• In-Memory OLTP is not a good fit for:
• No permission for code changes
• App depends on locking behavior
• Full data warehousing
• Long-running reporting workload (use Columnstore potentially instead)
• Use a lot of XML manipulation/Full-Text searches
• Heavily parallelized query (2014)
• Constrained on memory
Demonstration:
In-Memory
OLTP Scalability
In-Memory OLTP
Implementation
Create Filegroup FileGroup Container
CREATE DATABASE [Hekaton]
ON PRIMARY
(NAME = N'Hekaton_data', FILENAME = N'C:\Data\Hekaton_data.mdf'),
FILEGROUP [Hekaton_InMemory] CONTAINS MEMORY_OPTIMIZED_DATA
(NAME = N'Hekaton_mem', FILENAME = N'C:\Data\Mem\Hekaton_Lun1')
LOG ON
(NAME = N'Hekaton_log', FILENAME = N'C:\Data\Log\Hekaton_log.ldf')
This table is
memory This table is
optimized durable
Memory Optimized Table Limitations
• Optimized for high-throughput OLTP
• No DML triggers*
• No XML and no CLR data types
• Optimized for in-memory
SQL
• Rows are at most 8060 bytes – no off row data Server
• No Large Object (LOB) types like varchar(max)* 2016
• Scoping limitations
• No FOREIGN KEY and no CHECK constraints*
• No schema changes (ALTER TABLE) – need to drop/recreate table*
• No add/remove index – need to drop/recreate table*
• No Computed Columns
• No Cross-Database Queries
Data Access to Memory Optimized
Tables
Interpreted T-SQL Access Natively Compiled Procs
24
Memory Optimized Table Valued Parameters and
Variables
• Usage Scenarios
• Storing Intermediate Result Sets
• Replacement for table variables
• Passing TVP to Natively compiled procedures
• Advantages
• Always in memory, never spills to disk
• No TempDB utilization
• Data access is efficient
25
Memory Optimized Table Valued
Parameters
CREATE TYPE InMemTVP AS TABLE
(ProductID INT NOT NULL PRIMARY KEY NONCLUSTERED HASH(ProductID)
WITH (BUCKET_COUNT = 100))
WITH (MEMORY_OPTIMIZED = ON)
GO
26
Demonstration:
Creating Memory
Optimized Tables
Demonstration:
Accessing Memory
Optimized Tables
In-Memory OLTP
Native Compilation
Memory Optimized Table Creation
Summary
Database ID
Module loaded
sys.dm_os_loaded_mod
ules
Databa
se ID
Table Object ID
Other Considerations
• Parameter sniffing
• For Natively compiled procedures, UNKNOWN parameter value
assumed
• Still at play for Interpreted T-SQL
• Recompilation for Natively compiled procedures
• No automatic recompilation, requires a drop and recreate
• Recompiled on first execution after restart of the Server or failover
In-Memory OLTP
f(Jane) 0
Array of 1
f(John) 2 f(Prague)
8-byte f(Susan) 3 Hash
Memory 4 Collisions
5 f(Bogota), f(Beijing)
pointers 6
7
Range Index Page Format
Payload
Page Header
Values Offsets Keys
Data File
TS (ins) RowId TableId Row pay load
TS (ins) RowId TableId Row pay load
TS (ins) RowId TableId Row pay load
Checkpoint File Pair
Delta File
TS (ins) RowId TS (del)
TS (ins) RowId TS (del)
TS (ins) RowId TS (del)
Range 500-
Range 300-
Range 100-
Range 200-
Range 400-
data files
• Once a data file is closed,
299
399
499
199
it becomes read-only
• Row deletes are tracked in
delta file
• Files are append only
Memory-optimized Table Filegroup
Data file with rows generated in timestamp IDs of Deleted Rows (height indicates %
range deleted)
Merge Operation
• What is a Merge Operation?
• Merges two or more adjacent data/delta files pairs into 1 pair
• Benefits of Merge
• Reduces storage (i.e. fewer data/delta files) required to store active data rows
• Improves the recovery time as there will be fewer files to load
Memory Management
Memory Problem?
Available
Memory
Memory
Max Server Memory
• Management
• Limit memory consumption using Resource Governor
• DMVs, Perfmon
• Freeing memory is not synchronous in most cases
Resource Governor Integration
• Limits max memory allocation for In-Memory
OLTP
• Prevents performance degradation
Internal pool of regular
%available of
target dedicated pool before
SQL workloads memory OOM notification
• Implementation:
<= 8 GB 70%
• Define dedicated pool for In-Memory
• Bind DB to resource pool defined <= 16GB 75%
via sys.sp_xtp_bind_db_resource_pool
• Set database offline and Online <= 32GB 80%
>= 96 GB 90%
Monitoring
Garbage Collection
Garbage Collection
• Stale Row Versions
• Updates, deletes, and aborted insert operations
create row versions that (eventually) are no
longer visible to any transaction
• Slow down scans of index structures
• Create unused memory that needs to be reclaimed (i.e. Garbage
Collected)
• Garbage Collection (GC)
• Analogous to version store cleanup task for disk-based tables to
support Read Committed Snapshot (RCSI)
• System maintains ‘oldest active transaction’ hint
GC Diagnostics
• DMV’s
• sys.dm_xtp_gc_stats
• sys.dm_xtp_gc_queue_stats
• Performance counters – XTP Garbage Collection
• Extended Events – xtpengine.gc_*
Demonstration:
Garbage Collection
In-Memory OLTP
No TempDB
No locks, no Conflict
latches, detection to
minimal context ensure isolation
Optimistic switches
No blocking No deadlocks
Isolation Levels Supported
First
writer
wins
Validation Errors and Retry Logic
Err Err Message
Number
41302 The current transaction attempted to update a record that has been
updated since the transaction started
41305 The current transaction failed to commit due to a repeatable read
validation failure
41325 The current transaction failed to commit due to a serializable validation
failure
• Native Procedure
• XTPPROC_*
Demonstration:
Query Diagnostics
In-Memory OLTP
Short transactions
(versions)
Short transactions
Resource Governor
Managing Define dedicated pool
(versions)
Force Garbage Collection
Other Considerations
Number of Cores/Sockets
Resource Governor can
CPU 2-4 Socket boxes
help here as well
< 60 cores
OLTP in SQL
Server 2014
/ 2016
© 2015 Microsoft Corporation. All rights reserved.