|    Register
   
Wednesday, March 10, 2010
List objects per filegroup (by Leonid Sheinkman)

The following script lists link between database objects and filegroups. You can also use this script to identify filegroup with no objects associated

SQL2000

IF OBJECT_ID('tempdb.dbo.#FileGroup') IS NOT NULL

      DROP TABLE #FileGroup

 

IF OBJECT_ID('tempdb.dbo.#ObjectFileGroup') IS NOT NULL

      DROP TABLE #ObjectFileGroup

 

CREATE TABLE #FileGroup (

      FileGroup sysname

)

CREATE TABLE #ObjectFileGroup (

      ObjectName sysname,

      ObjectType varchar(20),

      FileGroupID int,

      FileGroup sysname

)

 

SET NOCOUNT ON

 

DECLARE @TableName sysname

DECLARE @id int

 

DECLARE cur_Tables CURSOR FAST_FORWARD FOR

      SELECT TableName = [name], id FROM dbo.sysobjects WHERE type = 'U'

OPEN cur_Tables

FETCH NEXT FROM cur_Tables INTO @TableName, @id

WHILE @@FETCH_STATUS = 0

  BEGIN

      TRUNCATE TABLE #FileGroup

      INSERT #FileGroup (FileGroup)

      EXEC sp_objectfilegroup @id

      INSERT #ObjectFileGroup (ObjectName, ObjectType, FileGroupID, FileGroup)

      SELECT @TableName, 'TABLE', FILEGROUP_ID(FileGroup), FileGroup

       FROM #FileGroup

      FETCH NEXT FROM cur_Tables INTO @TableName, @id

  END

 

CLOSE cur_Tables

DEALLOCATE cur_Tables

 

INSERT #ObjectFileGroup (ObjectName, ObjectType, FileGroupID, FileGroup)

SELECT OBJECT_NAME(id) + ' * ' +[name], 'INDEX', groupid, FILEGROUP_NAME(groupid)  FROM dbo.sysindexes

 WHERE FILEGROUP_NAME(groupid) IS NOT NULL

      AND OBJECT_NAME(id) NOT LIKE 'sys%'

      AND [name] NOT LIKE '_WA_Sys%'

      AND [name] NOT LIKE 'Statistic_%'

 

SELECT FileGroupName = FILEGROUP_NAME(sf.groupid),/*ofg.FileGroup, */ofg.ObjectName, ofg.ObjectType, FileName = sf.filename, FileSize = sf.[size] / 128

 FROM #ObjectFileGroup ofg

      RIGHT JOIN dbo.sysfiles sf

 ON ofg.FileGroupID = sf.groupid

 ORDER BY FileGroup, ObjectName

 

SQL 2005 (very simplified)

 

SELECT

      fg.data_space_id, fg.name,

ObjectName = OBJECT_NAME(p.object_id), p.index_id

      ,df.name, df.physical_name, [Size] = df.size*8/1024

 FROM sys.filegroups fg

 LEFT JOIN sys.database_files df

      ON fg.data_space_id = df.data_space_id

 LEFT JOIN sys.partitions p

      ON fg.data_space_id = p.partition_number

 WHERE (p.object_id>4096 or p.object_id IS NULL)

 

Comments
Average rating:  (3.7)
 SQL 2005 was wrong, try this, 1/26/2010 
Reviewer: James Gu (Montreal, Canada)
select object_name(i.object_id), d.name
from sys.indexes i, sys.data_spaces d
where i.index_id &lt 2 and d.data_space_id = i.data_space_id
and i.object_id &gt 4096
 2000 works, not 2005, 8/11/2008 
Reviewer: D N (Natick, MA, United States)
Under SQL 2005, the 2005 variant above doesn't find indexes where the underlying base table is in Primary, but the indexes are in the secondary group. Use the 200 variant and it works.
 sql server DBA, 6/10/2008 
Reviewer: Senthilkumar Rajendran (chennai, India)
its fine to understand
 Spot on!, 6/5/2008 
Reviewer: Braam (Centurion, South Africa)
Spot on!
 This is what I was looking for, 5/22/2008 
Reviewer: S C B (Whiteplains, United States)
Shows indexs and tables on all filegroups
12

Rating




*
Title *
Comment  *
Name *
City *
Country *
Google Ads
Copyright (c) 2010 TSQL.CA     |    Privacy Statement    |    Terms Of Use