SQL 2014 In-Memory OLTP
Amanda Ford
Adebimpe Alabi
Microsoft
©2014 Microsoft Corporation. All rights reserved.
Conditions and Terms of Use
Microsoft Confidential
This training package is proprietary and confidential, and is intended only for uses described in the training materials. Content and software is provided to you
under a Non-Disclosure Agreement and cannot be distributed. Copying or disclosing all or any portion of the content and/or software included in such packages is
strictly prohibited.
The contents of this package are for informational and training purposes only and are provided "as is" without warranty of any kind, whether express or implied,
including but not limited to the implied warranties of merchantability, fitness for a particular purpose, and non-infringement.
Training package content, including URLs and other Internet website references, is subject to change without notice. Because Microsoft must respond to changing
market conditions, the content should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any
information presented after the date of publication. Unless otherwise noted, the companies, organizations, products, domain names, e-mail addresses, logos,
people, places, and events depicted herein are fictitious, and no association with any real company, organization, product, domain name, e-mail address, logo,
person, place, or event is intended or should be inferred.
Copyright and Trademarks
© 2014 Microsoft Corporation. All rights reserved.
Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject matter in this document. Except as
expressly provided in written license agreement from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks,
copyrights, or other intellectual property.
Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under copyright, no part of this document may be
reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or
otherwise), or for any purpose, without the express written permission of Microsoft Corporation.
For more information, see Use of Microsoft Copyrighted Content at
http://www.microsoft.com/about/legal/permissions/
Microsoft®, Internet Explorer®, Outlook®, OneDrive®, Windows Vista®, Zune®, Xbox 360®, DirectX®, Windows Server® and Windows® are either registered
trademarks or trademarks of Microsoft Corporation in the United States and/or other countries. Other Microsoft products mentioned herein may be either
registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries. All other trademarks are property of their respective
owners.
Agenda
•
•
•
•
In Memory OLTP Overview
Memory Optimized Tables
Native Compiled Stored Procedures
Migration to In-Memory Objects and
things to consider…
Microsoft Confidential
SQL 2014 In-Memory OLTP
In Memory OLTP Overview
Microsoft Confidential
Drivers
Architectural Pillars
Customer
Benefits
In-Memory OLTP Summary
High performance data
operations
Efficient business-logic
processing
Frictionless scale-up
Hybrid engine and
integrated experience
Main-Memory
Optimized
T-SQL Compiled to
Machine Code
High Concurrency
SQL Server Integration
• T-SQL compiled to machine
code via C code generator
and VC
• Invoking a procedure is just
a DLL entry-point
• Aggressive optimizations at
compile-time
• Multi-version optimistic
concurrency control with full
ACID support
• Core engine uses lock-free
algorithms
• No lock manager, latches or
spinlocks
• Optimized for in-memory
data
• Indexes (hash and range)
exist only in memory
• No buffer pool, B-trees
• Stream-based storage
Business
Hardware trends
Steadily declining memory
price, Non-volatile RAM
Stalling CPU clock rate
Microsoft Confidential
• Same manageability,
administration &
development experience
• Integrated queries &
transactions
• Integrated HA and
backup/restore
Many-core processors
TCO
5
Myths
1.
2.
3.
4.
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 app
with NO changes whatsoever
Since tables are in memory, the data is not durable or highly
available; data is lost after a server crash
Microsoft Confidential
6
In Memory OLTP Architecture
Client App
TDS (Client Server Communications) Handler and Session Management
Key
Parser,
Catalog,
Optimizer
Existing SQL
Component
Natively Compiled
SPs and Schema
Native
Compiler
T-SQL Query Execution
In-mem OLTP
Component
Query
Interop
T1
T2
T3
Tables
Generated
.dll
Indexes
Memory Optimized Tables & Indexes
T1
Memory Optimized Data Filegroup
Buffer Pool for Tables & Indexes
SQL Server.exe
T2
T3
Transaction Log
Microsoft Confidential
T1
T2
T3
Data Filegroup
7
Performance Gains
No improvements in:
communication stack,
parameter passing,
result set generation
10-30x more efficient
Removes lock and latch
contention
Client App
TDS (Client Server communication) Handler and Session Management
Native
Compiler
Natively
Compiled SPs
and Schema
Engine for
Memory_optimized Tables
& Indexes
Reduced log contention
Memory-optimized
Table Filegroup
Parser,
Catalog,
Optimizer
Query
Interop
T-SQL Query Execution
Key
Existing SQL
Component
In-mem OLTP
Component
Generated
.dll
Buffer Pool for Tables &
Indexes
SQL Server.exe
Transaction Log
Microsoft Confidential
Data Filegroup
8
SQL 2014 In-Memory OLTP
In Memory Storage
Microsoft Confidential
FileGroup Container
Create Filegroup
CREATE DATABASE [Hekaton]
ON PRIMARY
(NAME = N'Hekaton_data', FILENAME = N'C:\Data\Data\Hekaton_data.mdf'),
FILEGROUP [Hekaton_InMemory] CONTAINS MEMORY_OPTIMIZED_DATA
(NAME = N'Hekaton_mem', FILENAME = N'C:\Data\Mem\Hekaton_Lun1.mdf')
LOG ON
(NAME = N'Hekaton_log', FILENAME = N'C:\Data\Log\Hekaton_log.ldf')
ALTER DATABASE [Hekaton]
ADD FILE (NAME = N'Hekaton_mem', FILENAME =
N'C:\Data\Mem\Hekaton_Lun2.mdf')
TO FILEGROUP [Hekaton_InMemory]
Microsoft Confidential
10
Create Memory Optimized Table
Hash Index
CREATE TABLE [Customer] (
[CustomerID] INT NOT NULL
PRIMARY KEY NONCLUSTERED HASH WITH (BUCKET_COUNT = 1000000),
[AddressID] INT NOT NULL INDEX [IxName] HASH WITH (BUCKET_COUNT =
1000000),
Collation BIN2
[LName] NVARCHAR(250) COLLATE Latin1_General_100_BIN2 NOT NULL
INDEX [IXLName] NONCLUSTERED (LName)
)
Range Index
WITH (MEMORY_OPTIMIZED = ON, DURABILITY = SCHEMA_AND_DATA);
This table is
memory optimized
This table is durable
Microsoft Confidential
11
In-Memory Row Format
Row Header
•
•
•
•
Payload (Actual column data)
Begin Ts
End Ts
StmtID
IdsLinkCount
8 bytes
8 bytes
4 bytes
2 bytes + 2
for padding
Index1 ptr
Index2 ptr
8 bytes * Number of Indexes
Begin/End timestamp determines row’s version validity and visibility
No concept of data pages, only rows exist
Row size limited to 8060 bytes (@table create time) to allow data to be moved to disk-based tables
Not every SQL table schema is supported (Ex: LOB and SqlVariant)
Microsoft Confidential
12
Memory Optimized Table Limitations
Optimized for high-throughput OLTP
• No XML and no CLR data types
Optimized for in-memory
• Rows are at most 8060 bytes – no off row data
• No Large Object (LOB) types like varchar(max)
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
Microsoft Confidential
13
Hash Indexes
Hash index with (bucket_count=8):
Hash function f:
• Maps values to buckets
• Built into the system
Hash index mapping:
Array of
8-byte
Memory
pointers
f(Claire)
f(John)
f(Susan)
0
1
2
3
4
5
6
7
f(Mike)
f(David)
Microsoft Confidential
Hash
Collisions
14
Hash Index Traversal
Hash index
mapping:
0
1
2
3
4
5
6
7
Chain
Ptrs
Name
John
Mike
Microsoft Confidential
15
SQL 2014 In-Memory OLTP
Demo: The bucket count
Microsoft Confidential
Memory Optimized Table Insert
Timestamps
Hash index
on Name
f(John)
50, ∞
Chain ptrs
Name
Jane
City
Prague
Hash index
on City
f(Prague)
100, ∞
John
Prague
90, ∞
Susan
Bogota
T100: INSERT (John, Prague)
Microsoft Confidential
17
Memory Optimized Table Delete
Timestamps
Hash index
on Name
50, ∞
Chain ptrs
Name
Jane
City
Prague
100, ∞
John
Prague
90,150
∞
90,
Susan
Bogota
T150: DELETE (Susan, Bogota)
Microsoft Confidential
Hash index
on City
18
Memory Optimized Table Update
Timestamps
Hash index
on Name
f(John)
Chain ptrs
50, ∞
100,200
∞
100,
Name
Jane
John
City
Prague
Hash index
on City
Prague
f(Beijing)
200, ∞
90, 150
John
Susan
Beijing
Bogota
T200: UPDATE (John, Prague) to (John, Beijing)
Microsoft Confidential
19
Garbage Collection
Timestamps
Hash index
on Name
f(Jane)
f(John)
Chain ptrs
50, ∞
Name
Jane
City
Prague
Hash index
on City
f(Prague)
100, 200
John
Prague
f(Beijing)
200, ∞
90, 150
John
Susan
T250: Garbage collection
Beijing
Bogota
Microsoft Confidential
20
In-Memory OLTP Structures summary
Rows
• Row structure is optimized for memory access
• There are no Pages
• Rows are versioned and there are no in-place updates
• Fully durable by default (but they don’t have to be)
Indexes
• There is no clustered index, only non-clustered indexes
• Indexes point to rows, access to rows is via an index
• Indexes do not exist on disk, only in memory, recreated during recovery
• Hash indexes for point lookups
• Range indexes for ordered scans and Range Scans
•
Microsoft Confidential
22
Memory Issues?
Max Server Memory
Available
Memory
Memory
Optimized
Tables
Memory Internal
Structures
Buffer Pool
Memory
Optimized
Tables
Memory Internal
Structures
Buffer Pool
Memory
Optimized
Tables
Memory Internal
Structures
Buffer Pool
Microsoft Confidential
Memory
Optimized
Tables
Memory Internal
Structures
Buffer Pool
23
Memory Considerations
Scenario
Symptom
Microsoft Confidential
Diagnosis
Solution
24
SQL 2014 In-Memory OLTP
On Disk Storage
Microsoft Confidential
Durability
Memory optimized tables can be durable or non-durable
• Non-durable tables are ideal for transient data
• Default is durable
CREATE TABLE [dbo].[SalesOrder_MemOpt]
(
[Order_ID] INT NOT NULL,
[Order_Date] DATETIME NOT NULL,
[Amount] FLOAT NOT NULL,
CONSTRAINT PK_SalesOrderID PRIMARY KEY NONCLUSTERED HASH (Order_ID)
)
WITH (MEMORY_OPTIMIZED = ON, DURABILITY = SCHEMA_AND_DATA);
•
•
•
•
Single memory optimized filegroup
Sequential IO pattern (no random IO)
Filegroup can have multiple containers to aid in parallel recovery
Recovery depends on IO speed – recommend SSD or Fast SAS drives
Microsoft Confidential
26
Storage
Filestream container is the underlying storage mechanism
Data files
•
•
•
~128MB in size, write 256KB chunks at a time ( 16MB if Server has < 16GB RAM)
Stores only the inserted rows (i.e. table content)
Chronologically organized streams of row versions
Delta files
•
•
File size is not constant, write 4KB chunks at a time
Stores IDs of deleted rows
Microsoft Confidential
27
Data and Delta Files
0
100
Checkpoint File Pair
Data File
Transaction Timestamp Range
TS (ins)
TS (ins)
TS (ins)
RowId
RowId
RowId
TableId
TableId
TableId
Row pay load
Row pay load
Row pay load
Data file contains rows inserted
within a given transaction range
Delta File
TS (ins)
TS (ins)
TS (ins)
RowId
RowId
RowId
TS (del)
TS (del)
TS (del)
Delta file contains deleted rows
within a given transaction range
Microsoft Confidential
28
Merge Operation
Memory-optimized data Filegroup
Data file with rows generated in
timestamp range
Range 500-600
Range 400-500
Merge
200-400
Range 200-400
Merge Operation
300-399
Range 300-400
Range 200-300
200-299
Files as of Time 600
Range 100-200
Range 400-500
Range 300-400
Range 200-300
Range 100-200
Files as of Time 500
Memory-optimized data Filegroup
IDs of Deleted Rows (height
indicates % deleted)
Microsoft Confidential
Files Under Merge
Deleted Files
Demonstration: Data and
Delta State Transitions
SQL 2014 In-Memory OLTP
Native Compilation
Microsoft Confidential
Natively Compiled Procedures
CREATE PROCEDURE [dbo].[InsertOrder](@id INT, @date DATETIME)
WITH
This proc is natively compiled
NATIVE_COMPILATION,
SCHEMABINDING,
Native procs must be schema-bound
EXECUTE AS OWNER
Execution context is required
AS
BEGIN ATOMIC
Atomic blocks
WITH
• Create a transaction if
(TRANSACTION ISOLATION LEVEL = SNAPSHOT,
there is none
LANGUAGE = N'us_english')
• Otherwise, create a
-- Insert T-SQL here…
savepoint
END
Session settings are fixed at
create time
Microsoft Confidential
32
Data Access to Memory Optimized Tables
Interpreted T-SQL Access
• Access both memory- and diskbased tables
• Less performant
• Virtually full T-SQL surface
• When to use
Natively Compiled Procs
• Access only memory optimized
tables
• Maximum performance
• Limited T-SQL surface area
• When to use
• Ad hoc queries
• Reporting-style queries
• Speeding up app migration
• OLTP-style operations
• Optimize performance critical business
logic
• More the logic embedded, better the
performance improvement
Microsoft Confidential
33
Native Compilation Process
Compile T-SQL statements and table data access logic into machine code
DLL
sqlservr.exe
Create
table/proc/variable
In-Memory
Compiler
SQL Engine
Parser/Algebrizer/
Metadata/Query Optimizer
Optimized query tree /
metadata
Microsoft Confidential
.c
file
VC compiler/linker
code generation
Natively Compiled Procedures Restrictions
Serial execution plan
In-Memory tables restriction  No Parallel Plan!
Not all operators/TSQLs are supported
Only Nested Loop join, no TSQL MERGE or EXISTS, cursors, MARS, nested queries
Transaction isolation level
SNAPSHOT, REPEATABLEREAD, and SERIALIZABLE
READ COMMITTED and READ UNCOMMITED is not supported
Collation
Comparison, sorting, and other operations on character strings require BIN2 collations
Microsoft Confidential
Natively Compiled Procedures Restrictions
Cannot access disk-based tables
No TEMPDB! Use In-Memory Table variables
No parameter sniffing
Optimize for UNKNOWN
No automatic recompile on statistics changes
Need to stop & start SQL or drop & create procedure
Microsoft Confidential
SQL 2014 In-Memory OLTP
Migration
Microsoft Confidential
Iterative Methodology
Establish perf
baseline
Run AMR
reports
Setup data
collection
(MDW)
Configure data
collection
Run workload
Migrate tables
Migrate stored
procedures
Collect perf
data
Compare to
baseline
Microsoft Confidential
38
AMR Toolset Data Collectors
Management data warehouse “New collectors” assist in analyzing migration
candidates
Can be run remotely
Microsoft Confidential
39
Table Usage Analysis
Microsoft Confidential
40
Stored Procedure Usage Analysis
Primarily looks at sys.dm_exec_procedure_stats
Drill down to referencing objects
41
Microsoft Confidential
Q&A
Microsoft Confidential
Microsoft Confidential
43