What is the relationship between a table and the files that contain it? This isn’t as easy a question as you might think because there is not a direct relationship between a logical object like a table and a physical object like a file. For a small database like AdventureWorks (< 200 MB) all you need is a single data file to hold all your tables. As things start to get larger and more complex you may add files as a way of increasing performance. For example if you have a single table that is large and heavily queried you may want to put it on a faster (and probably more expensive) drive. You may also have non-clustered indexes that cover your query that would benefit from being separated. If terms like non-clustered index and covering query are unfamiliar you might benefit from this excellent article by Joe Webb.
So, how do you control table and index placement? You do it by using filegroups. Every relational index (clustered or non-clustered) is assigned to a single filegroup that is composed of one or more files. The relationship looks like this:
In the picture above the Table01 is actually the clustered index and Table01/Index01 is a non-clustered index. Note that a file can be in exactly one filegroup, but the components of the table can be located and managed through the use of different filegroups.
Here is a script you can use to explore these relationships:
–Create a test DB. Does the DB exist? Drop if it does.
Use tempdb
Go
If Exists (Select name from sys.databases Where name = ‘TabFile’)
Begin
Drop Database TabFile
End
Go
–Create DB with two filegroups. You will need a “C:\temp”
–folder to use the script as written.
Create Database TabFile On
Primary –FileGroup
( Name = N’TabFileF01′,
Filename = N’C:\Temp\TabFileF01.mdf’),
Filegroup FG02 –FileGroup
( Name = N’TabFileF02′, –File
Filename = N’C:\Temp\TabFileF02.ndf’ ),
( Name = N’TabFileF03′, –File
Filename = N’C:\Temp\TabFileF03.ndf’)
Log ON –Log
( Name = N’TabFile_log’,
Filename = N’C:\Temp\TabFile_log.ldf’)
Go
–Create table with two indexes
Use TabFile
Go
Create Table dbo.Table_1
(Col1 int Constraint PK_Table_1 Primary Key Clustered Not Null Identity (1, 1),
Col2 int Not Null) On [Primary]
Go
Create Nonclustered index IX02_Table_1 ON dbo.Table_1
(Col1 ) On FG02
Go
Create Table dbo.Table_2
(Col1 int Constraint PK_Table_2 Primary Key Clustered Not Null Identity (1, 1),
Col2 int Not Null) On FG02
Go
At this point you can take a look at the various structures using either the Object Explorer in SSMS or TSQL. I prefer TSQL because it is repeatable and far more flexible. The following script shows the relationship between tables, filegroups, and files.
–View tables and files
Use TabFile
Go
Select Distinct
O.name As TableName
,I.name As IndexName
,I.type_desc As IndexType
,I.index_id As IndexID
,FG.name As FileGroupName
,F.name As FileLogicalName
,F.physical_name As FilePhysicalName
From
sys.indexes As I
Inner Join
sys.filegroups As FG
On I.data_space_id = FG.data_space_id
Inner Join
sys.master_files As F
On FG.data_space_id = F.data_space_id
And F.database_id = DB_ID()
Inner Join
sys.all_objects As O
On I.[object_id] = O.[object_id]
Where
I.data_space_id = FG.data_space_id
And O.[type] = ‘U’
Order By
TableName
,IndexID
,FileLogicalName
Go
I recommend you play with this script on the sample DB above but also on of your own DBs. Not only will it familiarize you with the various catalog views it will also point out how your data is arranged and may help you in managing it even better.