How to use Table Sys.master_files In SQL

The sys.master_files system-view provides detailed information about all files associated with the SQL Server instance, including database files. We can use this view to retrieve the size of database files.

USE [YourDatabaseName];
GO


SELECT
DB_NAME(database_id) AS DatabaseName,
name AS FileName,
size/128.0 AS FileSizeInMB
FROM
sys.master_files
WHERE
type_desc = 'ROWS';

OUTPUT:

DatabaseName

FileName

FileSizeInMB

YourDatabase

YourDatabase.mdf

5120.0000

Explanation:

  • DB_NAME(database_id) retrieves the name of the database.
  • name retrieves the name of the database file.
  • size/128.0 calculates the file size in megabytes (MB). SQL Server stores the size of database files in 8KB pages, so dividing by 128 converts it to MB.
  • type_desc = ‘ROWS’ filters the results to show only data files.

How to Get SQL Server Database Size

The ability to keep track of our SQL Server databases is very important to maintaining our storage resources effectively and within the proper performance limits. Knowing how large databases are can put us in a position to set up storage capacity which indicates the beginning of solving issues related to disk space, and improving database performance.

In this article, We will learn about how to Get SQL Server Database Size by understanding various methods along with the implementation and so on.

Similar Reads

How to Get SQL Server Database Size?

Monitoring SQL Server database sizes is essential for efficient storage management and performance optimization. Below are the approaches that help us to understand How to Get SQL Server Database Size as follows:...

1. Using Table Sys.master_files

The sys.master_files system-view provides detailed information about all files associated with the SQL Server instance, including database files. We can use this view to retrieve the size of database files....

2. Using Stored Proc sp_spaceused

SQL Server has a sp_spaceused stored procedure that, when executed, produces tables then defining the current space used for a certain database or any object within that database. This stored procedure provides data about data size, unallocated space, reserved space, data space, index space and space across unused....

3. Using the Manual Option in SQL Server Management Studio (SSMS)

SQL Server Management Studio (SSMS) grant us the GUI which is user-friendly and to be used to work on and track SQL Server Database Management. It has an embedded query ability that launches DB size visualization without running SQL queries....

Concusion

Overall, Monitoring SQL Server database sizes is critical for effective database management. By utilizing the methods outlined in this article, database administrators can efficiently track database sizes, allocate resources effectively, and optimize database performance. Understanding database sizes is key to making informed decisions and ensuring the smooth operation of SQL Server environments....