当数据库运行比较缓慢时,我们需要实时查看当前有什么Session在运行,获得信息越完整,对于分析低性能的原因越有帮助。根据之前调优的经历,简单几步分析如下:
1.通过SQL Server内置的Sp_who查询
2.更深入的可以通过Sp_who2查询
3.比第2点更丰富的,可以通过下面的语句,不但可以查询出当前executing_statement,还可以获取到当前语句的执行计划
--语句如下:
1 SELECT des.session_id , 2 3 des.status , 4 5 des.login_name , 6 7 des.[HOST_NAME] , 8 9 der.blocking_session_id , 10 11 DB_NAME(der.database_id) AS database_name , 12 13 der.command , 14 15 des.cpu_time , 16 17 des.reads , 18 19 des.writes , 20 21 dec.last_write , 22 23 des.[program_name] , 24 25 der.wait_type , 26 27 der.wait_time , 28 29 der.last_wait_type , 30 31 der.wait_resource , 32 33 CASE des.transaction_isolation_level 34 35 WHEN 0 THEN 'Unspecified' 36 37 WHEN 1 THEN 'ReadUncommitted' 38 39 WHEN 2 THEN 'ReadCommitted' 40 41 WHEN 3 THEN 'Repeatable' 42 43 WHEN 4 THEN 'Serializable' 44 45 WHEN 5 THEN 'Snapshot' 46 47 END AS transaction_isolation_level , 48 49 OBJECT_NAME(dest.objectid, der.database_id) AS OBJECT_NAME , 50 51 SUBSTRING(dest.text, der.statement_start_offset / 2, 52 53 ( CASE WHEN der.statement_end_offset = -1 54 55 THEN DATALENGTH(dest.text) 56 57 ELSE der.statement_end_offset 58 59 END - der.statement_start_offset ) / 2) 60 61 AS [executing statement] , 62 63 deqp.query_plan 64 65 FROM sys.dm_exec_sessions des 66 67 LEFT JOIN sys.dm_exec_requests der 68 69 ON des.session_id = der.session_id 70 71 LEFT JOIN sys.dm_exec_connections dec 72 73 ON des.session_id = dec.session_id 74 75 CROSS APPLY sys.dm_exec_sql_text(der.sql_handle) dest 76 77 CROSS APPLY sys.dm_exec_query_plan(der.plan_handle) deqp 78 79 --WHERE des.session_id <> @@SPID 80 81 ORDER BY des.session_id
---------------------------------------------------------------------------------------------------------
SameZhao