How to Get SQL Server Database Size
Last Updated :
18 Apr, 2024
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.
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:
- Using Table Sys.master_files
- Using Stored Proc sp_spaceused
- Using the Manual Option in SSMS
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.
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.
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.
To use sp_spaceused, execute the following SQL command:
USE [YourDatabaseName];
GO
EXEC sp_spaceused;
OUTPUT:
Database_name
| Database_size
| Unallocated space
| Reserved
| Data
| Index_size
| Unused
|
---|
YourDatabase
| 10240 KB
| 512 KB
| 5120 KB
| 4096 KB
| 1024 KB
| 0 KB
|
---|
Explanation: The result set from sp_spaceused includes the following columns:
- Database_name: Name of the database.
- Database_size: Total size of the database.
- Unallocated space: Space available for new data.
- Reserved: Total reserved space for database objects.
- Data: Used space by data.
- Index_size: Used space by indexes.
- Unused: Space that is allocated but not used.
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.
Steps to follows:
- Space to Verify Data Size in SSMS
- Run SSMS, specify the instance of our SQL Server and establish a connection to it.
- In the Object Explorer, if the Databases node is not expanded already, expand it.
- Click on the right-click on the data declaration we want to review. Select Properties after clicking on.
- In the Database Properties dialog which can be accessed from the General page and select the field names for the conditions.
- Number of database size will be displayed right below this header. We will see the database size in MB.
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.
Similar Reads
How to Get Database Size in SQL
SQL database size is important for effective management. It indicates the storage space occupied by tables, indexes, and other components. Knowing the size of a database is useful for various purposes, such as monitoring the growth, estimating the backup time, planning the storage capacity, and opti
7 min read
How to Get SQLite Database Size
SQLite is a lightweight and serverless SQL database. It is widely used in mobile devices and embedded systems due to its simplicity and efficiency. To understand how to manage SQLite databases efficiently, it is very important to know the Database size in SQLite. In this article, we will learn about
5 min read
How to Open a Database in SQL Server?
Opening a database in SQL Server is a fundamental task for database administrators and developers. It involves establishing a connection to the server instance and selecting a database to work with. In this article, we will explore two methods to open a database in SQL Server such as using SQL Serve
3 min read
How to Import and Export SQL Server Database?
Creating and managing a SQL Server database is an essential skill for database administrators and developers. In this article, We will go through the process of setting up a database in SQL Server, from creating the database and tables to inserting records, and finally, exporting and importing the d
3 min read
SQL Server Show/List Databases
Listing all databases in SQL Server is a common task for database administrators and developers. SQL Server provides two main methods to solve this such as using SQL commands and using SQL Server Management Studio (SSMS). In this article, we will learn about how to Show/List the SQL Server Databases
4 min read
SQL Server - Database Objects
In SQL Server, database objects are essential components that allow to store and manage data effectively. These objects can range from tables and views to stored procedures and indexes. Understanding the various types of database objects is important for database design, management, and optimization
5 min read
How to Show Database in PL/SQL
PL/SQL is the Procedural Language/Structured Query Language and serves as a procedural language built-in extension to SQL language, which allows seamless integration of procedural constructs with SQL. One of the most common functions of a DBMS is the retrieval of information about databases which is
4 min read
How to Migrate SQL Server Database to MySQL?
The migration of an SQL Server database to MySQL can be an intricate process, but using the ODBC driver facilitates the execution, so no worries for users with basic skills. This guide will take you through transferring the database called 'Work' from Microsoft SQL Server to MySQL, ensuring that all
5 min read
How to Export SQL Server Data to a CSV File?
Here we will see, how to export SQL Server Data to CSV file by using the 'Import and Export wizard' of SQL Server Management Studio (SSMS). CSV (Comma-separated values): It is a file that consists of plain text data in which data is separated using comma(,). It is also known as Comma Delimited Files
2 min read
List All Databases in SQL Server
In SQL Server, databases are crucial for storing and managing data efficiently. Whether we are managing a large enterprise system or a small application, understanding how to list all the databases on our SQL Server is essential. In this article, we will write SQL queries that help us to retrieve al
3 min read