(转)以备不时之需

最耗时的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

-- 查找阻塞

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
)

通过 a.blocking_session_id 查看引起阻塞的进程,text是对应的sql脚本

sqlserver常用调优脚本(转)的更多相关文章

  1. sqlserver常用调优脚本

    ------------------------最耗时的sql--------------------------------------- DECLARE @n INT; ; WITH cte1 A ...

  2. JVM常用调优工具介绍

    前言 王子在之前的JVM文章中已经大体上把一些原理性问题说清楚了,今天主要是介绍一些实际进行JVM调优工作的工具和命令,不会深入讲解,因为网上资料很多,篇幅可能不长,但都是实用的内容,小伙伴们有不清楚 ...

  3. sqlserver性能调优第一步

    相信不少的朋友,无论是做开发.架构的,还是DBA等,都经常听说“调优”这个词.说起“调优”,可能会让很多技术人员心头激情澎湃,也可能会让很多人感觉苦恼,不知道如何入手.当然,也有很多人对此不屑一顾,因 ...

  4. JVM的常用的调优策略和垃圾回收算法及Tomcat的常用调优参数

    jvm调优主要针对堆内存,堆内存分为:新生区.养老区和永久区 永久区存放的是系统jdk自身的interface和class的元数据,所以唯有新生区和养老区具有优化空间. 新生区:伊甸区和幸存者区.所有 ...

  5. Mysql 常用调优命令总结

    在工作中,经常会碰到Mysql性能问题,本文记录了调优时会用到的一些命令: 1. show full processlist\G: 显示Mysql服务器正在执行的线程.有root权限的用户可以显示所有 ...

  6. Linux内存子系统及常用调优参数

    1>内存子系统 1>组件: slab    allocator buddy    system kswapd pdflush 2>虚拟化环境: PA:进程地址: HA:虚拟机地址: ...

  7. SQLServer性能调优3之索引(Index)的维护

    前言 前一篇的文章介绍了通过建立索引来提高数据库的查询性能,这其实只是个开始.后续如果缺少适当的维护,你先前建立的索引甚至会成为拖累,成为数据库性能的下降的帮凶. 查找碎片 消除碎片可能是索引维护最常 ...

  8. Linux常用调优配置

    cenos 6.5 文件句柄和网络端口 修改系统所有进程可用句柄数,vi /etc/sysctl.conf fs.file-max=655360net.ipv4.ip_local_port_range ...

  9. SQLSERVER性能调优小技巧

    平时做个记录,在工作过程中针对大数据查询的一些小技巧 -----------SELECT------------- 1.必要的冗余字段,减少关联查询 2.关键查询字段必须加索引 否则百万级以上你就别想 ...

随机推荐

  1. Xcode编译WebApps找不到js的错误解决办法<转>

    使用Xcode做WebApps时,使用UIWebview来调用一个页面,有时会遇到问题,其一就是编译的时候出现黄色感叹号的Warning,js文件都报错:warning: no rule to pro ...

  2. 简述Mesos API–files

    Below is a set of endpoints available on a Mesos agent. These endpoints are reachable at the address ...

  3. Linux中的两种守护进程stand alone和xinetd

    Linux中的两种守护进程stand alone和xinetd --http://www.cnblogs.com/itech/archive/2010/12/27/1914846.html#top 一 ...

  4. 由一个Servlet 看java入门常犯的几个错误

    安装完java环境后,cmd-javac 报错           ------------->环境变量配错了,最后全配成系统变量,ok了 能浪费一天的时间 写一个最简单的Servlet ,to ...

  5. RFS_窗口或区域之间的切换

    1.  测试用例描述 [前置条件]: 1. 已经登录系统 [测试步骤]: 1. 验证登录成功 2. 选择[用户管理]菜单 3. 打开[新增用户]页面 4. 输入必填字段,点击[Submit]按钮 [预 ...

  6. javascript实例学习之四——javascript分页

    话不多少,直接上代码 html代码: <!DOCTYPE html> <html lang="en"> <head> <meta char ...

  7. Hadoop学习(3)-- 安装1.x版本

    Hadoop有三种安装模式,分别为单机模式.伪分布式模式和完全分布式模式(集群模式).本文安装版本是hadoop-1.1.2,hadoop-2.x版本安装请参考:http://www.cnblogs. ...

  8. DG - 将physical standby置于read-only模式

    1.备库处于关闭状态 SQL> startup ORACLE instance started. Total System Global Area 430075904 bytes Fixed S ...

  9. WEB urllib2 module note

    收藏好文,看的懂文档,但效率太慢 cookie 清空 import urllib2 import cookielib from time import sleep cookie=cookielib.C ...

  10. Java基础之一组有用的类——使用Scanner对象(TryScanner)

    控制台程序. java.util.Scanner类定义的对象使用正则表达式来扫描来自各种源的字符输入,并把输入显示为各种基本类型的一系列标记或者显示为字符串. 默认情况下,Scanner对象读取标记时 ...