Skip to content

Instantly share code, notes, and snippets.

@JerryNixon
Created February 22, 2023 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JerryNixon/9fdfee37af2a3519a52dc233dc8c4baf to your computer and use it in GitHub Desktop.
Save JerryNixon/9fdfee37af2a3519a52dc233dc8c4baf to your computer and use it in GitHub Desktop.
Create Azure SQL DB In-Memory Table
-- configure recommended DB option
ALTER DATABASE CURRENT SET MEMORY_OPTIMIZED_ELEVATE_TO_SNAPSHOT=ON;
GO
-- validate tier
IF (DatabasePropertyEx(DB_Name(), 'IsXTPSupported') = 0)
BEGIN
PRINT 'This database does not support in-mem database.'
RETURN
END
-- so we can run this over and over
DROP TABLE IF EXISTS dbo.Users
DROP TABLE IF EXISTS dbo.InMemUsers
GO
-- create table of record with sample data
WITH [generator] (Id) AS
(
SELECT 1
UNION ALL
SELECT [Id] + 1 FROM generator WHERE [Id] < 1000
)
SELECT
Id
, NEWID() as FirstName
, NEWID() as LastName
INTO dbo.Users
FROM [generator] OPTION (MAXRECURSION 0)
-- create table (non-persist for reading)
CREATE TABLE dbo.InMemUsers
(
Id INT PRIMARY KEY NONCLUSTERED
, FirstName NVARCHAR(50)
, LastName NVARCHAR(50)
) WITH (MEMORY_OPTIMIZED=ON, DURABILITY=SCHEMA_ONLY);
GO
-- copy from table of record into in-mem table
INSERT INTO dbo.InMemUsers
(Id, FirstName, LastName)
SELECT Id, FirstName, LastName from dbo.Users
-- query
SELECT COUNT(*) FROM dbo.InMemUsers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment