{"id":73425,"date":"2012-07-01T22:33:32","date_gmt":"2012-07-01T22:33:32","guid":{"rendered":"https:\/\/www.webstaging.red-gate.com\/simple-talk\/?p=73425"},"modified":"2018-12-12T13:18:10","modified_gmt":"2018-12-12T13:18:10","slug":"utility-queries-database-files-and-filegroups","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/utility-queries-database-files-and-filegroups\/","title":{"rendered":"Utility Queries\u2013Database Files, (and Filegroups)"},"content":{"rendered":"<p>It has been a while since I last posted a utility query, and today, to avoid other work I am supposed to be doing, I decided to go ahead and work on another post.&#160; Today, I went ahead and worked on a server configuration type query. One query I find I use pretty often is the following one that lists the files in the database. In this blog I will include 3 queries.&#160; The first will deal with files and databases, and the second runs in a database to see the files and their filegroups (If there is an easy way to get the filegroups at a server level, I am not sure of it\u2026let me know).<\/p>\n<p><strong>Database Files, All Databases \u2013 File Level <\/strong>(<a href=\"http:\/\/www.drsql.org\/Documents\/DatabaseFilesSizing.sql\" target=\"_blank\">Download source<\/a>)<\/p>\n<p>It is a pretty simple query, and it returns the following columns. (A value of &#8216;<a href=\"http:\/\/mailto:'@TOTAL'\">@TOTAL&#8217;<\/a> indicates that the row is a summary row, and some file_types will not report a file size. ):<\/p>\n<ul>\n<li>database_name \u2013 The name of the database <\/li>\n<li>database_file_name \u2013 The file name that was set when the file was added to the database (the logical name, not the physical name) <\/li>\n<li>size_in_kb \u2013 The size of the file in kilobytes, such that it matches the file size in the Windows Explorer <\/li>\n<li>size_in_mb \u2013 The size of the file in megabytes, a size that is more typical the people want to see <\/li>\n<li>size_in_gb \u2013 The size of the file in gigabytes, useful when looking at really large files <\/li>\n<li>file_type \u2013 How the file is used in the server<!--EndFragment--> <\/li>\n<li>filesystem_drive_letter \u2013 the drive letter where the file is located <\/li>\n<li>filesystem_file_name \u2013 name of the physical file <\/li>\n<li>filesystem_path \u2013 the path where the files are located. <\/li>\n<\/ul>\n<p>&#8211;Get the files and total size of files for all databases<\/p>\n<p>SELECT&#160; &#8211;the name of the database    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; CASE WHEN GROUPING(DB_NAME(database_id)) = 1 THEN &#8216;@TOTAL&#8217;     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ELSE DB_NAME(database_id)     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; END AS database_name ,<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#8211;the logical name of the file    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; CASE WHEN GROUPING(master_files.name) = 1 THEN &#8216;@TOTAL&#8217;     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ELSE master_files.name     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; END AS database_file_name ,<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#8211;the size of the file is stored in # of pages    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; SUM(master_files.size * 8.0) AS size_in_kb,     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; SUM(master_files.size * 8.0) \/ 1024.0 AS size_in_mb,     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; SUM(master_files.size * 8.0) \/ 1024.0 \/ 1024.0 AS size_in_gb,<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#8211;the physical filename only    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; CASE WHEN GROUPING(master_files.name) = 1 THEN &#8221;     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ELSE MAX(master_files.type_desc)     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; END AS file_type ,&#160; <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#8211;the physical filename only     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; CASE WHEN GROUPING(master_files.name) = 1 THEN &#8221;     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ELSE MAX(UPPER(SUBSTRING(master_files.physical_name, 1, 1)))     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; END AS filesystem_drive_letter ,&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160; &#8211;thanks to Phillip Kelley from <a href=\"http:\/\/stackoverflow.com\/questions\/1024978\/find-index-of-last-occurrence-of-a-sub-string-using-t-sql\">http:\/\/stackoverflow.com\/questions\/1024978\/find-index-of-last-occurrence-of-a-sub-string-using-t-sql<\/a>     <br \/>&#160;&#160;&#160;&#160;&#160;&#160; &#8211;for the REVERSE code to get the filename and path.<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#8211;the physical filename only    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; CASE WHEN GROUPING(master_files.name) = 1 THEN &#8221;     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ELSE MAX(REVERSE(LEFT(REVERSE(master_files.physical_name),     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CHARINDEX(&#8216;\\&#8217;, REVERSE(physical_name)) &#8211; 1)))     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; END AS filesystem_file_name ,<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#8211;the path of the file only    <br \/>&#160;&#160;&#160;&#160;&#160;&#160; cASE WHEN GROUPING(master_files.name) = 1 THEN &#8221;     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ELSE MAX(REPLACE(master_files.physical_name,     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; REVERSE(LEFT(REVERSE(master_files.physical_name),     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CHARINDEX(&#8216;\\&#8217;, REVERSE(physical_name)) &#8211; 1)), &#8221;))     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; END AS filesystem_path<\/p>\n<p>FROM&#160;&#160;&#160; sys.master_files    <br \/>GROUP BY DB_NAME(database_id) , &#8211;the database and filegroup and the file (all of the parts)     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; master_files.name WITH rollup     <br \/>ORDER BY database_name, database_file_name<\/p>\n<p><strong>Single Database By Filegroup<\/strong> (<a href=\"http:\/\/www.drsql.org\/Documents\/FilegroupFileSizing.sql\" target=\"_blank\">Download source<\/a>)<\/p>\n<p><em>Edited: Added code based on one of the comments here: <\/em><a title=\"http:\/\/www.sqlblog.lv\/2011\/05\/ka-apskatit-datu-bazes-failu-izmeru-un.html\" href=\"http:\/\/www.sqlblog.lv\/2011\/05\/ka-apskatit-datu-bazes-failu-izmeru-un.html\"><em>http:\/\/www.sqlblog.lv\/2011\/05\/ka-apskatit-datu-bazes-failu-izmeru-un.html<\/em><\/a><em>.&#160; His post does all db\u2019s with sizing, but I preferred to have this query only work on one database. I added columns for available space, used space, as well as on disk space<\/em><\/p>\n<p>In the second query, it will, for one database, list all of the row and log filegroups and their files. Like the previous query, it may list filegroups that have a 0 size for types like full text. It uses sys.database_files for the files. This has one downside, and that is that if the database is read only, it is possible that the results will not be correct and will reflect a previous version of the metadata. Use master_files if you want to get current values, but there is no guarantees that it will match the filegroups.&#160; <\/p>\n<p>It will return:<\/p>\n<ul>\n<li>filegroup_name \u2013 The name of the filegroup in the database <\/li>\n<li>database_file_name \u2013 The file name that was set when the file was added to the database (the logical name, not the physical name) <\/li>\n<li>size_in_kb \u2013 The size of the file in kilobytes, such that it matches the file size in the Windows Explorer <\/li>\n<li>size_in_mb \u2013 The size of the file in megabytes, a size that is more typical the people want to see (Commented Out)<\/li>\n<li>size_in_gb \u2013 The size of the file in gigabytes, useful when looking at really large files <\/li>\n<li>used_size_in_kb \u2013 The amount of the file that has data allocated, in kilobytes<\/li>\n<li>used_size_in_mb \u2013 The amount of the file that has data allocated in megabytes, a size that is more typical the people want to see (Commented Out) <\/li>\n<li>used_size_in_gb \u2013 The amount of the file that has data allocated, in gigabytes, useful when looking at really large files <\/li>\n<li>available_size_in_kb \u2013 The amount of free space in kilobytes, such that it matches the file size in the Windows Explorer <\/li>\n<li>available_size_in_mb \u2013 The amount of free space in megabytes, a size that is more typical the people want to see (Commented Out) <\/li>\n<li>available_size_in_gb \u2013 The amount of free space in gigabytes, useful when looking at really large files <\/li>\n<li>size_on_disk_kb \u2013 The amount of space the file takes in the file system (reported from the DMVs)<!--EndFragment--><\/li>\n<p> <!--EndFragment-->    <\/p>\n<li>file_type \u2013 How the file is used in the server<!--EndFragment--> <\/li>\n<li>filesystem_drive_letter \u2013 the drive letter where the file is located <\/li>\n<li>filesystem_file_name \u2013 name of the physical file <\/li>\n<li>filesystem_path \u2013 the path where the files are located. <\/li>\n<\/ul>\n<p>SELECT&#160; &#8211;the name of the database<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#8211;the name of the filegroup (or Log for the log file, which doesn&#8217;t have a filegroup)   <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CASE WHEN GROUPING(filegroups.name) = 1 THEN &#8216;@TOTAL&#8217;    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; WHEN filegroups.name IS NULL THEN &#8216;LOGS&#8217;    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ELSE filegroups.name    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; END AS filegroup_name ,    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#8211;the logical name of the file    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CASE WHEN GROUPING(database_files.name) = 1 THEN &#8216;@TOTAL&#8217;    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ELSE database_files.name    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; END AS database_file_name ,<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#8211;the size of the file is stored in # of pages   <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; SUM(database_files.size * 8.0) AS size_in_kb,    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#8211;SUM(database_files.size * 8.0) \/ 1024.0 AS size_in_mb,    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; SUM(database_files.size * 8.0) \/ 1024.0 \/ 1024.0 AS size_in_gb,    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; SUM(FILEPROPERTY(database_files.NAME,&#8217;SpaceUsed&#8217;) * 8.0) AS used_size_in_kb,    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#8211;SUM(FILEPROPERTY(database_files.NAME,&#8217;SpaceUsed&#8217;) * 8.0)\/ 1024.0&#160; AS used_size_in_mb,    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; SUM(FILEPROPERTY(database_files.NAME,&#8217;SpaceUsed&#8217;) * 8.0) \/ 1024.0 \/ 1024.0 AS used_size_in_gb,&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; SUM((database_files.size &#8211; FILEPROPERTY(database_files.NAME,&#8217;SpaceUsed&#8217;)) * 8.0) AS available_size_in_kb,   <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#8211;SUM((database_files.size &#8211; FILEPROPERTY(database_files.NAME,&#8217;SpaceUsed&#8217;)) * 8.0)\/ 1024.0&#160; AS available_size_in_mb,    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; SUM((database_files.size &#8211; FILEPROPERTY(database_files.NAME,&#8217;SpaceUsed&#8217;)) * 8.0) \/ 1024.0 \/ 1024.0 AS available_size_in_gb,&#160;&#160; <\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; SUM(DIVFS.size_on_disk_bytes\/1024.0) AS size_on_disk_kb,   <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#8211;the physical filename only    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CASE WHEN GROUPING(database_files.name) = 1 THEN &#8221;    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ELSE MAX(database_files.type_desc)    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; END AS file_type ,&#160; <\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#8211;the physical filename only   <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CASE WHEN GROUPING(database_files.name) = 1 THEN &#8221;    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ELSE MAX(UPPER(SUBSTRING(database_files.physical_name, 1, 1)))    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; END AS filesystem_drive_letter ,&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#8211;thanks to Phillip Kelley from <a href=\"http:\/\/stackoverflow.com\/questions\/1024978\/find-index-of-last-occurrence-of-a-sub-string-using-t-sql\">http:\/\/stackoverflow.com\/questions\/1024978\/find-index-of-last-occurrence-of-a-sub-string-using-t-sql<\/a><\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#8211;the physical filename only   <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CASE WHEN GROUPING(database_files.name) = 1 THEN &#8221;    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ELSE MAX(REVERSE(LEFT(REVERSE(database_files.physical_name), CHARINDEX(&#8216;\\&#8217;, REVERSE(database_files.physical_name)) &#8211; 1)))    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; END AS filesystem_file_name ,<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#8211;the path of the file only   <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CASE WHEN GROUPING(database_files.name) = 1 THEN &#8221;    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ELSE MAX(REPLACE(database_files.physical_name, REVERSE(LEFT(REVERSE(database_files.physical_name),    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CHARINDEX(&#8216;\\&#8217;, REVERSE(database_files.physical_name)) &#8211; 1)), &#8221;))    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; END AS filesystem_path    <br \/>FROM&#160;&#160;&#160; sys.database_files &#8211;use sys.master_files if the database is read only and you want to see the metadata that is the database    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#8211;log files do not have a filegroup    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; LEFT OUTER JOIN sys.filegroups     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ON database_files.data_space_id = filegroups.data_space_id    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Left Join sys.dm_io_virtual_file_stats(DB_ID(), DEFAULT) DIVFS    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; On database_files.file_id = DIVFS.file_id&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br \/>GROUP BY&#160; filegroups.name ,    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; database_files.name WITH ROLLUP    <br \/>ORDER BY&#160;&#160;&#160;&#160; &#8211;the name of the filegroup (or Log for the log file, which doesn&#8217;t have a filegroup)    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CASE WHEN GROUPING(filegroups.name) = 1 THEN &#8216;@TOTAL&#8217;    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; WHEN filegroups.name IS NULL THEN &#8216;@TOTAL-SortAfter&#8217;    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ELSE filegroups.name    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; END,     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; database_file_name<\/p>\n<p>Hope these queries help out sometime.&#160; More on the way as I finish up other projects!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It has been a while since I last posted a utility query, and today, to avoid other work I am supposed to be doing, I decided to go ahead and work on another post.&#160; Today, I went ahead and worked on a server configuration type query. One query I find I use pretty often is&#8230;&hellip;<\/p>\n","protected":false},"author":56085,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[],"coauthors":[19684],"class_list":["post-73425","post","type-post","status-publish","format-standard","hentry","category-blogs"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/73425","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/56085"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=73425"}],"version-history":[{"count":1,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/73425\/revisions"}],"predecessor-version":[{"id":82313,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/73425\/revisions\/82313"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=73425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=73425"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=73425"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=73425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}