This article explains how to manage a memory-optimized filegroup of SQL Database. The memory-optimized filegroup contains the memory-optimized tables and table variables. I have written two articles that explain how we can migrate the disk-based tables to memory-optimized tables. Now, we will learn how to add memory-optimized filegroups in a SQL database. I will cover the following topics in the article.
- Add a memory-optimized filegroup in SQL Database using SQL Server management studio and T-SQL queries
- Create a memory-optimized table in a specific file group
I am using SQL Server 2019 and stackoverflow2010 database. The Stackoverflow database contains tables. First, let us see how we can add a memory-optimized filegroup.
Add memory-optimized filegroup using SSMS
To add a memory-optimized filegroup, Open SSMS and connect to SQL Server database engine 🡪 Expand databases🡪 Right-click on Stackoverflow2010 and select Properties.
A dialog box named Database Properties opens. In the dialog box, select the Filegroups option. In the MEMORY OPTIMIZED FILEGROUP section, click on Add Filegroup.
A row in the filegroup grid-view will be created. Specify the desired name of the memory-optimized filegroup in the Name column of the grid view.
Click OK to create a filegroup. Now, we must add a data file to the FG_MemoryOptimized_Users filegroup. To do that, click on Files in the Database Properties dialog box and click on Add.
A row in the Database files grid-view, a row will be added. Specify the following details:
- Logical Name: Specify desired logical name of the memory-optimized filegroup
- File type: Select FILESTREAM Data in the File type column
- Filegroup: The file group name will be retrieved automatically
- Path: In the Path column, specify the location in which you want to store the memory-optimized tables
Following is the screenshot of the files pan of Database Properties.
Click OK to add a data file. Now, run the following query to populate the details of the memory-optimized filegroup.
1 2 3 4 5 6 7 8 9 10 11 12 |
SELECT [databasefile].NAME AS [FileName], [filegroup].NAME AS [File_Group_Name], [filegroup].type_desc, physical_name [Data File Location], size / 128 AS [Size_in_MB], state_desc [State of FILE], growth [Data file growth] FROM sys.database_files [databasefile] INNER JOIN sys.filegroups [filegroup] ON [databasefile].data_space_id = [filegroup].data_space_id |
Query Output
As you can see in the above image, the filegroup is created. You can view the data files of the memory-optimized filegroup in D:\FG_MemoryOptimized_Users\FG_MemoryOptimized_Users location.
Now, let us create a memory-optimized filegroup in the SQL database using a T-SQL query.
Add memory-optimized filegroups using T-SQL queries
We can use ALTER DATABASE ADD FILEGROUP CONTAINS statement to add a filegroup in the existing SQL Database. The syntax to add a memory-optimized filegroup is as follows:
1 2 3 |
ALTER DATABASE <database_name> ADD FILEGROUP <file_group_name> CONTAINS MEMORY_OPTIMIZED_DATA |
In the syntax:
- database_name: Specify the database name in which you want to add the memory-optimized filegroup
- file_group_name: Specify the desired filegroup name that you want to add to the SQL Database
- CONTAINS: You can specify the type of data you want to store in the filegroup. We want to add memory-optimized data, so specify MEMORY_OPTIMIZED_DATA
In our case, we are adding FG_MemoryOptimized_Posts in a Stackoverflow2010 database. To do that, run the following query statement.
1 2 3 |
ALTER DATABASE [Stackoverflow2010] ADD FILEGROUP [FG_MemoryOptimized_Posts] CONTAINS MEMORY_OPTIMIZED_DATA |
Once the filegroup is added, let us add a datafile to the FG_MemoryOptimized_Posts filegroup. Add data file in the memory-optimized file-group. To add a datafile, we can use ALTER DATABASE ADD DATAFILE TO FILEGROUP statement. The syntax to add a data file is as the following:
1 2 3 |
ALTER DATABASE <database_name> ADD FILE (NAME= <logical_file_name>, FILENAME=<Memory_Optimized_Filegroup_Location> ) TO FILEGROUP <FILE_GROUP_NAME> |
In the syntax,
- database_name: Specify the database name in which you want to add the memory-optimized filegroup
- file_group_name: Specify the desired filegroup name that you want to add to the Database
- ADD FILE: Specify the details of the data file that you want to add to a filegroup. In the ADD FILE keywords, we must specify the following parameters
- Logical_file_name: Specify the desired name of the filegroup
- Memory_Optimized_Filegroup_Location: Specify the location of the directory in which you want to store the memory-optimized data
- FILE_GROUP_NAME: Specify the name of the filegroup in which you want to add the data file. The filegroup name must be specified after the TO FILEGROUP keyword
In our case, we want to add a data file named DF_MemoryOptimized_Posts in the FG_MemoryOptimized_Posts filegroup. The add a data file, run the following command.
1 2 3 |
ALTER DATABASE [Stackoverflow2010] ADD FILE ( NAME = N'DF_MemoryOptimized_Posts', FILENAME = N'D:\FG_MemoryOptimized_Posts' ) TO FILEGROUP [FG_MemoryOptimized_Posts] |
Once the data file is added, run the below query to view the list of filegroups.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
use StackOverflow2010 go SELECT [databasefile].NAME AS [FileName], [filegroup].NAME AS [File_Group_Name], [filegroup].type_desc, physical_name [Data File Location], size / 128 AS [Size_in_MB], state_desc [State of FILE], growth [Data file growth] FROM sys.database_files [databasefile] INNER JOIN sys.filegroups [filegroup] ON [databasefile].data_space_id = [filegroup].data_space_id |
Query Output
As you can see, the data file named DF_MemoryOptimized_Posts has been added in the FG_MemoryOptimized_Posts filegroup.
Create a table in the memory-optimized filegroup
Now, let us create a simple table named want to create a table named tblArchivedPosts in FG_MemoryOptimized_Posts filegroup, the query to create a table will be the following.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
CREATE TABLE [tblArchivedPosts] ( [Id] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY NONCLUSTERED, [AcceptedAnswerId] [int] NULL, [AnswerCount] [int] NULL, [Body] [nvarchar](max) NOT NULL, [ClosedDate] [datetime] NULL, [CommentCount] [int] NULL, [CommunityOwnedDate] [datetime] NULL, [CreationDate] [datetime] NOT NULL, [FavoriteCount] [int] NULL, [LastActivityDate] [datetime] NOT NULL, [LastEditDate] [datetime] NULL, [LastEditorDisplayName] [nvarchar](40) NULL, [LastEditorUserId] [int] NULL, [OwnerUserId] [int] NULL INDEX IX_tblArchivedPosts_OwnerUserId NONCLUSTERED HASH WITH (BUCKET_COUNT=1000000), [ParentId] [int] NULL, [PostTypeId] [int] NOT NULL, [Score] [int] NOT NULL, [Tags] [nvarchar](150) NULL, [Title] [nvarchar](250) NULL, [ViewCount] [int] NOT NULL ) WITH (MEMORY_OPTIMIZED=ON) GO |
Once query executed successfully, let us verify that table is created in FG_MemoryOptimized_Posts file group. To do that, run the following query
1 2 3 4 5 6 7 |
use StackOverflow2010 go select t.object_id [Object ID], t.name [Table Name],f.name [Filegroup Name],durability_desc [Durability],f.type_desc [Filegroup Type] from sys.tables t inner join sys.filegroups f on t.lob_data_space_id=f.data_space_id where lob_data_space_id=3 |
Query Output
As you can see, the tblArchivedPosts table is in the FG_MemoryOptimized_Posts filegroup.
Summary
In this article, we learned how we could create a memory-optimized filegroup in an existing database. We learned the following topics.
- Add multiple memory-optimized filegroups in SQL Database using SQL Server management studio and T-SQL queries.
- Create a memory-optimized table in a specific file group.
- Different ways to identify and change compatibility levels in SQL Server - July 22, 2024
- Copy SQL Databases between Windows 10 and CentOS using SQL Server data tools - October 19, 2022
- Changing the location of FILESTREAM data files in SQL Database - October 14, 2022