Designing and Managing Filegroups
SQL Server databases have three types of files.
PRIMARY FILEGROUP
Each database has a primary filegroup. This filegroup hosts the primary data file and any secondary files that you have not allocated to other filegroups. The system tables are hosted in the primary filegroup. You can create filegroups to host data files together for reasons including data allocation, administrative, and placement.
Primary data files use the mdf extension.
SECONDARY FILEGROUP
Secondary data files use the .ndf extension.
You can assign these secondary files to different filegroups. Secondary data files that are hosted on different volumes can be assigned to the same filegroup.
When you create a table or an index, you can configure it to use a specific filegroup. When a filegroup contains more than one file, the Database Engine will write data across the files proportionally, depending on how much free space is available in each file.
Adding New FILEGROUP
To add a new filegroup to a database by using SQL Server Management Studio, perform the following steps:
-
In SQL Server Management Studio, right-click the database to which you want to add the filegroup and then choose Properties.
-
On the Filegroups page, click Add and then enter the name of the new filegroup,. You can add new files to a filegroup on the Files page in the Database Properties dialog box.
-
You can use the ALTER DATABASE statement with the ADD FILEGROUP option to add filegroups to a database.
To add a new filegroup named Tertiary to the AdventureWorks2012 database, use the following query:
USE [master]
GO
ALTER DATABASE [AdventureWorks2012] ADD FILEGROUP [Tertiary]
GO
Moving an Index from One Filegroup to Another
To move an index to a different filegroup or partition scheme in SQL Server Management Studio, perform the following steps:
-
In SQL Server Management Studio, right-click the index that you want to move to a new filegroup and then choose Properties.
-
On the Storage page, use the Filegroup drop-down list to select the filegroup to which you want to move the index.
You can move indexes under the following conditions:
- You cannot move indexes created using a unique or primary key constraint through SQL Server Management Studio. You can move these indexes by using the CREATE INDEX statement with the (DROP_EXISTING=ON) option.
- If the table or index is partitioned, you must select the partition scheme in which to move the index.
- You can move clustered indexes by using online processing, allowing user access to data during the move.
To move an index by using Transact-SQL, use the CREATE INDEX statement with the
DROP_EXISTING = ON option and specify the target filegroup.