~ 8 min read

SQL Server In-Memory Technologies

By: Adam Richardson
Share:

In-memory tables: what are they and how do they work?

In-memory tables are a SQL Server feature that allows you to store data completely in-memory instead of on disk, providing faster access to data. When you create an in-memory table, SQL Server creates a memory-optimized filegroup for the table, and all the data and indexes for the table are stored in memory.

One of the advantages of in-memory tables is that they are lock-free, which means that they can be accessed and modified by multiple sessions concurrently without causing blocking or deadlocking. For example, if you have a high-traffic e-commerce website, you can use in-memory tables to store user shopping carts, which typically have a short lifespan and high concurrency.

To create an in-memory table, you need to specify the MEMORY_OPTIMIZED=ON option in the CREATE TABLE statement. You also need to specify the PRIMARY KEY constraint on the table, as this is required for creating indexes on memory-optimized tables.

Here’s an example of how to create an in-memory table:

CREATE TABLE dbo.CartItem
(
    ItemId INT NOT NULL PRIMARY KEY NONCLUSTERED,
    CartId INT NOT NULL INDEX ix_CartId HASH WITH (BUCKET_COUNT=1024),
    ProductId INT NOT NULL,
    Quantity INT NOT NULL,
    Price DECIMAL(10, 2) NOT NULL
)
WITH (MEMORY_OPTIMIZED=ON, DURABILITY=SCHEMA_ONLY);

In this example, we’re creating an in-memory table called CartItem that stores items in a shopping cart. The table has a Primary Key on the ItemId column and a non-clustered index on the CartId column. We’re also specifying that the table should use schema-only durability, which means that the table and indexes are persisted in memory only, and not written to disk.

In-memory tables can also have secondary indexes, which can improve the performance of queries that join or filter on non-primary key columns. In the example above, we have created a non-clustered hash index on the CartId column, which can be used to quickly look up all items in a specific cart.

In-memory tables also support native stored procedures that run entirely in memory. Stored procedures can take advantage of in-memory tables to perform fast data access and manipulation, which can be particularly useful for data-intensive applications.

Now that you understand the basics of in-memory tables and how they work, you can start experimenting with them yourself to improve the performance of your SQL Server databases.

In-memory indexes: improving performance through hashing

In-memory indexes are an integral part of in-memory technology in SQL Server. They are used to improve the performance of querying and manipulating data stored in memory-optimized tables. There are two types of in-memory indexes: hash and range.

Hash indexes are designed to help you find rows quickly based on the values of a single column. When you create a hash index, SQL Server creates an in-memory hash table that is used to look up rows based on the hash value of the indexed column. For example, if you have an in-memory table that stores a list of books and their authors, you can create a hash index on the author column. This will allow you to quickly find all books written by a particular author.

Here’s an example of how to create a hash index on an in-memory table:

CREATE TABLE dbo.Books
(
    Id INT NOT NULL PRIMARY KEY NONCLUSTERED,
    Title NVARCHAR(50) NOT NULL INDEX ix_Title HASH WITH (BUCKET_COUNT=1024),
    Author NVARCHAR(50) NOT NULL
)
WITH (MEMORY_OPTIMIZED=ON);

In this example, we’re creating an in-memory table called Books that stores information on books, including the title and author. We’ve created a hash index on the Title column using the INDEX option. The BUCKET_COUNT parameter determines how many buckets to use for the hash index, which affects the granularity of the hash values.

When you query an in-memory table with a hash index, SQL Server will use the hash value of the input value to look up the corresponding rows in the hash table. This process is much faster than scanning the entire table looking for matching rows.

One important thing to note about hash indexes is that they do not support range queries or sorting. If you need to query an in-memory table based on a range of values or sort the results, you should consider using a range index instead.

In conclusion, in-memory hash indexes are a powerful tool you can use to improve the performance of data lookups on specific columns within in-memory tables. They are simple to implement and can lead to significant performance gains in data-intensive applications.

In-memory stored procedures: faster query execution

In-memory stored procedures are a type of stored procedure that execute entirely in memory. That means all data access and manipulation is done in memory, without the overhead of locking and I/O operations associated with disk-based tables. This makes in-memory stored procedures much faster than traditional disk-based stored procedures.

In-memory stored procedures can be used to perform a wide range of database operations, such as selecting, updating, inserting, and deleting data. They are particularly useful for data-intensive applications where performance is critical.

Here’s an example of how to create an in-memory stored procedure:

CREATE PROCEDURE dbo.InsertCartItem
    @CartId INT,
    @ProductId INT,
    @Quantity INT,
    @Price DECIMAL(10, 2)
WITH NATIVE_COMPILATION, SCHEMABINDING
AS
BEGIN ATOMIC
    WITH (TRANSACTION ISOLATION LEVEL = SNAPSHOT, LANGUAGE = N'us_english')
    INSERT INTO dbo.CartItem (CartId, ProductId, Quantity, Price)
    VALUES (@CartId, @ProductId, @Quantity, @Price);
    SELECT SCOPE_IDENTITY() AS ItemId;
END

In this example, we’re creating an in-memory stored procedure called InsertCartItem that inserts a new item into an in-memory table called CartItem. The stored procedure takes four parameters, including the CartId, ProductId, Quantity, and Price. We’re using the NATIVE_COMPILATION option to compile the stored procedure to machine code, which makes it much faster than a traditional stored procedure.

Another advantage of in-memory stored procedures is that they support optimistic concurrency control, which allows multiple sessions to access and modify data in memory simultaneously without conflicts. This makes them well-suited for high-concurrency scenarios.

In summary, in-memory stored procedures offer a significant performance boost for data-intensive applications. They are relatively easy to create, and can be used to perform a wide range of database operations quickly and efficiently.

Memory-optimized filegroups: data durability in case of a failure

Memory-optimized filegroups are a key feature of the in-memory technology in SQL Server. They are used to provide data durability in case of a failure, ensuring that your data is not lost or corrupted.

When you create an in-memory table, you specify that it should be part of a memory-optimized filegroup. This creates a filegroup that stores the data and indexes for the table in memory, as well as a checkpoint file on disk that is used to provide durability.

In the event of a system failure, SQL Server will automatically recover any in-memory data that was not saved to disk using the checkpoint file. This ensures that your data remains intact and consistent, even in the event of a power outage or other unexpected failure.

Here’s an example of how to create an in-memory filegroup:

ALTER DATABASE AdventureWorks2019
ADD FILEGROUP InMemoryFilegroup CONTAINS MEMORY_OPTIMIZED_DATA;

ALTER DATABASE AdventureWorks2019
ADD FILE (
    NAME = InMemoryData,
    FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\InMemoryData'
)
TO FILEGROUP InMemoryFilegroup;

In this example, we’re creating an in-memory filegroup called InMemoryFilegroup and adding a data file to it called InMemoryData. This file is located on disk and is used as a checkpoint file in case of a failure.

It’s important to note that memory-optimized filegroups come with a few restrictions. For example, you can’t create a memory-optimized filegroup on a compressed disk or append-only file, and you can’t add a file to a memory-optimized filegroup that is not in memory.

In conclusion, memory-optimized filegroups are a critical component of the in-memory technology in SQL Server. They provide data durability in case of a failure, making in-memory tables a reliable and performant option for data-intensive applications.

Summary

In this article, we’ve explored SQL Server’s in-memory technologies and how they can be used to significantly improve database performance. We’ve covered topics such as in-memory tables, indexes, stored procedures, and memory-optimized filegroups, explaining how they work and providing examples of how to use them.

If you’re a technical developer looking to optimize the performance of your SQL Server databases, in-memory technologies are definitely worth exploring. By leveraging the speed and efficiency of in-memory processing, you can improve the performance of your data-intensive applications, without sacrificing data durability.

My personal advice is to start small and experiment with in-memory technology in a test environment before deploying it to production. This will give you the opportunity to learn how it works and how to optimize it for your specific use case. With a bit of experimentation and optimization, you can achieve significant performance improvements using SQL Server’s in-memory technologies.

Share:
Subscribe to our newsletter

Stay up to date with our latest content - No spam!

Related Posts