sqlserver常用调优脚本
------------------------最耗时的sql---------------------------------------
DECLARE @n INT;
SET @n = 500; WITH cte1
AS ( SELECT a.* ,
t.*
FROM sys.dm_exec_query_stats a
CROSS APPLY sys.dm_exec_sql_text(a.plan_handle) t
WHERE t.dbid >= 5
)
SELECT t.dbid ,
DB_NAME(t.dbid) AS dbname ,
a.total_worker_time ,
a.avg_time_ms ,
a.execution_count ,
a.cache_count ,
REPLACE(REPLACE(t.text, CHAR(10), ' '), CHAR(13), ' ')
FROM ( SELECT TOP ( @n )
plan_handle ,
SUM(total_worker_time) / 1000 AS total_worker_time ,
SUM(execution_count) AS execution_count ,
COUNT(1) AS cache_count ,
( SUM(total_worker_time) / SUM(execution_count) )
/ 1000 AS avg_time_ms
FROM cte1
GROUP BY plan_handle
ORDER BY avg_time_ms DESC
) a
CROSS APPLY sys.dm_exec_sql_text(a.plan_handle) t
WHERE avg_time_ms > 200
ORDER BY avg_time_ms DESC; GO ------------------------最频繁的sql---------------------------------------
DECLARE @n INT;
SET @n = 500;
WITH cte1
AS ( SELECT a.* ,
t.*
FROM sys.dm_exec_query_stats a
CROSS APPLY sys.dm_exec_sql_text(a.plan_handle) t
WHERE t.dbid >= 5
)
SELECT t.dbid ,
DB_NAME(t.dbid) AS dbname ,
a.execution_count ,
a.total_worker_time ,
a.avg_time_ms ,
a.cache_count ,
REPLACE(REPLACE(t.text, CHAR(10), ' '), CHAR(13), ' ')
FROM ( SELECT TOP ( @n )
plan_handle ,
SUM(total_worker_time) / 1000 AS total_worker_time ,
SUM(execution_count) AS execution_count ,
COUNT(1) AS cache_count ,
( SUM(total_worker_time) / SUM(execution_count) )
/ 1000 AS avg_time_ms
FROM cte1
GROUP BY plan_handle
ORDER BY avg_time_ms DESC
) a
CROSS APPLY sys.dm_exec_sql_text(a.plan_handle) t
ORDER BY execution_count DESC;
GO ------------------------耗cpu的sql---------------------------------------
DECLARE @n INT;
SET @n = 500;
WITH cte1
AS ( SELECT a.* ,
t.*
FROM sys.dm_exec_query_stats a
CROSS APPLY sys.dm_exec_sql_text(a.plan_handle) t
WHERE t.dbid >= 5
)
SELECT t.dbid ,
DB_NAME(t.dbid) AS dbname ,
a.total_logical_reads ,
a.avg_reads ,
a.total_logical_writes ,
a.avg_writes ,
a.execution_count ,
a.total_worker_time ,
a.avg_time_ms ,
a.cache_count ,
REPLACE(REPLACE(t.text, CHAR(10), ' '), CHAR(13), ' ')
FROM ( SELECT TOP ( @n )
plan_handle ,
SUM(total_logical_reads) AS total_logical_reads ,
( SUM(total_logical_reads) / SUM(execution_count) ) AS avg_reads ,
SUM(total_logical_writes) AS total_logical_writes ,
( SUM(total_logical_writes) / SUM(execution_count) ) AS avg_writes ,
SUM(execution_count) AS execution_count ,
COUNT(1) AS cache_count ,
SUM(total_worker_time) AS total_worker_time ,
( SUM(total_worker_time) / SUM(execution_count) )
/ 1000 AS avg_time_ms
FROM cte1
GROUP BY plan_handle
ORDER BY ( ( SUM(total_logical_reads) / SUM(execution_count) )
+ ( SUM(total_logical_writes) / SUM(execution_count) ) ) DESC
) a
CROSS APPLY sys.dm_exec_sql_text(a.plan_handle) t
ORDER BY ( avg_reads + avg_writes ) DESC;
GO -----------------------当前数据库可能缺少的索引-------------------------------
SELECT d.* ,
s.avg_total_user_cost ,
s.avg_user_impact ,
s.last_user_seek ,
s.unique_compiles
FROM sys.dm_db_missing_index_group_stats s ,
sys.dm_db_missing_index_groups g ,
sys.dm_db_missing_index_details d
WHERE s.group_handle = g.index_group_handle
AND d.index_handle = g.index_handle
ORDER BY s.avg_user_impact DESC; -----------------------当前数据库没用到的索引-------------------------------
SELECT tb_name = OBJECT_NAME(a.object_id) ,
idx_name = b.name ,
last_user_update ,
c.colid ,
c.keyno ,
col_name = d.name
INTO #tmp
FROM sys.dm_db_index_usage_stats a
LEFT JOIN sys.indexes b ON a.object_id = b.object_id
AND a.index_id = b.index_id
LEFT JOIN sys.sysindexkeys c ON c.id = a.object_id
AND c.indid = a.index_id
LEFT JOIN syscolumns d ON d.id = c.id
AND d.colid = c.colid
WHERE database_id = DB_ID()
AND last_user_seek IS NULL
AND last_user_scan IS NULL
AND last_user_lookup IS NULL
AND last_user_update IS NOT NULL
ORDER BY tb_name ,
idx_name ,
keyno;
SELECT tb_name ,
idx_name ,
last_user_update ,
keywords = STUFF(( SELECT ',' + col_name
FROM #tmp
WHERE tb_name = a.tb_name
AND idx_name = a.idx_name
ORDER BY tb_name ,
idx_name ,
keyno
FOR
XML PATH('')
), 1, 1, '')
FROM #tmp a
GROUP BY tb_name ,
idx_name ,
last_user_update;
DROP TABLE #tmp;
GO ------------------------------当前数据库索引的使用率--------------
SELECT OBJECT_NAME(object_id) AS table_name ,
( SELECT name
FROM sys.indexes
WHERE object_id = stats.object_id
AND index_id = stats.index_id
) AS index_name ,
*
FROM sys.dm_db_index_usage_stats AS stats
WHERE database_id = DB_ID()
ORDER BY table_name; --当前数据库指定表的索引使用情况
DECLARE @table AS NVARCHAR(100);
SET @table = 't_ire_candidate'; SELECT ( SELECT name
FROM sys.indexes
WHERE object_id = stats.object_id
AND index_id = stats.index_id
) AS index_name ,
*
FROM sys.dm_db_index_usage_stats AS stats
WHERE object_id = OBJECT_ID(@table)
ORDER BY user_seeks ,
user_scans ,
user_lookups ASC;
GO -- 查找阻塞,通过 a.blocking_session_id 查看引起阻塞的进程,text是对应的sql脚本
SELECT b.text , -- sql执行计划
b.dbid ,
DB_NAME(b.dbid) AS dbname ,
a.session_id , -- 执行会话id
a.blocking_session_id , -- 引起阻塞的会话id
a.status ,
a.command ,
a.wait_time ,
a.wait_type ,
a.wait_resource ,
a.total_elapsed_time
FROM sys.dm_exec_requests a
CROSS APPLY sys.dm_exec_sql_text(a.sql_handle) b
WHERE session_id IN ( SELECT DISTINCT
request_session_id
FROM sys.dm_tran_locks );
摘录http://www.cnblogs.com/chengxiaohui/articles/6377569.html
sqlserver常用调优脚本的更多相关文章
- sqlserver常用调优脚本(转)
(转)以备不时之需 最耗时的sql declare @n int set @n=500 ; with cte1 as(select a.*,t.*from sys.dm_exec_query_stat ...
- JVM常用调优工具介绍
前言 王子在之前的JVM文章中已经大体上把一些原理性问题说清楚了,今天主要是介绍一些实际进行JVM调优工作的工具和命令,不会深入讲解,因为网上资料很多,篇幅可能不长,但都是实用的内容,小伙伴们有不清楚 ...
- sqlserver性能调优第一步
相信不少的朋友,无论是做开发.架构的,还是DBA等,都经常听说“调优”这个词.说起“调优”,可能会让很多技术人员心头激情澎湃,也可能会让很多人感觉苦恼,不知道如何入手.当然,也有很多人对此不屑一顾,因 ...
- JVM的常用的调优策略和垃圾回收算法及Tomcat的常用调优参数
jvm调优主要针对堆内存,堆内存分为:新生区.养老区和永久区 永久区存放的是系统jdk自身的interface和class的元数据,所以唯有新生区和养老区具有优化空间. 新生区:伊甸区和幸存者区.所有 ...
- Mysql 常用调优命令总结
在工作中,经常会碰到Mysql性能问题,本文记录了调优时会用到的一些命令: 1. show full processlist\G: 显示Mysql服务器正在执行的线程.有root权限的用户可以显示所有 ...
- Linux内存子系统及常用调优参数
1>内存子系统 1>组件: slab allocator buddy system kswapd pdflush 2>虚拟化环境: PA:进程地址: HA:虚拟机地址: ...
- SQLServer性能调优3之索引(Index)的维护
前言 前一篇的文章介绍了通过建立索引来提高数据库的查询性能,这其实只是个开始.后续如果缺少适当的维护,你先前建立的索引甚至会成为拖累,成为数据库性能的下降的帮凶. 查找碎片 消除碎片可能是索引维护最常 ...
- Linux常用调优配置
cenos 6.5 文件句柄和网络端口 修改系统所有进程可用句柄数,vi /etc/sysctl.conf fs.file-max=655360net.ipv4.ip_local_port_range ...
- SQLSERVER性能调优小技巧
平时做个记录,在工作过程中针对大数据查询的一些小技巧 -----------SELECT------------- 1.必要的冗余字段,减少关联查询 2.关键查询字段必须加索引 否则百万级以上你就别想 ...
随机推荐
- Hierarchical query-层次查询之START WITH CONNECT BY用法
Hierarchical query-层次查询中start with...connect by prior子句用法: connect by 是结构化查询中用到的,其基本语法是: select ... ...
- Mysql中使用Group_Concat将列组合进来。
一.上例子: ) as CityId,group_concat(stationId) ,NameCn from `wd-area` where type='cn3k' and areaId like ...
- canvas语法糖
- 【leetcode】solution in java——Easy2
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6410409.html 6:Reverse String Write a function that takes ...
- java HMAC_SHA1加密算法
java HMAC_SHA1加密算法 CreationTime--2018年7月14日16点46分 Author:Marydon 1.准备工作 import javax.crypto.Mac; i ...
- idea 配置svn
1.情景展示 idea安装好后,如何配置svn? 2.分析 由于没有针对idea的svn插件,因此只能选择安装svn的客户端. 3.解决方案 第一步:下载小乌龟SVN客户端: 官网地址:htt ...
- jqPlot图表插件学习之折线图-散点图-series属性
一.准备工作 首先我们需要到官网下载所需的文件: 官网下载(笔者选择的是jquery.jqplot.1.0.8r1250.zip这个版本) 然后读者需要根据自己的情况新建一个项目并且按照如下的方式加载 ...
- Linux中的共享链接库shared libraries
可执行文件的静态链接和动态链接静态链接会将需要的库函数在编译时一并包含, 所以体积会比较大. 使用ldd命令查看可执行文件链接的库 $ ldd /sbin/ldconfig not a dynamic ...
- 监听textarea文本框文本输入情况
// When the value of the text area changes... $("textarea").on("input", function ...
- kubelet Pod status的状态分析
CrashLoopBackOff: 容器退出,kubelet正在将它重启 InvalidImageName: 无法解析镜像名称 ImageInspectError: 无法校验镜像 ErrImageNe ...