标签:
在初涉SQL Server性能问题(1/4)里,我们知道了如何快速检查服务器实例上正运行的任务数和IO等待的任务数。这个是轻量级的脚本,不会给服务器造成任何压力,即使服务器在高负荷下,也可以正常获得结果。
问题检测的第2步是获取在进行任何资源等待的会话。下面的脚本会帮助我们获得这些信息。这个查询需要预建立一个函数,如果会话是由SQL Server代理启动的话,会显示具体的作业名。
1 /***************************************************************************************** 2 PREREQUISITE FUNCTION 3 ******************************************************************************************/ 4 USE MASTER 5 GO 6 CREATE FUNCTION ConvertStringToBinary ( @hexstring VARCHAR(100) 7 ) RETURNS BINARY(34) AS 8 BEGIN 9 10 RETURN(SELECT CAST(‘‘ AS XML).value(‘xs:hexBinary( substring(sql:variable("@hexstring"), sql:column("t.pos")) )‘, ‘varbinary(max)‘) 11 FROM (SELECT CASE SUBSTRING(@hexstring, 1, 2) WHEN ‘0x‘ THEN 3 ELSE 0 END) AS t(pos)) 12 END 13 /*************************************************************************************** 14 STEP 2: List the session which are currently waiting for resource 15 ****************************************************************************************/ 16 SELECT node.parent_node_id AS Node_id, 17 es.HOST_NAME, 18 es.Login_name, 19 CASE WHEN es.program_name LIKE ‘%SQLAgent - TSQL JobStep%‘ THEN 20 ( 21 SELECT ‘SQL AGENT JOB: ‘+name FROM msdb..sysjobs WHERE job_id= 22 MASTER.DBO.ConvertStringToBinary (LTRIM(RTRIM((SUBSTRING(es.program_name,CHARINDEX(‘(job‘,es.program_name,0)+4,35))))) 23 ) 24 ELSE es.program_name END AS [Program Name] , 25 DB_NAME(er.database_id) AS DatabaseName, 26 er.session_id, 27 wt.blocking_session_id, 28 wt.wait_duration_ms, 29 wt.wait_type, 30 wt.NoThread , 31 er.command, 32 er.status, 33 er.wait_resource, 34 er.open_transaction_count, 35 er.cpu_time, 36 er.total_elapsed_time AS ElapsedTime_ms, 37 er.percent_complete , 38 er.reads, 39 er.writes, 40 er.logical_reads, 41 wlgrp.name AS ResoursePool , 42 SUBSTRING (sqltxt.TEXT,(er.statement_start_offset/2) + 1, 43 ((CASE WHEN er.statement_end_offset = -1 44 THEN LEN(CONVERT(NVARCHAR(MAX), sqltxt.TEXT)) * 2 45 ELSE er.statement_end_offset 46 END - er.statement_start_offset)/2) + 1) AS [Individual Query], 47 sqltxt.TEXT AS [Batch Query] 48 FROM (SELECT session_id, SUM(wait_duration_ms) AS 49 wait_duration_ms,wait_type,blocking_session_id,COUNT(*) AS NoThread 50 FROM SYS.DM_OS_WAITING_TASKS GROUP BY session_id, wait_type,blocking_session_id) wt 51 INNER JOIN SYS.DM_EXEC_REQUESTS er ON wt.session_id=er.session_id INNER JOIN SYS.DM_EXEC_SESSIONS es ON es.session_id= er.session_id 52 INNER JOIN SYS.DM_RESOURCE_GOVERNOR_WORKLOAD_GROUPS wlgrp ON wlgrp.group_id=er.group_id 53 INNER JOIN (SELECT os.parent_node_id ,task_address FROM SYS.DM_OS_SCHEDULERS OS INNER JOIN 54 SYS.DM_OS_WORKERS OSW ON OS.scheduler_address=OSW.scheduler_address 55 WHERE os.status=‘VISIBLE ONLINE‘ GROUP BY os.parent_node_id ,task_address ) node 56 ON node.task_address=er.task_address 57 CROSS APPLY SYS.DM_EXEC_SQL_TEXT(er.sql_handle) AS sqltxt 58 WHERE sql_handle IS NOT NULL AND wt.wait_type NOT IN (‘WAITFOR‘,‘BROKER_RECEIVE_WAITFOR‘) 59 GO
输出结果的每列说明介绍如下:
上述查询多次执行后,输出结果有很长wait_duration_ms的会话,这个会话不被其他会话阻塞,且一直在输出结果里。我们就要看看这个会话的程序名,主机名,登录用户名,还有对应的执行语句,具体进行什么操作造成的。根据这些信息,我们可以选择性的去终止这个会话,然后分析下具体的执行语句。如果会话是被阻塞的,我们要用另外的语句来找出阻塞的会话。
第3步,列出服务器上正运行的会话清单。
1 /*************************************************************************************** 2 STEP 3: List the session which are currently waiting/running 3 ****************************************************************************************/ 4 SELECT node.parent_node_id AS Node_id, 5 es.HOST_NAME, 6 es.login_name, 7 CASE WHEN es.program_name LIKE ‘%SQLAgent - TSQL JobStep%‘ THEN 8 (SELECT ‘SQL AGENT JOB: ‘+name FROM msdb..sysjobs WHERE job_id=DBO.ConvertStringToBinary (LTRIM(RTRIM((SUBSTRING(es.program_name,CHARINDEX(‘(job‘,es.program_name,0)+4,35))))) 9 )ELSE es.program_name END AS program_name , 10 DB_NAME(er.database_id) AS DatabaseName, 11 er.session_id, 12 wt.blocking_session_id, 13 wt.wait_duration_ms, 14 wt.wait_type, 15 wt.NoThread , 16 er.command, 17 er.status, 18 er.wait_resource, 19 er.open_transaction_count, 20 er.cpu_time, 21 er.total_elapsed_time AS ElapsedTime_ms, 22 er.percent_complete , 23 er.reads,er.writes,er.logical_reads, 24 wlgrp.name AS ResoursePool , 25 SUBSTRING (sqltxt.TEXT,(er.statement_start_offset/2) + 1, 26 ((CASE WHEN er.statement_end_offset = -1 27 THEN LEN(CONVERT(NVARCHAR(MAX), sqltxt.TEXT)) * 2 28 ELSE er.statement_end_offset 29 END - er.statement_start_offset)/2) + 1) AS [Individual Query], 30 sqltxt.TEXT AS [Batch Query] 31 FROM 32 SYS.DM_EXEC_REQUESTS er INNER JOIN SYS.DM_EXEC_SESSIONS es ON es.session_id= er.session_id 33 INNER JOIN SYS.DM_RESOURCE_GOVERNOR_WORKLOAD_GROUPS wlgrp ON wlgrp.group_id=er.group_id 34 INNER JOIN (SELECT os.parent_node_id ,task_address FROM SYS.DM_OS_SCHEDULERS OS 35 INNER JOIN SYS.DM_OS_WORKERS OSW ON OS.scheduler_address=OSW.scheduler_address 36 WHERE os.status=‘VISIBLE ONLINE‘ GROUP BY os.parent_node_id ,task_address ) node ON node.task_address=er.task_address 37 LEFT JOIN 38 (SELECT session_id, SUM(wait_duration_ms) AS 39 wait_duration_ms,wait_type,blocking_session_id,COUNT(*) AS NoThread 40 FROM SYS.DM_OS_WAITING_TASKS GROUP BY session_id, wait_type,blocking_session_id) wt 41 ON wt.session_id=er.session_id 42 CROSS apply SYS.DM_EXEC_SQL_TEXT(er.sql_handle) AS sqltxt 43 WHERE sql_handle IS NOT NULL AND ISNULL(wt.wait_type ,‘‘) NOT IN 44 (‘WAITFOR‘,‘BROKER_RECEIVE_WAITFOR‘) 45 ORDER BY er.total_elapsed_time DESC 46 47 GO
这里的输出列和第2步完全相同,我会分析total_elapsed_time占用时间较长的会话,酌情考虑是否终止这些会话,并分析下对应的执行SQL语句。大多数情况下(服务器一致运行稳定,突然卡住了),使用上述步骤就可以解决问题。下一篇文章我们会看下阻塞的会话,还有打开未活动事务的会话。
初涉SQL Server性能问题(2/4):列出等待资源的会话
标签:
原文地址:http://www.cnblogs.com/woodytu/p/4543544.html