0

Every couple of months, for about 30 minutes, I see my SQL Server CPU shoot up, running around the 80% usage mark.

I finally caught it in the act running this query on tempdb:

SET NOCOUNT ON;

DECLARE @previous_collection_time datetime;
DECLARE @previous_request_count bigint;
DECLARE @current_collection_time datetime;
DECLARE @current_request_count bigint;
DECLARE @batch_requests_per_sec bigint;
DECLARE @interval_sec bigint;

-- Get the previous snapshot's time and batch request count
SELECT TOP 1 @previous_collection_time = collection_time, @previous_request_count = request_count 
FROM #am_request_count
ORDER BY collection_time DESC;

-- Get the current total time and batch request count
SET @current_collection_time = GETDATE();
SELECT @current_request_count = cntr_value 
FROM sys.sysperfinfo
WHERE counter_name = 'Batch Requests/sec' COLLATE Latin1_General_BIN;

SET @interval_sec = 
    -- Avoid divide-by-zero
    CASE
        WHEN DATEDIFF (second, @previous_collection_time, @current_collection_time) = 0 THEN 1
        ELSE DATEDIFF (second, @previous_collection_time, @current_collection_time)
    END;

-- Calc the Batch Requests/sec rate for the just-completed time interval. 
SET @batch_requests_per_sec = (@current_request_count - @previous_request_count) / @interval_sec;

-- Save off current batch count
INSERT INTO #am_request_count (collection_time, request_count) 
VALUES (@current_collection_time, @current_request_count);

-- Return the batch requests/sec rate for the just-completed time interval. 
SELECT ISNULL (@batch_requests_per_sec, 0) AS batch_requests_per_sec;

-- Get rid of all but the most recent snapshot's data
DELETE FROM #am_request_count WHERE collection_time < @current_collection_time;

Not being an expert for SQL Server, does anyone know what's going on? And how can I prevent this / schedule this?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
D-W
  • 5,201
  • 14
  • 47
  • 74
  • 1
    See https://social.msdn.microsoft.com/Forums/sqlserver/en-US/3659b2eb-f920-4cb0-ad6d-468a298e0451/sa-using-temp-db?forum=sqldatabaseengine – JohnLBevan Nov 10 '17 at 16:34
  • Possible duplicate of [Where are all those SQL Server sessions from?](https://stackoverflow.com/questions/4021428/where-are-all-those-sql-server-sessions-from) – JohnLBevan Nov 10 '17 at 16:35

0 Answers0