SQL Server 很慢很卡?可以试着查找影响数据库性能的SQL语句

时间:2025-04-02 07:47:22

使用SQL Server数据库的时候,数据库耗资源很大、卡死,怎么办?可以试着找找是否有执行很慢的SQL语句......

选择【master】数据库,点击【新建查询】,执行SQL语句:

SELECT TOP 30
total_worker_time/1000 AS [CPU总耗时(ms)], 
execution_count [运行次数], 
qs.total_worker_time/qs.execution_count/1000 AS [CPU平均耗时(ms)], 
last_execution_time AS [最后执行时间], 
max_worker_time/1000 AS [最大执行耗时(ms)], 
SUBSTRING(,qs.statement_start_offset/2+1, 
 (CASE WHEN qs.statement_end_offset = -1 
 THEN DATALENGTH() 
 ELSE qs.statement_end_offset 
 END -qs.statement_start_offset)/2 + 1) AS [SQL语句]
FROM sys.dm_exec_query_stats qs WITH(nolock)
CROSS apply sys.dm_exec_sql_text(qs.sql_handle) AS qt
WHERE execution_count > 1
ORDER BY max_worker_time DESC