SQL Server 查看CPU情况

--CPU相关视图
SELECT * FROM sys.dm_os_sys_info
SELECT * FROM sys.dm_exec_sessions
SELECT * FROM sys.sysprocesses
SELECT * FROM sys.dm_os_tasks
SELECT * FROM sys.dm_os_workers
SELECT * FROM sys.dm_os_threads
SELECT * FROM sys.dm_os_schedulers
SELECT * FROM sys.dm_os_memory_objects
SELECT * FROM sys.dm_os_nodes
SELECT * FROM sys.dm_os_memory_nodes  

exec sp_configure ‘max degree of parallelism‘--系统默认并行度
exec sp_configure ‘cost threshold for parallelism‘ --并发阈值
exec sp_configure ‘max worker threads‘--系统最大工作线程数
exec sp_configure ‘affinity mask‘ --CPU关联  

--数据库系统 cpu,线程 数量
select max_workers_count,scheduler_count,cpu_count,hyperthread_ratio
,(hyperthread_ratio/cpu_count) AS physical_cpu_count
,(max_workers_count/scheduler_count) AS workers_per_scheduler_limit
from sys.dm_os_sys_info  

--执行的线程所遇到的所有等待的相关信息
SELECT TOP 10 wait_type,waiting_tasks_count,signal_wait_time_ms
FROM sys.dm_os_wait_stats ORDER BY signal_wait_time_ms DESC  

--正在等待某些资源的任务的等待队列的信息
SELECT TOP 10 wait_type,wait_duration_ms,session_id,blocking_session_id
FROM sys.dm_os_waiting_tasks ORDER BY wait_duration_ms DESC  

--CPU或调度器当前分配的工作情况
SELECT scheduler_id,cpu_id,status,is_idle
,current_tasks_count AS 当前任务数           --在等待或运行的任务
,runnable_tasks_count AS 等待调度线程数        --已分配任务并且正在可运行队列中
,current_workers_count AS 当前线程数     --相关或未分配任何任务的工作线程
,active_workers_count AS 活动线程数          --在运行、可运行或挂起
,work_queue_count AS 挂起任务数              --等待工作线程执行
FROM sys.dm_os_schedulers
WHERE scheduler_id < 255  

--当前线程数
select COUNT(*) as 当前线程数 from sys.dm_os_workers  

--非SQL server create的threads
select * from sys.dm_os_threads where started_by_sqlservr=0 --即scheduler_id > 255  

--有task 等待worker去执行
select * from sys.dm_os_tasks where task_state=‘PENDING‘  

--计数器
select * from  sys.dm_os_performance_counters where object_name=‘SQLServer:SQL Statistics‘
select * from  sys.dm_os_performance_counters where object_name=‘SQLServer:Plan Cache‘  

-----------------------------------------------------------------------------
-----------------------------------------------------------------------------  

--1. 实例累积的信号(线程/CPU)等待比例是否严重
SELECT CAST(100.0 * SUM(signal_wait_time_ms) / SUM (wait_time_ms) AS NUMERIC(20,2))  AS [%signal (cpu) waits],
CAST(100.0 * SUM(wait_time_ms - signal_wait_time_ms) / SUM (wait_time_ms) AS  NUMERIC(20,2)) AS [%resource waits]
FROM sys.dm_os_wait_stats WITH (NOLOCK) OPTION (RECOMPILE);   

--2. SqlServer各等待类型的线程等待信息
SELECT TOP 20
wait_type,waiting_tasks_count ,wait_time_ms,signal_wait_time_ms
,wait_time_ms - signal_wait_time_ms AS resource_wait_time_ms
,CONVERT(NUMERIC(14,2),100.0 * wait_time_ms /SUM (wait_time_ms ) OVER( )) AS percent_total_waits
,CONVERT(NUMERIC(14,2),100.0 * signal_wait_time_ms /SUM (signal_wait_time_ms) OVER( )) AS percent_total_signal_waits
,CONVERT(NUMERIC(14,2),100.0 * ( wait_time_ms - signal_wait_time_ms )/SUM (wait_time_ms ) OVER( )) AS percent_total_resource_waits
FROM sys .dm_os_wait_stats
WHERE wait_time_ms > 0
ORDER BY percent_total_signal_waits DESC  

--3. 闩锁(latch)等待的信息
select top 20 latch_class,waiting_requests_count,wait_time_ms,max_wait_time_ms
from sys.dm_os_latch_stats
order by wait_time_ms desc  

--使用最多处理器时间的用户数据库
;WITH DB_CPU_Stats AS  (
    SELECT DatabaseID, DB_Name(DatabaseID) AS [DatabaseName],
    SUM(total_worker_time) AS [CPU_Time_Ms]
    FROM sys.dm_exec_query_stats AS qs
    CROSS APPLY (SELECT CONVERT(int, value) AS [DatabaseID]
    FROM sys.dm_exec_plan_attributes(qs.plan_handle)
    WHERE attribute = N‘dbid‘) AS F_DB
    GROUP BY DatabaseID)
SELECT ROW_NUMBER() OVER(ORDER BY [CPU_Time_Ms] DESC) AS [row_num], DatabaseName
, [CPU_Time_Ms], CAST([CPU_Time_Ms] * 1.0
    / SUM([CPU_Time_Ms])OVER() * 100.0 AS DECIMAL(5,2)) AS [CPUPercent]
FROM DB_CPU_Stats
WHERE DatabaseID > 4 -- system databases
AND DatabaseID <> 32767 -- ResourceDB
ORDER BY row_num OPTION (RECOMPILE);  

--缓存中最耗CPU的语句
select total_cpu_time,total_execution_count,number_of_statements,[text]
from (
    select top 20
    sum(qs.total_worker_time) as total_cpu_time,
    sum(qs.execution_count) as total_execution_count,
    count(*) as  number_of_statements,
    qs.plan_handle
    from sys.dm_exec_query_stats qs
    group by qs.plan_handle
    order by total_cpu_time desc
) eqs cross apply sys.dm_exec_sql_text(eqs.plan_handle) as est
order by total_cpu_time desc  

/*【ask 让出scheduler :worker yielding】
1. worker读数据页超过4ms
2. 64k结果集排序
3. compile或recompile(常有)
4. 客户端不能及时取走结果集
5. batch 的每个操作完整
*/  

--当前正在执行的语句
SELECT
der.[session_id],der.[blocking_session_id],
sp.lastwaittype,sp.hostname,sp.program_name,sp.loginame,
der.[start_time] AS ‘开始时间‘,
der.[status] AS ‘状态‘,
der.[command] AS ‘命令‘,
dest.[text] AS ‘sql语句‘,
DB_NAME(der.[database_id]) AS ‘数据库名‘,
der.[wait_type] AS ‘等待资源类型‘,
der.[wait_time] AS ‘等待时间‘,
der.[wait_resource] AS ‘等待的资源‘,
der.[reads] AS ‘物理读次数‘,
der.[writes] AS ‘写次数‘,
der.[logical_reads] AS ‘逻辑读次数‘,
der.[row_count] AS ‘返回结果行数‘
FROM sys.[dm_exec_requests] AS der
INNER JOIN master.dbo.sysprocesses AS sp on der.session_id=sp.spid
CROSS APPLY  sys.[dm_exec_sql_text](der.[sql_handle]) AS dest
WHERE [session_id]>50 AND session_id<>@@SPID AND DB_NAME(der.[database_id])=‘platform‘
ORDER BY [cpu_time] DESC  

--实例级最大的瓶颈
WITH Waits AS
(
    SELECT wait_type , wait_time_ms / 1000. AS wait_time_s , 100.* wait_time_ms / SUM(wait_time_ms) OVER ( ) AS pct ,
    ROW_NUMBER() OVER ( ORDER BY wait_time_ms DESC ) AS rn
    FROM sys.dm_os_wait_stats
    WHERE
    wait_type NOT IN ( ‘CLR_SEMAPHORE‘, ‘LAZYWRITER_SLEEP‘, ‘RESOURCE_QUEUE‘, ‘SLEEP_TASK‘, ‘SLEEP_SYSTEMTASK‘, ‘SQLTRACE_BUFFER_FLUSH‘, ‘WAITFOR‘, ‘LOGMGR_QUEUE‘,
    ‘CHECKPOINT_QUEUE‘, ‘REQUEST_FOR_DEADLOCK_SEARCH‘, ‘XE_TIMER_EVENT‘, ‘BROKER_TO_FLUSH‘, ‘BROKER_TASK_STOP‘, ‘CLR_MANUAL_EVENT‘, ‘CLR_AUTO_EVENT‘,
    ‘DISPATCHER_QUEUE_SEMAPHORE‘, ‘FT_IFTS_SCHEDULER_IDLE_WAIT‘, ‘XE_DISPATCHER_WAIT‘, ‘XE_DISPATCHER_JOIN‘ )
)
SELECT W1.wait_type , CAST(W1.wait_time_s AS DECIMAL(12, 2)) AS wait_time_s ,
CAST(W1.pct AS DECIMAL(12, 2)) AS pct ,CAST(SUM(W2.pct) AS DECIMAL(12, 2)) AS running_pct
FROM Waits AS W1
INNER JOIN Waits AS W2 ON W2.rn <= W1.rn
GROUP BY W1.rn , W1.wait_type , W1.wait_time_s , W1.pct
HAVING SUM(W2.pct) - W1.pct < 95 ; -- percentage threshold  

原文地址:https://www.cnblogs.com/turingchang/p/8297844.html

时间: 2024-10-09 04:05:20

SQL Server 查看CPU情况的相关文章

SQL Server查看索引重建、重组索引进度

原文:SQL Server查看索引重建.重组索引进度 相信很多SQL Server DBA或开发人员在重建或重组大表索引时,都会相当郁闷,不知道索引重建的进度,这个对于DBA完全是一个黑盒子,对于系统负载非常大的系统或维护窗口较短的系统,你会遇到一些挑战.例如,你创建索引的时候,很多会话被阻塞,你只能取消创建索引的任务.查看这些索引维护操作的进度.预估时间对于我们有较大的意义,需要根据这个做一些决策.下面我们来看看看看如何获取CREATE INDEX.ALTER INDEX REBUILD.AL

SQL Server查看表结构及视图,适合开发者使用,简单易用

SELECT * FROM INFORMATION_SCHEMA.TABLES SELECT * FROM INFORMATION_SCHEMA.COLUMNS 查看执行结果 SQL Server查看表结构及视图,适合开发者使用,简单易用,布布扣,bubuko.com

SQL SERVER 查看SQL语句IO,时间,索引消耗

1.查看SQL语句IO消耗 set statistics io on     select * from dbo.jx_order where order_time>'2011-04-12 12:49:57.580' set statistics io off 2.查看SQL语句时间消耗 set statistics time on      select * from dbo.jx_order where order_time>'2011-04-12 12:49:57.580' set st

sql server 查看列备注、类型、字段大小

select 列名 = a.name ,类型 = c.name ,长度 = columnproperty(a.id,a.name,'precision') ,备注 = g.value from syscolumns a left join sysobjects b on a.id=b.id left join systypes c on a.xusertype = c.xusertype left join sysproperties g on a.id =g.id and a.colid =g

top查看CPU情况

Linux查看CPU情况 在系统维护的过程中,随时可能有需要查看 CPU 使用率,并根据相应信息分析系统状况的需要.在 CentOS 中,可以通过 top 命令来查看 CPU 使用状况.运行 top 命令后,CPU 使用状态会以全屏的方式显示,并且会处在对话的模式 -- 用基于 top 的命令,可以控制显示方式等等.退出 top 的命令为 q (在 top 运行中敲 q 键一次). 1.可以按进程查看 可以用ps -ef 查看进程的PID,再top -p PID  查看用户CPU及内存使用情况2

SQL SERVER中什么情况会导致索引查找变成索引扫描

SQL Server 中什么情况会导致其执行计划从索引查找(Index Seek)变成索引扫描(Index Scan)呢? 下面从几个方面结合上下文具体场景做了下测试.总结.归纳. 1:隐式转换会导致执行计划从索引查找(Index Seek)变为索引扫描(Index Scan) Implicit Conversion will cause index scan instead of index seek. While implicit conversions occur in SQL Serve

Sql Server 查看所有存储过程或视图的位置及内容

原文:Sql Server 查看所有存储过程或视图的位置及内容 select a.name,a.[type],b.[definition] from sys.all_objects a,sys.sql_modules b where a.is_ms_shipped=0 and a.object_id = b.object_id and a.[type] in ('P','V','AF') order by a.[name] asc 通过这个sql语句可以查到sql server中的视图和存储过程

sql server 查看对象最后修改时间

sql server 查看对象最后修改时间,根据最后修改时间排序 存储过程 SELECT * FROM sys.all_objects WHERE  TYPE='P' ORDER BY modify_date DESC; 视图 SELECT * FROM sys.all_objects WHERE  TYPE='v' ORDER BY modify_date DESC; 表 SELECT * FROM sys.all_objects WHERE  TYPE='u' ORDER BY modify

lock(1)——创建及更新表过程中SQL SERVER锁资源分配情况

锁应该说是由关系型数据库ACID(Atomicity,Consistency,Isolation,Durability)特性而引出的. 以下将测试在创建及更新表过程中SQL Server锁资源分配情况 获取当前会话的事务隔离级别:DBCC USEROPTIONS 测试环境:SQL SERVER 2008 R2 read committed隔离级别下 创建表 当我们只是打开一个SSMS查询窗口,选择数据库为master和tempdb时,没有任何锁产生,当我选择其他数据库,sql server就会在