查看SQLServerCPU占用
--看一下当前的数据库用户连接有多少
USE master GO
--如果要指定数据库就把注释去掉
SELECT * FROM sys.[sysprocesses] WHERE [spid]>50 --AND DB_NAME([dbid])='gposdb' SELECT COUNT(*) FROM [sys].[dm_exec_sessions] WHERE [session_id]>50
--查看阻塞的SQL 各项指标是否正常,是否有阻塞,这个语句选取了前10个最耗CPU时间的会话 SELECT TOP 10 [session_id], [request_id], [start_time] AS '开始时间', [status] AS '状态', [command] AS '命令', dest.[text] AS 'sql语句', DB_NAME([database_id]) AS '数据库名', [blocking_session_id] AS '正在阻塞其他会话的会话ID', [wait_type] AS '等待资源类型', [wait_time] AS '等待时间', [wait_resource] AS '等待的资源', [reads] AS '物理读次数', [writes] AS '写次数', [logical_reads] AS '逻辑读次数', [row_count] AS '返回结果行数' FROM sys.[dm_exec_requests] AS der CROSS APPLY sys.[dm_exec_sql_text](der.[sql_handle]) AS dest WHERE [session_id]>50 AND DB_NAME(der.[database_id])='SZVB_UAT' ORDER BY [cpu_time] DESC
--在SSMS里选择以文本格式显示结果 具体的SQL语句可以执行下面的SQL语句,记得在SSMS里选择以文本格式显示结果 SELECT TOP 10 dest.[text] AS 'sql语句' FROM sys.[dm_exec_requests] AS der CROSS APPLY sys.[dm_exec_sql_text](der.[sql_handle]) AS dest WHERE [session_id]>50 ORDER BY [cpu_time] DESC
--查看CPU数和user scheduler数目 SELECT cpu_count,scheduler_count FROM sys.dm_os_sys_info --查看最大工作线程数 SELECT max_workers_count FROM sys.dm_os_sys_info
/* 对照下面这个表 各种CPU和SQLSERVER版本组合自动配置的最大工作线程数 CPU数 32位计算机 64位计算机 <=4 256 512 8 288 576 16 352 704 32 480 960 */
---如果SQLSERVER存在要等待的资源,那么执行下面语句就会显示出会话中有多少个worker在等待 SELECT TOP 10 [session_id], [request_id], [start_time] AS '开始时间', [status] AS '状态', [command] AS '命令', dest.[text] AS 'sql语句', DB_NAME([database_id]) AS '数据库名', [blocking_session_id] AS '正在阻塞其他会话的会话ID', der.[wait_type] AS '等待资源类型', [wait_time] AS '等待时间', [wait_resource] AS '等待的资源', [dows].[waiting_tasks_count] AS '当前正在进行等待的任务数', [reads] AS '物理读次数', [writes] AS '写次数', [logical_reads] AS '逻辑读次数', [row_count] AS '返回结果行数' FROM sys.[dm_exec_requests] AS der INNER JOIN [sys].[dm_os_wait_stats] AS dows ON der.[wait_type]=[dows].[wait_type] CROSS APPLY sys.[dm_exec_sql_text](der.[sql_handle]) AS dest WHERE [session_id]>50 ORDER BY [cpu_time] DESC