SQL server database capacity planning TSQL Script


To perform capacity planning for a SQL Server database using T-SQL, you can utilize system views and functions to gather information about your database and workload. Here’s an example of T-SQL queries you can use:

  1. Determine Current Database Size:
SELECT 
    DB_NAME() AS DatabaseName,
    SUM(size * 8 / 1024) AS DatabaseSizeMB
FROM sys.master_files
WHERE type = 0 AND DB_NAME(database_id) = DB_NAME()
GROUP BY database_id;
  1. Estimate Growth Rate:
SELECT 
    DB_NAME() AS DatabaseName,
    (SUM(size * 8 / 1024) - MAX(size * 8 / 1024)) / COUNT(*) AS GrowthRateMBPerDay
FROM sys.master_files
WHERE type = 0 AND DB_NAME(database_id) = DB_NAME()
GROUP BY database_id;
  1. Calculate CPU Usage:
SELECT 
    DB_NAME() AS DatabaseName,
    MAX(total_worker_time) / DATEDIFF(second, GETDATE(), GETDATE() + 1) AS CPUUsagePercent
FROM sys.dm_exec_query_stats
WHERE DB_NAME(dbid) = DB_NAME()
GROUP BY dbid;
  1. Estimate Memory Requirements:
SELECT 
    DB_NAME() AS DatabaseName,
    COUNT(*) * 8 AS BufferPoolSizeMB
FROM sys.dm_os_buffer_descriptors
WHERE DB_NAME(database_id) = DB_NAME();
  1. Assess Storage Requirements:
SELECT 
    DB_NAME() AS DatabaseName,
    SUM(size * 8 / 1024) AS TotalDataSizeMB,
    SUM(CASE WHEN type = 0 THEN size * 8 / 1024 ELSE 0 END) AS DataFileSizeMB,
    SUM(CASE WHEN type = 1 THEN size * 8 / 1024 ELSE 0 END) AS LogFileSizeMB
FROM sys.master_files
WHERE DB_NAME(database_id) = DB_NAME()
GROUP BY database_id;
  1. Analyze Query Performance:
SELECT TOP 10
    qs.total_worker_time / qs.execution_count AS AvgCPUTime,
    qs.total_logical_reads / qs.execution_count AS AvgLogicalReads,
    qs.total_logical_writes / qs.execution_count AS AvgLogicalWrites,
    t.text AS QueryText
FROM sys.dm_exec_query_stats AS qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS t
WHERE DB_NAME(t.dbid) = DB_NAME()
ORDER BY AvgCPUTime DESC;

By running these queries and analyzing the results, you can gain insights into the current database size, growth rate, CPU usage, memory requirements, storage needs, and query performance. This information can help you in estimating the capacity and making informed decisions for capacity planning in your SQL Server database.

Author: Rajib Kundu

Rajib Kundu is a SQL Server Evangelist/Cloud Architect with over 16 years of Expertise.A husband, father of boy, entrepreneur, Rajib has learned throughout his journey the value of hard work and giving back. Rajib is a hard working & high energy individual fueled by his passion to help people and solve problems on SQL Server and Robotic Process Automation. Rajib Kundu is a technology enthusiast with a track record of delivering breakthrough innovations at leading Indian pure play & multi national IT companies. Equally comfortable with executives, creative directors and engineers, Rajib has differentiated himself through his ability to balance business and technology needs to deliver powerful solutions. Rajib Kundu Innovative with demonstrated success in increasing revenues, market share, and earnings, achieving cost reduction, and improving client satisfaction in customer-facing operations and large, diverse organisation. Expertise in management and implementation of complex corporate acquisition initiatives and successful organisation Integrations. Talent for analysing competitive landscape, conducting research, and aligning product offering with customer requirements which translates into lower product development and Implementation costs. Reputation as a change agent With the ability to analyse issues, devise continuous process Improvements, and incorporate business process outsourcing Initiatives to Increase efficiency ,streamline operations, and decrease aggregate expenses With limited resources. You can contact me here: India:-+91-9731155800 | USA & Canada: +1- 647-694-1826 | Skype: rk_india1@rediffmail.com

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.