SQL Server In-Memory OLTP (MS Docs article) is a powerful engine integrated into the SQL Server Engine (version 2014 and later), optimized for Online Transaction Processing (OLTP). This article helps you getting started with SQL Server In-Memory OLTP and it is the first part in a series of articles focusing on SQL Server In-Memory OLTP.
An Introduction to SQL Server In-Memory OLTP
In-Memory OLTP (codenamed “Hekaton”) is a powerful engine which is fully integrated into SQL Server’s Database Engine and introduces new data structures for optimizing the performance of OLTP workloads. So, you can have memory-optimized tables, natively-compiled stored procedures, memory-optimized table variables, etc.
In-Memory OLTP offers a significant performance boost when it comes to processing large amounts of information, especially in data environments with high levels of concurrency.
In this series of articles, we will see in plain words, how we can take advantage of this technology and start using it for boosting performance of heavy workloads.
In this first article, we are going to see how easy is to create an In-Memory OLTP-enabled database and memory-optimized tables.
Learn more about In-Memory OLTP in SQL Server – Enroll to the Course!
In this course, you will learn all about SQL Server’s In-Memory Database Processing Engine also known as In-Memory OLTP. To this end, you will learn how to enable In-Memory OLTP in SQL Server, what memory-optimized tables and natively-compiled stored procedures are, as well as, how to boost the performance of your data processes using this powerful SQL Server feature.
(Lifetime Access/ Live Demos / Downloadable Resources and more!)
How to Create a SQL Server In-Memory OLTP-Enabled Database
OK, enough talking, let’s see some code! The below T-SQL script creates a new In-Memory OLTP-enabled database.
--create database
CREATE DATABASE InMemOLTPDB;
GO
--use the database
USE InMemOLTPDB;
GO
--add memory-optimized file group to the database
ALTER DATABASE InMemOLTPDB ADD FILEGROUP InMemOLTPDB_mofg CONTAINS MEMORY_OPTIMIZED_DATA;
--add file for memory-optimized objects
--you can set any path you like - in this example: c:memoptdata
--if the directory does not exist you will need to create it
ALTER DATABASE InMemOLTPDB ADD FILE (name='InMemOLTPDB_mofg1', filename='C:memoptdataInMemOLTPDB_mofg1') TO FILEGROUP InMemOLTPDB_mofg;
--set the isolation level for memory-optimized tables to SNAPSHOT
ALTER DATABASE InMemOLTPDB SET MEMORY_OPTIMIZED_ELEVATE_TO_SNAPSHOT=ON;
GO
How to Create Memory-Optimized Tables in SQL Server
The next step, is to create memory-optimized tables.
When creating memory-optimized tables you have two options:
Option 1: Create a durable memory-optimized table which means that in the case of server crash or failover the data will be available as they will be recovered from transaction logs.
Option 2: Create a non-durable table which means that in the case of a server crash or failover the data in the table will be lost.
The below script creates a sample durable memory-optimized table:
--Memory-Optimized Table: Durable
CREATE TABLE [dbo].[Person_Durable]
(
ID INT NOT NULL PRIMARY KEY NONCLUSTERED HASH WITH (BUCKET_COUNT = 1000),
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50) NOT NULL,
Remarks VARCHAR(50) NOT NULL,
)WITH (MEMORY_OPTIMIZED = ON, DURABILITY = SCHEMA_AND_DATA);
GO
The below script creates a sample non-durable memory-optimized table:
--Memory-Optimized Table: Non-Durable
CREATE TABLE [dbo].[Person_Non_Durable]
(
ID INT NOT NULL PRIMARY KEY NONCLUSTERED HASH WITH (BUCKET_COUNT = 1000),
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50) NOT NULL,
Remarks VARCHAR(50) NOT NULL,
)WITH (MEMORY_OPTIMIZED = ON, DURABILITY = SCHEMA_ONLY);
GO
Stay tuned as in the next article we are going to run some simple experiments with In-Memory OLTP and get an indication on the performance benefits.
Video: What are Memory-Optimized Tables in SQL Server?
Our Online Course Video Preview: Boost SQL Server Database Performance with In Memory OLTP
Artemakis Artemiou is a seasoned Senior Database and AI/Automation Architect with over 20 years of expertise in the IT industry. As a Certified Database, Cloud, and AI professional, he has been recognized as a thought leader, earning the prestigious Microsoft Data Platform MVP title for nine consecutive years (2009-2018). Driven by a passion for simplifying complex topics, Artemakis shares his expertise through articles, online courses, and speaking engagements. He empowers professionals around the globe to excel in Databases, Cloud, AI, Automation, and Software Development. Committed to innovation and education, Artemakis strives to make technology accessible and impactful for everyone.
Views:3,928
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent. Read More
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
Cookie
Duration
Description
cookielawinfo-checkbox-analytics
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional
11 months
The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy
11 months
The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.