DatawarehouseETLSQL QueriesSQL Tips

How to deal with Long running SQL Query?

By 07/12/2022 December 3rd, 2024 No Comments

Problem Statement:

Long running queries have been found in the plan cache. These may be ETL, reports, or other queries that should run long. Or it could be that someone is searching for all users whose names are LIKE '%%'. Either way, these queries bear investigating.

In some cases, this is the total clock time that the query took to execute and in others this is the total CPU time that the query took to execute. Queries with a high max_elapsed_time just take a lot of time to run – they could be slow single threaded queries. Queries with a high max_worker_time (CPU time) may be highly parallel queries.

Code Snippet:

--Empty the cache
DBCC FREEPROCCACHE
GO

-- Now execute the workload

-- get query stats
SELECT qt.[text] SQLText, qs.*
FROM sys.dm_exec_query_stats AS qs 
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS qt
ORDER BY qs.total_elapsed_time DESC;

Leave a Reply