0% found this document useful (0 votes)
98 views

SQL Server Monitoring Metrics

This document provides SQL queries to monitor various metrics on a SQL Server instance including available memory, top queries by performance metrics, number of connections, disk space usage, database sizes, and errors in the SQL Server error log. Queries are presented to check available physical memory, identify poorly performing SQL statements, get the number of current connections, check available disk space, view database sizes, and search the error log for recent errors.

Uploaded by

Vijay Rajendiran
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views

SQL Server Monitoring Metrics

This document provides SQL queries to monitor various metrics on a SQL Server instance including available memory, top queries by performance metrics, number of connections, disk space usage, database sizes, and errors in the SQL Server error log. Queries are presented to check available physical memory, identify poorly performing SQL statements, get the number of current connections, check available disk space, view database sizes, and search the error log for recent errors.

Uploaded by

Vijay Rajendiran
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

SQL SERVER MONITORING METRICS

1. Are you running low on server memory for SQL Server?

To check the memory on your server we can use the dynamic management view
dm_os_sys_memory.

SELECT available_physical_memory_kb/1024 as "Total Memory MB",


available_physical_memory_kb/(total_physical_memory_kb*1.0)*100 AS "% Memory Free"
FROM sys.dm_os_sys_memory

2. Are there any SQL Server statements in the cache that could use tuning?

The following query will identify any poor performing SQL statements. You can alter the
"order by" clause depending on what you are most concerned with (IO vs. CPU vs. Elapsed
Time).

SELECT top 10 text as "SQL Statement",


last_execution_time as "Last Execution Time",
(total_logical_reads+total_physical_reads+total_logical_writes)/execution_count as [Average IO],
(total_worker_time/execution_count)/1000000.0 as [Average CPU Time (sec)],
(total_elapsed_time/execution_count)/1000000.0 as [Average Elapsed Time (sec)],
execution_count as "Execution Count",
qp.query_plan as "Query Plan"
FROM sys.dm_exec_query_stats qs
CROSS APPLY sys.dm_exec_sql_text(qs.plan_handle) st
CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) qp
order by total_elapsed_time/execution_count desc

3. How many connections do you have to your SQL Server instance?

This query on its own does not provide too much information other than show you if there
is some blocking in the system. However, once you get a baseline for your applications
through running this query, you'll be able to see if you have a higher than normal number of
connections. This can be an early sign that there may be a problem.

SELECT spid, kpid, blocked, d.name, open_tran, status, hostname,


cmd, login_time, loginame, net_library
FROM sys.sysprocesses p
INNER JOIN sys.databases d
on p.dbid=d.database_id
4. Are you running out of space on any of your disks on your SQL Server?

You can use the extended stored procedure xp_fixeddrives to get a look at the space left on
our drives.

exec master.dbo.xp_fixeddrives

5. To Check the all the database size

You can use the query to get a look at the maximum size of a database in MB.

SELECT distinct
name,
size,
size * 8/1024 'Size (MB)',
max_size
FROM sys.master_files;

6. Are there any errors in your SQL Server Error Log?

In order to check the SQL Server Error Log we are going to use the undocumented extended
stored procedure, xp_readerrorlog. This query will look at the current log and go back a
maximum of 2 days looking for any errors during that time frame.

declare @Time_Start datetime;


declare @Time_End datetime;
set @Time_Start=getdate()-2;
set @Time_End=getdate();
-- Create the temporary table
CREATE TABLE #ErrorLog (logdate datetime
, processinfo varchar(255)
, Message varchar(500))
-- Populate the temporary table
INSERT #ErrorLog (logdate, processinfo, Message)
EXEC master.dbo.xp_readerrorlog 0, 1, null, null , @Time_Start, @Time_End, N'desc';
-- Filter the temporary table
SELECT LogDate, Message FROM #ErrorLog
WHERE (Message LIKE '%error%' OR Message LIKE '%failed%') AND processinfo NOT LIKE 'logon'
ORDER BY logdate DESC
-- Drop the temporary table
DROP TABLE #ErrorLog

7. Enabling Deadlock Metrics as it is already on the Zabbix – It is in disabled state now.

You might also like