USE master GO --如果要指定数据库就把注释去掉 SELECT*FROM sys.[sysprocesses] WHERE [spid]>50--AND DB_NAME([dbid])='gposdb' SELECTCOUNT(*) FROM [sys].[dm_exec_sessions] WHERE [session_id]>50
declare@sqlvarchar(100) while 1=1 begin select top 1@sql='kill '+cast(spid asvarchar(3)) from master..sysprocesses where spid >50and spid <> @@spid if @@rowcount=0 break exec(@sql) end
SELECT TOP 10 st.text AS batch_text, SUBSTRING(st.TEXT, (qs.statement_start_offset /2) +1, ((CASE qs.statement_end_offset WHEN-1THEN DATALENGTH(st.TEXT) ELSE qs.statement_end_offset END- qs.statement_start_offset) /2) +1) AS statement_text, (qs.total_worker_time /1000) / qs.execution_count AS avg_cpu_time_ms, (qs.total_elapsed_time /1000) / qs.execution_count AS avg_elapsed_time_ms, qs.total_logical_reads / qs.execution_count AS avg_logical_reads, (qs.total_worker_time /1000) AS cumulative_cpu_time_all_executions_ms, (qs.total_elapsed_time /1000) AS cumulative_elapsed_time_all_executions_ms FROM sys.dm_exec_query_stats qs CROSS APPLY sys.dm_exec_sql_text(sql_handle) st ORDERBY(qs.total_worker_time / qs.execution_count) DESC
下面的语句我看不太懂
1 2 3 4 5 6 7 8 9 10 11
SELECT TOP 10 total_worker_time/execution_count AS avg_cpu_cost, plan_handle, execution_count, (SELECTSUBSTRING(text, statement_start_offset/2+1, (CASEWHEN statement_end_offset =-1 THEN LEN(CONVERT(nvarchar(max), text)) *2 ELSE statement_end_offset END- statement_start_offset)/2) FROM sys.dm_exec_sql_text(sql_handle)) AS query_text FROM sys.dm_exec_query_stats ORDERBY [avg_cpu_cost] DESC
SELECT name ,count(*) FROM ( SELECT b.name,a.*FROM [Master].[dbo].[SYSPROCESSES] a INNERJOIN [Master].[dbo].[SYSDATABASES] b ON a.dbid=b.dbid )t GROUPBY t.name
查询每一个连接的信息
1 2 3 4 5 6 7 8 9 10 11
SELECT*FROM [Master].[dbo].[SYSPROCESSES] WHERE [DBID] IN ( SELECT [DBID] FROM [Master].[dbo].[SYSDATABASES] WHERE NAME='你的数据库名称' )