MSDN Documentation

Database File Structures in SQL Server

Understanding the file structures of SQL Server databases is crucial for efficient management, performance tuning, and troubleshooting. SQL Server utilizes two primary types of files for each database:

Primary Data Files (.mdf)

The primary data file (.mdf) contains the startup information for the database and all other data and objects. Every SQL Server database must have one primary data file.

Secondary Data Files (.ndf)

Secondary data files (.ndf) are optional and are used to extend the storage capacity of a database. A database can have zero or more secondary data files.

Transaction Log Files (.ldf)

Transaction log files (.ldf) record all transactions and database modifications. These files are essential for recovery and maintaining data integrity.

Filegroups

Files are organized into filegroups. Each database has at least one filegroup: the PRIMARY filegroup. Data files (.mdf and .ndf) can be assigned to specific filegroups. Transaction log files are not part of filegroups but are managed independently.

The PRIMARY filegroup contains the .mdf file and any secondary files assigned to it. You can create additional filegroups to partition data and improve performance by spreading I/O across multiple disks.

Example Scenario

Consider a large database named SalesData:

By strategically placing these files, you can optimize query performance for frequently accessed data and indexes, while managing storage costs for archival data. Proper planning of file structures is fundamental to robust SQL Server database design.