SQLServer 维护脚本分享(06)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
SQLServer 维护脚本分享(06)CPU的更多相关文章
- SQLServer 维护脚本分享(11)部分DBCC及系统存储过程
--DBCC命令与用法 DBCC HELP('?') DBCC HELP('useroptions') DBCC USEROPTIONS WITH NO_INFOMSGS --当前DB的区及文件 DB ...
- SQLServer 维护脚本分享(05)内存(Memory)
--查看设置的最大最小每次 exec sp_configure 'max server memory (MB)' exec sp_configure 'min server memory (MB)' ...
- SQLServer 维护脚本分享(08)临时数据库(tempdb)
dbcc sqlperf(logspace) --各数据库日志大小及使用百分比 dbcc loginfo --查看当前数据库的虚拟日志文件 --临时表'Tempdb'最近使用情况 SELECT t1. ...
- SQLServer 维护脚本分享(09)相关文件读取
/********************[读取跟踪文件(trc)]********************/ --查看事件类型描述 SELECT tc.name,te.trace_event_id, ...
- SQLServer 维护脚本分享(04)服务器角色和数据库角色相关操作
/*------------------------------------------------------------------------------------ [服务器级别-服务器角色] ...
- SQLServer 维护脚本分享(07)IO
sp_helpfile --当前数据库文件分配情况 sp_spaceused --当前db空间大小(有时不准) sp_spaceused 'dbo.user' --指定表的空间大小(有时不准) sp_ ...
- SQLServer 维护脚本分享(10)索引
--可添加索引的字段 migs.user_seeks,migs.avg_total_user_cost,migs.avg_user_impact,migs.last_user_seek ,mid.st ...
- SQL Server定时自动抓取耗时SQL并归档数据发邮件脚本分享
SQL Server定时自动抓取耗时SQL并归档数据发邮件脚本分享 第一步建库和建表 USE [master] GO CREATE DATABASE [MonitorElapsedHighSQL] G ...
- SQL Server定时自动抓取耗时SQL并归档数据脚本分享
原文:SQL Server定时自动抓取耗时SQL并归档数据脚本分享 SQL Server定时自动抓取耗时SQL并归档数据脚本分享 第一步建库 USE [master] GO CREATE DATABA ...
随机推荐
- ACM/ICPC 之 BFS范例(ZOJ2913-ZOJ1136(POJ1465))
通过几道经典BFS例题阐述BFS思路 ZOJ2913-Bus Pass 题意:找一个center区域,使得center到所有公交线路最短,有等距的center则输出id最小的. 题解:经典的BFS,由 ...
- bing壁纸xml地址
http://www.bing.com/gallery/?src=livesino# http://www.bing.com/HPImageArchive.aspx?format=xml&id ...
- SlimDX.dll安装之后所在位置
C:\Windows\Microsoft.NET\assembly\GAC_64\SlimDX\v4.0_4.0.13.43__b1b0c32fd1ffe4f9\SlimDX.dll
- IntelliJ IDEA 15.0.4常用快捷键整理
一.背景 最近刚转了IDEA,感觉真是爽的一逼,太智能了,回不去Eclipse了,还有些淡淡的忧伤呢~在使用中很多的快捷键帮了开发的大忙,让我可以达到事半功倍的效果,下面就罗列出来,与大家共同分享. ...
- W3C对DOM2.0定义的标准事件
DOM2.0模型将事件处理流程分为三个阶段: 一.事件捕获阶段, 二.事件目标阶段, 三.事件起泡阶段. 具体如图(图片来源于网络,侵删) 事件捕获:当某个元素触发某个事件(如onclick),顶层对 ...
- iOS开发--UIDatePicker
UIDatePicker 是一个控制器类,封装了 UIPickerView,但是他是UIControl的子类,专门用于接受日期.时间和持续时长的输入.日期选取器的各列会按照指定的风格进行自动配置,这样 ...
- 微信token验证失败的解决方法
一.问题由来 在使用URL和Token启用微信公众平台开发模式消息接口的时候,我们会碰到下面三种情况 1. token校验失败 这样回头检查一下各项配置是否正确.如果确定配置没有问题,请按下面的方法检 ...
- MyEclipse2014配置Tomcat开发JavaWeb程序JSP以及Servlet(转载)
转载地址:http://blog.csdn.net/21aspnet/article/details/21867241 1.安装准备 1).下载安装MyEclipse2014,这已经是最新版本. 2) ...
- 如何在java中使用别人提供的jar包进行导入,编译,运行
一步一步往前走, 现在折分! JAR包即为上篇文章的东东. 测试JAVA文件. package com.security; import com.security.AESencrp; /** * 实现 ...
- SQL Server排序规则
在使用数据库的过程中,总会碰到一些特别的需求.有时候需要储存中文字符,区分大小写或者按照中文的比划顺序排序.这就涉及到了对数据库排列规则的选择. 我们一般可以选择数据库名称-->右键属性(Pro ...