Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « SQL Advent 2011 Day 21: TRY CATCHSQL Server Auditing: Creating a Server Specification »
    comments

    I was recently working on a new presentation about filegroups (debuting in January! Come see me at the Wisconsin SSUG on January 10, 2012.). I was looking for a query to tie filegroups and files together. I found several blogs about it, but all of them referenced sysfiles.

    Why is this a problem? The SQL 2000 virtual table sysfiles, and the corresponding SQL 2005 + compatibility view sys.sysfiles, will be removed in a future version of SQL Server. Our very own Denis Gobo has been writing a series about updating your code to use new syntax.

    In that case, what's the new hotness?

    We should now be using the catalog view sys.database_files. Here, I'll show you this view on SQL 2008R2, using AdventureWorks2008R2.

    Here are the columns returned when querying sysfiles.

    1. USE AdventureWorks2008R2;
    2. GO
    3.  
    4. SELECT fileid, groupid, size, maxsize, growth, status, perf, name, filename
    5. FROM sysfiles;
    6. GO

    Moving on, here is the query of sys.sysfiles.

    1. USE AdventureWorks2008R2;
    2. GO
    3.  
    4. SELECT fileid, groupid, size, maxsize, growth, status, perf, name, filename
    5. FROM sys.sysfiles;

    This query's result is the same as the original virtual table.

    Now, the new hotness, sys.database_files.

    1. USE AdventureWorks2008R2;
    2. GO
    3.  
    4. SELECT *
    5. FROM sys.database_files;

    There's an immediate difference here in the number of columns returned. The new view is much more comprehensive than the old table.

    What's new?

    sys.database_files includes more than just the data and log files. It will also show FILESTREAM and fulltext files.

    The state_desc can tell you if the file is ONLINE, OFFLINE, RESTORING, and so forth.

    There are fields to tell you if the file is read-only, if it's on read-only media, and if it's a sparse file.

    Let's look at a couple of useful queries using this new view.

    A query to determine file size (with a little help from Bob Pusateri (twitter | blog)) is:

    1. SELECT fg.data_space_id AS FGID,
    2.    (f.file_id) AS FileCount,
    3.    ROUND(CAST((f.size) AS FLOAT)/128,2) AS Reserved_MB,
    4.    ROUND(CAST((FILEPROPERTY(f.name,'SpaceUsed')) AS FLOAT)/128,2) AS Used_MB,
    5.    ROUND((CAST((f.size) AS FLOAT)/128)-(CAST((FILEPROPERTY(f.name,'SpaceUsed'))AS FLOAT)/128),2) AS Free_MB
    6. FROM sys.filegroups fg
    7.     LEFT JOIN sys.database_files f ON f.data_space_id = fg.data_space_id

    Another useful query, especially as it related to what I was doing, is to determine what filegroup a file is on.

    1. SELECT FG.name as FilegroupName, F.file_id, F.name as [FileName]
    2. FROM sys.database_files F
    3.     INNER JOIN sys.filegroups FG ON FG.data_space_id = F.data_space_id;

    As Microsoft moves away from the old virtual tables and compatibility views, make sure your T-SQL is also up to date. It's a good idea to review scripts that have been in use for some time, and see if they can be updated.

    About the Author

    User bio imageJes Schultz Borland is a Consultant with Brent Ozar PLF, and a Microsoft SQL Server MVP. She holds an AAS - Programmer/Analyst degree, and has worked with SQL Server since 2007, focusing on Reporting Services and day-to-day administration. She is an active member of PASS, Director of Communication for MADPASS, founder of Tech on Tap, and a LessThanDot.com blogger. She's a frequent presenter at user groups, SQL Saturdays, and other community events. She is also an avid runner and chef.
    Social SitingsTwitterLinkedInLTD RSS Feed
    Instapaper

    3 comments

    Comment from: David Taylor [Visitor]
    David Taylor Jes and Bob,

    That's awesome, thanks! It's always the newer stuff that I miss. Being caught between a Dev (SSIS 90% of the time) and a DBA(the other 50% of the time :), I can't spend enough time on either to learn all I need to know to be effective. It's folks like you that keep me up to date, thanks again!

    David T
    12/21/11 @ 07:46
    Comment from: SQLDenis [Member] Email
    12/21/11 @ 09:25
    Comment from: Naomi Nosonovsky [Member]
    Naomi Nosonovsky I recently needed to write a script to find out the space used and compare if we're reaching a limit in case the Express version is used.

    Here is this script

    DECLARE @MaxSize INT, @MaxSizeDesc varchar(10)
    SELECT @MaxSize = CASE WHEN SERVERPROPERTY('ProductVersion') > '10.5' THEN 10 * 1024 * 1024 -- 10GB for SQL Server 2008 R2 Express
    ELSE 4 * 1024 * 1024 END,
    @MaxSizeDesc = CASE WHEN SERVERPROPERTY('ProductVersion') > '10.5' THEN '10 GB'
    ELSE '4 GB' END

    SELECT SUM(SIZE*8) AS SizeKB, @MaxSize AS MaxSize, @MaxSizeDesc as MaxSizeDesc
    FROM sys.database_files
    WHERE type = 0
    12/22/11 @ 14:54

    Leave a comment


    Your email address will not be revealed on this site.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)