Implement memory-optimized (in-memory) tables
This article explains how memory-optimized (in-memory) tables improve performance, where they are available in Azure SQL Database, and how durability works, including delayed durability and non-durable tables. It then covers hash and memory-optimized nonclustered indexes, the disadvantages of in-memory tables and when to use them.
What memory-optimized tables are
Memory-optimized tables, also known as in-memory tables, are stored in the main memory. This improves performance, as data access and transaction execution are more efficient. One company improved performance by up to 30 times; another by 2 times.
Natively compiled stored procedures
The best performance is achieved using natively compiled stored procedures.
- These are compiled when they are created, instead of during the first execution.
- They cannot use the TOP expression if they return more than 8,000 rows.
To create a natively compiled stored procedure, use CREATE PROCEDURE … WITH NATIVE_COMPILATION.
Availability in Azure SQL Database
For Azure SQL Database, memory-optimized tables:
- are available in the Premium and Business Critical service tiers; and
- have some functionality available in the Hyperscale service tier.
Creating a memory-optimized table
To create an in-memory table, add the following at the end of the table definition:
WITH (MEMORY_OPTIMIZED = ON)
The default value is:
WITH (MEMORY_OPTIMIZED = ON, DURABILITY = SCHEMA_AND_DATA)
Durability
By default, all transactions written to memory are simultaneously written to disk. If the server restarts, the memory-optimized version is restored from disk.
Delayed durability
If you want greater performance, you can use memory-optimized tables with transaction durability delayed.
- Transactions which have been committed to the in-memory table are soon after committed to the disk version.
- This means that, if there is a crash or failover, some transactions may be lost.
To do this, run:
ALTER DATABASE DatabaseName
SET DELAYED_DURABILITY = ALLOWED;
The opposite is SET DELAYED_DURABILITY = DISABLED.
Non-durable tables
You can also have non-durable memory-optimized tables.
- Transactions are not logged or persisted on disk.
- If there is a crash or failover, the data will be lost.
To create such a table, use:
WITH (MEMORY_OPTIMIZED = ON, DURABILITY = SCHEMA_ONLY)
| Setting | Written to disk | After a crash or failover |
|---|---|---|
| DURABILITY = SCHEMA_AND_DATA (default) | Simultaneously | Restored from disk |
| DELAYED_DURABILITY = ALLOWED | Soon after the commit | Some transactions may be lost |
| DURABILITY = SCHEMA_ONLY | Not logged or persisted | The data will be lost |
Indexes on memory-optimized tables
Indexes on memory-optimized tables are not logged on disk.
Important
You cannot use clustered indexes. Primary keys need to be defined with nonclustered indexes, using PRIMARY KEY NONCLUSTERED.
Indexes are created using either a hash index or a memory-optimized nonclustered index.
Hash indexes
A hash index is an array of pointers. It has:
- excellent performance when the WHERE clause uses an exact value (a point lookup) for each column in the hash key;
- poor performance when the WHERE clause uses a range of values;
- poor performance when values are not specified for each column in the key; and
- fine performance for retrieving all table rows.
To create one, use:
ALTER TABLE NameOfTable
ADD INDEX NameOfIndex [UNIQUE] HASH (ColumnNames) WITH (BUCKET_COUNT = 64);
The bucket count should be 1 to 2 times the number of distinct values in the index key.
- Performance is still good with 10 times the actual number of key values.
- Overestimating is generally better than underestimating.
Memory-optimized nonclustered indexes
A memory-optimized nonclustered index is best for:
- ORDER BY clauses on the indexed column(s), in the order of the index (ASC or DESC);
- queries where only the first column(s) of the index are tested; and
- WHERE queries which do not use =, such as <> or >=.
It is also fine for retrieving all table rows. You create it in the same way as a normal nonclustered index.
Comparing the index types
Rows in memory-optimized tables are versioned. Memory-optimized nonclustered indexes have better performance than disk-based versions. For point lookups (using an = sign in the WHERE clause), memory-optimized hash indexes have even better performance.
Exam tip
Choose between the two index types by the WHERE clause. A hash index is excellent for point lookups (using the = sign in the WHERE) which give an exact value for every column in the hash key, but poor for ranges. A memory-optimized nonclustered index suits ranges, ORDER BY in the index order, and tests on only the first column(s).
Disadvantages of in-memory tables
The disadvantages of in-memory tables are:
- they use memory which is not available to other objects, which can reduce the performance of those objects; and
- you cannot use clustered indexes.
When to use in-memory tables
You should consider using in-memory tables:
- when you have large volumes of transactions, and you need low latency for individual transactions. This can include financial trading, sports betting, mobile gaming and advert delivery;
- when you ingest large amounts of data from multiple sources at the same time. This can include inserting sensor readings or events, and updating data from multiple sources;
- when you need to maintain session state and cache data (for example, in an ASP.NET application);
- for temporary tables, table variables and table-valued parameters; and
- when loading data into a staging table, transforming the data, and loading it into final tables (ETL – extract, transform, load).
Continue studying DP-800
This article covers Implement memory-optimized (in-memory) tables, part of the Design and implement database objects section of the DP-800 study guide.
Related articles
That worth reading, thank you for the post.