patternsqlMinor
Check server activity with code
Viewed 0 times
withcodeserveractivitycheck
Problem
I would like to see the activity on of the SQL server. So I can run a query when the server load is low. I know there is an activity monitor in SQL server but my company doesn't want me to give the rights to use this tool. Apparently you can kill other people queries and this is too dangerous.
Is there a way to check the status of the server with code?
(Or view the activity without needing those rights so I can manually run them?)
Is there a way to check the status of the server with code?
(Or view the activity without needing those rights so I can manually run them?)
Solution
The below query uses the sys.dm_os_performance_counters DMV to capture the performance counters of SQL Server. You can change the WAITFOR DELAY options and the Numeric suffix after the second GO command to modify the time interval and the number of times you want the code to be executed.
Once you have enough data and you have narrowed down the counters which are relevant, you can decide upon the best time to run your queries. And also as mentioned in the answer by @Yaroslav ,it is very important to keep in mind what type of query you want to run and decide accordingly.
And the most important thing, be careful with production data!!!
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
IF OBJECT_ID('Tempdb..#PerformanceMonitor') IS NOT NULL
DROP TABLE #PerformanceMonitor;
CREATE TABLE #PerformanceMonitor
(
[object_name] NVARCHAR(255) NOT NULL,
[counter_name] NVARCHAR(255) NOT NULL,
[instance_name] NVARCHAR(255) NULL,
[cntr_value] BIGINT NOT NULL,
[cntr_type] INT NOT NULL,
[LoadedOn] DATETIME NOT NULL DEFAULT GETDATE(),
)
GO
INSERT INTO #PerformanceMonitor
([object_name],[counter_name],[instance_name],[cntr_value],[cntr_type])
(SELECT object_name,counter_name,instance_name,cntr_value,cntr_type
FROM sys.dm_os_performance_counters)
WAITFOR DELAY '00:00:10' --Change the interval here
GO 10 --Change the number of times the data will be captured here
SELECT * FROM #PerformanceMonitorOnce you have enough data and you have narrowed down the counters which are relevant, you can decide upon the best time to run your queries. And also as mentioned in the answer by @Yaroslav ,it is very important to keep in mind what type of query you want to run and decide accordingly.
And the most important thing, be careful with production data!!!
Code Snippets
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
IF OBJECT_ID('Tempdb..#PerformanceMonitor') IS NOT NULL
DROP TABLE #PerformanceMonitor;
CREATE TABLE #PerformanceMonitor
(
[object_name] NVARCHAR(255) NOT NULL,
[counter_name] NVARCHAR(255) NOT NULL,
[instance_name] NVARCHAR(255) NULL,
[cntr_value] BIGINT NOT NULL,
[cntr_type] INT NOT NULL,
[LoadedOn] DATETIME NOT NULL DEFAULT GETDATE(),
)
GO
INSERT INTO #PerformanceMonitor
([object_name],[counter_name],[instance_name],[cntr_value],[cntr_type])
(SELECT object_name,counter_name,instance_name,cntr_value,cntr_type
FROM sys.dm_os_performance_counters)
WAITFOR DELAY '00:00:10' --Change the interval here
GO 10 --Change the number of times the data will be captured here
SELECT * FROM #PerformanceMonitorContext
StackExchange Database Administrators Q#43223, answer score: 6
Revisions (0)
No revisions yet.