Check Database HIGH CPU Utilization in SQL Server
Check Database HIGH CPU Utilization in SQL Server
com
High CPU utilization in SQL Server can impact the performance of your database and affect overall system
responsiveness. Identifying the cause of high CPU usage involves examining various aspects of SQL
Server, including query performance, indexing, and system configuration. Here’s a detailed guide on how to
check and diagnose high CPU utilization in SQL Server:
SSMS includes a built-in performance dashboard that provides an overview of CPU usage:
1. Open SSMS.
2. Connect to your SQL Server instance.
3. Navigate to Management > Data Collection > Performance Dashboard.
4. Review the CPU Utilization section to see if the CPU usage is high.
DMVs provide detailed insights into SQL Server performance and can help identify high CPU usage.
https://www.sqldbachamps.com
This query helps you identify which queries or sessions are consuming the most CPU resources:
SELECT
session_id,
login_name,
status,
cpu_time,
total_elapsed_time,
program_name,
host_name,
nt_domain,
nt_user_name
FROM
sys.dm_exec_requests
ORDER BY
cpu_time DESC;
https://www.sqldbachamps.com
https://www.sqldbachamps.com
2. Query to Check High CPU Usage by Index Scans
Indexes that are not optimized can lead to high CPU usage. This query helps identify expensive index
scans:
SELECT
OBJECT_NAME(s.[object_id]) AS TableName,
i.name AS IndexName,
s.index_id,
s.[logical_reads],
s.[reads],
s.[writes],
s.[cpu_time]
FROM
sys.dm_db_index_usage_stats AS s
INNER JOIN sys.indexes AS i
ON s.[object_id] = i.[object_id] AND s.index_id = i.index_id
WHERE
OBJECTPROPERTY(s.[object_id], 'IsUserTable') = 1
ORDER BY
s.cpu_time DESC;
https://www.sqldbachamps.com
Wait statistics can indicate resource bottlenecks, including high CPU usage:
SELECT
wait_type,
wait_time_ms / 1000.0 AS WaitTimeSec,
wait_time_ms / (wait_time_ms + signal_wait_time_ms) * 100 AS WaitTimePercent
FROM
sys.dm_os_wait_stats
ORDER BY
wait_time_ms DESC;
https://www.sqldbachamps.com
https://www.sqldbachamps.com
2. Diagnosing High CPU Utilization
1. Query Execution Plan: Review the execution plans of the queries consuming high CPU to identify
inefficiencies. Use the Actual Execution Plan feature in SSMS.
2. Query Statistics: Use the following query to get query execution statistics:
SELECT
query_stats.query_hash,
query_stats.execution_count,
query_stats.total_worker_time / query_stats.execution_count AS AvgCPUTime,
query_stats.total_worker_time AS TotalCPUTime,
query_stats.total_elapsed_time / query_stats.execution_count AS AvgElapsedTime
FROM
sys.dm_exec_query_stats AS query_stats
ORDER BY
TotalCPUTime DESC;
1. Missing Indexes: Identify missing indexes that could help reduce CPU usage:
https://www.sqldbachamps.com
SELECT
migs.database_id,
mid.[object_id],
mid.index_handle,
mid.equality_columns,
mid.inequality_columns,
mid.included_columns
FROM
sys.dm_db_missing_index_groups AS migs
JOIN sys.dm_db_missing_index_details AS mid
ON migs.database_id = mid.database_id
AND migs.[object_id] = mid.[object_id]
ORDER BY
migs.index_handle DESC;
https://www.sqldbachamps.com
https://www.sqldbachamps.com
2. Fragmentation: Check for index fragmentation and rebuild or reorganize fragmented indexes:
SELECT
DB_NAME(ps.database_id) AS DatabaseName,
OBJECT_NAME(ps.[object_id]) AS TableName,
i.index_id,
i.name AS IndexName,
ps.avg_fragmentation_in_percent
FROM
sys.dm_db_index_physical_stats(NULL, NULL, NULL, NULL, NULL) AS ps
INNER JOIN sys.indexes AS i
ON ps.[object_id] = i.[object_id] AND ps.index_id = i.index_id
WHERE
ps.avg_fragmentation_in_percent > 30
ORDER BY
ps.avg_fragmentation_in_percent DESC;
1. SQL Server Wait Statistics: Review wait statistics to identify any contention issues:
SELECT
https://www.sqldbachamps.com
wait_type,
wait_time_ms / 1000.0 AS WaitTimeSec,
wait_time_ms / (wait_time_ms + signal_wait_time_ms) * 100 AS WaitTimePercent
FROM
sys.dm_os_wait_stats
WHERE
wait_time_ms > 0
ORDER BY
wait_time_ms DESC;
2. Blocking: Check for blocking sessions that might be causing CPU spikes:
SELECT
blocking_session_id AS BlockingSessionID,
session_id AS BlockedSessionID,
wait_type,
wait_time,
wait_time_ms / 1000.0 AS WaitTimeSec
FROM
sys.dm_exec_requests
WHERE
blocking_session_id <> 0
ORDER BY
wait_time DESC;
https://www.sqldbachamps.com
https://www.sqldbachamps.com
3. Mitigating High CPU Utilization
a. Query Optimization
b. Index Optimization
- Create Missing Indexes: Add indexes that can reduce the need for expensive operations.
- Rebuild/Defragment Indexes: Rebuild or reorganize fragmented indexes to improve performance.
c. System Configuration
- Max Degree of Parallelism (MAXDOP): Adjust the MAXDOP setting to control how many processors SQL
Server can use for parallel query execution.
- CPU Affinity: Configure CPU affinity to control which CPUs SQL Server can use.
https://www.sqldbachamps.com
- Regular Monitoring: Use performance monitoring tools to continuously check CPU utilization.
- Performance Tuning: Periodically review and tune performance based on monitoring data.
Use SQL Server Profiler to capture and analyze events related to CPU usage:
Use Windows Performance Monitor to monitor CPU usage and SQL Server performance counters:
https://www.sqldbachamps.com
https://www.sqldbachamps.com
Conclusion
High CPU utilization in SQL Server can be caused by various factors, including inefficient queries, missing
indexes, and system configuration issues.
By using DMVs, performance tools, and SQL Server Agent jobs, you can identify and diagnose high CPU
usage, optimize queries and indexes, and adjust system settings to mitigate performance issues.
Regular monitoring and performance tuning are essential to maintaining optimal CPU performance in SQL
Server.
https://www.sqldbachamps.com
https://www.sqldbachamps.com