--当前的数据库用户连接情况--
1 SELECT * FROM sys.[sysprocesses] WHERE [spid]>50 AND DB_NAME([dbid])='db_name' 2 SELECT * FROM [sys].[dm_exec_sessions] WHERE [session_id]>50
---选取了前10个最耗CPU时间的会话--
1 SELECT TOP 10 2 [session_id], 3 [request_id], 4 [start_time] AS '开始时间', 5 [status] AS '状态', 6 [command] AS '命令', 7 dest.[text] AS 'sql语句', 8 DB_NAME([database_id]) AS '数据库名', 9 [blocking_session_id] AS '正在阻塞其他会话的会话ID', 10 [wait_type] AS '等待资源类型', 11 [wait_time] AS '等待时间', 12 [wait_resource] AS '等待的资源', 13 [reads] AS '物理读次数', 14 [writes] AS '写次数', 15 [logical_reads] AS '逻辑读次数', 16 [row_count] AS '返回结果行数' 17 FROM sys.[dm_exec_requests] AS der 18 CROSS APPLY 19 sys.[dm_exec_sql_text](der.[sql_handle]) AS dest 20 WHERE [session_id]>50 AND DB_NAME(der.[database_id])='db_name' 21 ORDER BY [cpu_time] DESC
---选取了前10个最耗CPU时间的SQL---
1 SELECT TOP 10 2 dest.[text] AS 'sql语句' 3 FROM sys.[dm_exec_requests] AS der 4 CROSS APPLY 5 sys.[dm_exec_sql_text](der.[sql_handle]) AS dest 6 WHERE [session_id]>50 7 ORDER BY [cpu_time] DESC
1 SELECT TOP 10 2 total_worker_time/execution_count AS avg_cpu_cost, plan_handle, 3 execution_count, 4 (SELECT SUBSTRING(text, statement_start_offset/2 + 1, 5 (CASE WHEN statement_end_offset = -1 6 THEN LEN(CONVERT(nvarchar(max), text)) * 2 7 ELSE statement_end_offset 8 END - statement_start_offset)/2) 9 FROM sys.dm_exec_sql_text(sql_handle)) AS query_text 10 FROM sys.dm_exec_query_stats 11 ORDER BY [avg_cpu_cost] DESC
----查询缺失索引----
1 SELECT 2 DatabaseName = DB_NAME(database_id) 3 ,[Number Indexes Missing] = count(*) 4 FROM sys.dm_db_missing_index_details 5 GROUP BY DB_NAME(database_id) 6 ORDER BY 2 DESC;
1 SELECT TOP 10 2 [Total Cost] = ROUND(avg_total_user_cost * avg_user_impact * (user_seeks + user_scans),0) 3 , avg_user_impact 4 , TableName = statement 5 , [EqualityUsage] = equality_columns 6 , [InequalityUsage] = inequality_columns 7 , [Include Cloumns] = included_columns 8 FROM sys.dm_db_missing_index_groups g 9 INNER JOIN sys.dm_db_missing_index_group_stats s 10 ON s.group_handle = g.index_group_handle 11 INNER JOIN sys.dm_db_missing_index_details d 12 ON d.index_handle = g.index_handle 13 ORDER BY [Total Cost] DESC;