IN this article, we are going to learn how we can manage the filegroup of SQL Database in SQL Server. The SQL Server has four filegroups.
- Primary Filegroup: The primary filegroup is a default filegroup. When we create a new SQL database, the Primary filegroup is automatically created.
- Secondary/User-defined filegroups: The secondary filegroup is created by a user. The secondary filegroup is used to manage the data of the database. If you want to keep your highly accessed tables in faster disks, you can create a secondary filegroup and a table in it.
- Memory-optimized filegroup: The memory-optimized filegroup is used to store the In-memory OLTP tables and table variables. To create a memory-optimized table in a SQL database, we must create a memory-optimized filegroup. We will learn more about the memory-optimized filegroups in the next article.
- FILESTREAM filegroup: The FILESTREAM filegroup is created to host the FILESTREAM data and FILETABLES. I will explain more about the FILESTRAEAM filegroup in the next article.
The filegroup contains one or multiple data files. This article explains how we can manage the We are going to understand the following use cases.
- Add data files in filegroups
- Rename the filegroup
- Change default filegroup
To demonstrate the above scenarios, I have installed SQL Server 2019 on my laptop and restored a stackoverflow2010 sample database. You can download the Stackoverflow2010 database from here. I have also created a filegroup named FG_POSTS in a database. You can view the list of filegroups and list of datafiles associated filegroup by executing the following query.
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 |
Output
Now, let us understand all use cases.
Add data files in a filegroup
We can use ALTER DATABASE ADD FILE TO FILEGRAOUP statement. The syntax is following:
1 2 3 4 |
ALTER DATABASE <db_name> ADD FILE (NAME= <logical_file_name>, FILENAME =<file_location>, SIZE=<File_size>, FILEGROWTH= <datafile_growth>) To FILEGROUP <file_group_name> |
In the syntax,
- db_name: Specify the name of the database in which you want to add a data file. The db_name must be specified after ALTER DATABASE keyword.
- logical_file_name: Specify the logical name of the secondary data file.
- file_location: Specify the path of the data file.
- file_size: Specify the initial size of the data file. The unit of the Size parameter can be KB/MB/GB.
- datafile_growth: Specify the growth of the data file. The unit of the FILEGROWTH parameter can be KB/MB/GB
- file_group_name: Specify the name of the filegroup in which you want to add a data file.
For example, we want to add a data file to the FG_POSTS filegroup with the following options
- logical file name: fg_posts_data
- datafile location: D:\FG_Posts
- data_file_name: fg_posts_data.ndf
- initial Size: 10MB
- growth: 5MB.
The query to add a data file is following.
1 2 3 4 5 |
ALTER DATABASE [Stackoverflow2010] ADD FILE ( NAME = N'fg_posts_data', FILENAME = N'D:\FG_Posts\fg_posts_data.ndf' , SIZE = 10MB , FILEGROWTH = 5MB ) TO FILEGROUP [FG_POSTS] GO |
Once the command is executed, successfully open the D:\FG_Posts to view the data file.
As you can see, the file has been created. Alternatively, you can run the following query to view the filegroups.
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, the data file named fg_posts_data has been created.
Now, let us understand how we can rename the existing filegroup.
Rename existing filegroup
You can use ALTER DATABASE MODIFY FILEGROUP statement. Following is the syntax:
1 2 3 |
ALTER DATABASE <db_name> MODIFY FILEGROUP FILEGROUP <file_group_name> NAME= <file_group_new_name> |
In the syntax,
- db_name: Specify the database name in which the filegroup has been created. The database name must be specified after ALTER DATABASE statement.
- file_group_name: Specify the filegroup name that you want to rename.
- file_group_new_name: Specify the new name of the filegroup.
Suppose you want to rename the filegroup named FG_Posts to FG_POSTS_1. The query to rename the filegroup the command is following:
1 2 3 |
ALTER DATABASE [Stackoverflow2010] MODIFY FILEGROUP FG_POSTS NAME= FG_POSTS_1 |
Once the command is executed successfully, run the following query to verify the changes have been applied or not.
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 |
Output
As you can see, the filegroup has been renamed successfully.
Now, let us see how we can mark a secondary filegroup as a default filegroup.
Change default filegroup
To change the default filegroup, we can use ALTER DATABASE MODIFY FILEGROUP statement. Following is the syntax:
1 2 3 |
ALTER DATABASE <db_name> MODIFY FILEGROUP <file_group_name> DEFAULT |
In the syntax,
- db_name: Specify the database name in which the filegroup has been created. The database name must be specified after ALTER DATABASE statement.
- File_group_name: Specify the filegroup name that you want to make as a default filegroup. The filegroup name must be specified after MODIFY FILEGROUP keyword.
- DEFAULT: The DEFAULT keyword must be specified after the filegroup name.
Suppose you want to set the FG_POSTS_1 as a default filegroup. To do that, run the following query.
1 2 3 |
ALTER DATABASE [Stackoverflow2010] MODIFY FILEGROUP [FG_POSTS_1] DEFAULT |
Once the command is executed successfully, run the following query to view the default filegroup of a database named Stackoverflow2010.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
SELECT [databasefile].NAME AS [FileName], [filegroup].NAME AS [File_Group_Name], [filegroup].type_desc, physical_name [Data File Location], Case when is_default =1 then 'Default filegroup' else 'non-default filegroup' end [Is default Filegroup], 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 FG_POSTS_1 is a default filegroup.
Now, let us create a table named tblStackoverflow_Users. The script to create the table is the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
USE [Stackoverflow2010] GO CREATE TABLE [dbo].[tblStackoverflow_Users]( [Id] [int] IDENTITY(1,1) NOT NULL, [AboutMe] [nvarchar](max) NULL, [Age] [int] NULL, [CreationDate] [datetime] NOT NULL, [DisplayName] [nvarchar](40) NOT NULL, [DownVotes] [int] NOT NULL, [EmailHash] [nvarchar](40) NULL, [LastAccessDate] [datetime] NOT NULL, [Location] [nvarchar](100) NULL CONSTRAINT [PK_tblStackoverflow_Users_Id] PRIMARY KEY CLUSTERED ( [Id] ASC )) GO |
Now, let us see in which filegroup the table has been created. To do that, run the following query:
1 2 3 4 5 6 7 8 9 10 |
use Stackoverflow2010 go SELECT OBJECT_NAME(t.object_id) AS [Table Name], d.name AS [Filegroup Name] FROM sys.data_spaces d JOIN sys.indexes i on i.data_space_id = d.data_space_id JOIN sys.tables t on t.object_id = i.object_id WHERE i.index_id<2 AND t.type = 'U' AND OBJECT_NAME(t.object_id) ='tblStackoverflow_Users' |
Output
As you can see, the table is created in the FG_POSTS_1 filegroup.
Summary
In this article, we learned how we could manage the filegroups in SQL Database. We learned the following scenarios:
- Add data files in filegroups
- Rename the filegroup
- Change default filegroup
In my upcoming articles, we will learn how we can add FILESTREAM filegroup and memory-optimized file groups in a SQL Database.
- 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