Advertisement
Intermediate SQL Server, In-Memory OLTP (Memory-Optimized Tables)

What are natively compiled stored procedures, and how do they work together with memory optimized tables?

Short answer

A natively compiled stored procedure is compiled directly into machine code when it is created rather than being interpreted at execution time like a regular stored procedure, offering even faster execution specifically for operations against memory optimized tables, though it…

Save SQL Server mock interview
Advertisement
AModel answer
A natively compiled stored procedure is compiled directly into machine code when it is created rather than being interpreted at execution time like a regular stored procedure, offering even faster execution specifically for operations against memory optimized tables, though it comes with some limitations on the T-SQL syntax and features it can use.
CREATE PROCEDURE InsertSession
  @SessionId INT, @UserId INT
WITH NATIVE_COMPILATION, SCHEMABINDING
AS
BEGIN ATOMIC WITH (TRANSACTION ISOLATION LEVEL = SNAPSHOT, LANGUAGE = 'English')
  INSERT INTO SessionData VALUES (@SessionId, @UserId, SYSDATETIME());
END;
Real-world example
A real time bidding platform uses a natively compiled stored procedure to insert session records as fast as technically possible, squeezing out additional performance beyond what a regular stored procedure against a memory optimized table could achieve.
Likely follow-ups
  • What T-SQL features are not supported inside a natively compiled stored procedure?
  • Is native compilation necessary to get the benefits of memory optimized tables?
Related topics