使用profiler建立跟踪,将跟踪结果存到表中,使用下面存储过程执行 exec temp_profiler 'tra_tablename'对表数据进行处理归类,然后进行性能分析

1.先建存储过程

2.再执行下面的查询

--处理trace数据
--exec temp_profiler 'temp_profiler201509061618'
select top 10000 * from temp_profiler201509061618 order by reads desc
--1)查出最耗时的语句或过程
select * from temp_profiler201509061618_stat order by total_duration desc
--3) 统计某个过程或者sql语句的个数
select proc_sql_id ,count(1) counts from temp_profiler201509061618 group by proc_sql_id order by counts desc
--2)查询某个过程或者sql语句详情
select * from temp_profiler201509061618 where proc_sql_id = 3--
select * from temp_profiler201509061618 where proc_sql_id = 22--
select * from temp_profiler201509061618 where proc_sql_id = 18--

exec temp_profiler 'temp_profiler201508210920'
select top 10000 * from temp_profiler201508210920 order by reads desc
select * from temp_profiler201508210920_stat order by total_duration desc
select  proc_sql_id ,count(1) counts from  temp_profiler201508210920  group by  proc_sql_id  order by counts desc
select  *  from  temp_profiler201508210920  where proc_sql_id = 2--5761
select * from temp_profiler_2015060816 where textdata like 'exec PageList%'

USE [temp_profiler]
GO /****** Object: StoredProcedure [dbo].[temp_profiler] Script Date: 08/26/2015 10:16:31 ******/
SET ANSI_NULLS ON
GO SET QUOTED_IDENTIFIER ON
GO -- =============================================
--http://www.cnblogs.com/davidhou
-- Author: <houpeidong>
-- Create date: <20150611>
-- Description: <profiler抓取到的数据存入表中,此存储过程功能为性能分析工具,表名不能加中括号>
-- =============================================
CREATE PROCEDURE [dbo].[temp_profiler]
@tblName NVARCHAR(500)
as
begin SET NOCOUNT ON --DECLARE @tblName NVARCHAR(500) ;
--set @tblName = 'temp_profiler_20151' ; DECLARE @sqlStr NVARCHAR(2000); --10. 对trace表的数据进行处理前的一些准备:
-- 时间改为毫秒
SET @sqlStr = N'update ' + @tblName +' set duration = duration / 1000 where duration is not null ';
EXECUTE sp_executesql @sqlStr ; --update temp_profiler_2015060917 set duration = duration / 1000 where duration is not null -- 时间改为毫秒 --修改 textdata 为 nvarchar(max)类型,因为textdata默认保存为ntext类型,处理不方便
SET @sqlStr = N'alter table '+@tblName+' alter column textdata nvarchar(max)';
EXECUTE sp_executesql @sqlStr ;
--alter table temp_profiler_2015060917 alter column textdata nvarchar(max) --新增两个字段
SET @sqlStr = N'alter table '+@tblName+' add proc_sql nvarchar(max)';
EXECUTE sp_executesql @sqlStr ;
--alter table temp_profiler_2015060917 add proc_sql nvarchar(max) -- 保存该textdata调用的存储过程,原始sql等; SET @sqlStr = N'alter table '+@tblName+' add proc_sql_id int';
EXECUTE sp_executesql @sqlStr ;
--alter table temp_profiler_2015060917 add proc_sql_id int -- 为存储过程和原始sql指定一个编号 --11. 处理trace数据
-- 1)找出执行的sql脚本(带参数) ,更新到 proc_sql 字段
SET @sqlStr = N'
update '+@tblName+'
set proc_sql = replace(left(textdata,charindex('''''',N'''''',textdata) - 1),''exec sp_executesql N'''''','''')
where (proc_sql is null or proc_sql = '''' )
and charindex(''exec sp_executesql N'', textdata ) = 1
';
EXECUTE sp_executesql @sqlStr ; --2)找出执行的存储过程,更新到 proc_sql 字段
SET @sqlStr = N'
update '+@tblName+'
set proc_sql =
replace(
replace(
left(
right(textdata,len(textdata) - charindex(''exec '',textdata) + 3),
charindex(''@'',
right(textdata,len(textdata) - charindex(''exec '',textdata) + 3)
)
),''exec '','''')
,''@'','''')
where (proc_sql is null or proc_sql = '''' )
and charindex(''exec '',textdata) > 0
';
EXECUTE sp_executesql @sqlStr ; --3)找出没有参数的sql脚本,更新到 proc_sql 字段
--update temp_profiler_2015060917 set proc_sql = textdata where proc_sql is null and textdata is not null
SET @sqlStr = N'update '+@tblName+' set proc_sql = textdata where proc_sql is null and textdata is not null'
EXECUTE sp_executesql @sqlStr ; --12. 统计
--1)新建表,用于保存统计数据,trace_20130910每个proc_sql对应一行
SET @sqlStr = N'
create table ['+@tblName+'_stat]
(
id int identity(1,1) primary key,
databaseid int,
proc_sql nvarchar(max), -- 对应trace_20130910的proc_sql
total_duration bigint, -- 总耗时
max_duration int, -- 该语句最大耗时
min_duration int, -- 该语句最小耗时
rate_duration int -- 所耗时间百分比
)
';
EXECUTE sp_executesql @sqlStr ; --2)生成统计数据,存入1)步的表中 trace_20130910_stat]
SET @sqlStr = N'
;with cte
(
databaseid,
proc_sql,
total_duration,
max_duration ,
min_duration
) as
(select databaseid,
proc_sql,
sum(duration) as total_duration,
max(duration) as max_duration,
min(duration) as min_duration
from '+@tblName+'
where proc_sql is not null and proc_sql <> ''''
group by databaseid,proc_sql
)
, cte2 as
(-- 总耗时,用来计算百分比
select sum(total_duration) as total_duration from cte
)
insert into ['+@tblName+'_stat]
(
databaseid,
proc_sql,
total_duration,
max_duration ,
min_duration ,
rate_duration
)
select
databaseid,
proc_sql,
total_duration,
max_duration ,
min_duration ,
100 * total_duration / ( select total_duration from cte2 ) as rate_duration
from cte
order by rate_duration desc
';
EXECUTE sp_executesql @sqlStr ; -- 3)更新记录表[trace_20130910]的 proc_sql_id
SET @sqlStr = N'
update ['+@tblName+'] set proc_sql_id = b.id
from ['+@tblName+'] a inner join ['+@tblName+'_stat] b
on a.databaseid = b.databaseid and a.proc_sql = b.proc_sql
';
EXECUTE sp_executesql @sqlStr ;
end ;
GO

profiler跟踪事件存为表之后性能分析工具的更多相关文章

  1. CLR Profiler 性能分析工具

    CLR Profiler 性能分析工具 CLR Profiler 性能分析工具 CLR Profiler 有两个版本,分别用于CLR1.1 和 CLR2.0,至于CLR4试了一些也可以,但不知道是否完 ...

  2. Java 性能分析工具 , 第 1 部分: 操作系统工具

    引言 性能分析的前提是将应用程序内部的运行状况以及应用运行环境的状况以一种可视化的方式更加直接的展现出来,如何来达到这种可视化的展示呢?我们需要配合使用操作系统中集成的程序监控工具和 Java 中内置 ...

  3. 11个Visual Studio代码性能分析工具

    软件开发中的性能优化对程序员来说是一个非常重要的问题.一个小问题可能成为一个大的系统的瓶颈.但是对于程序员来说,通过自身去优化代码是十分困难的.幸运的是,有一些非常棒的工具可以帮助程序员进行代码分析和 ...

  4. 系统级性能分析工具 — Perf

    从2.6.31内核开始,linux内核自带了一个性能分析工具perf,能够进行函数级与指令级的热点查找. perf Performance analysis tools for Linux. Perf ...

  5. 系统级性能分析工具perf的介绍与使用[转]

    测试环境:Ubuntu16.04(在VMWare虚拟机使用perf top存在无法显示问题) Kernel:3.13.0-32 系统级性能优化通常包括两个阶段:性能剖析(performance pro ...

  6. .NET 11 个 Visual Studio 代码性能分析工具

    原文地址 软件开发中的性能优化对程序员来说是一个非常重要的问题.一个小问题可能成为一个大的系统的瓶颈.但是对于程序员来说,通过自身去优化代码是十分困难的.幸运的是,有一些非常棒的工具可以帮助程序员进行 ...

  7. Android 常用的性能分析工具详解:GPU呈现模式, TraceView, Systrace, HirearchyViewer(转)

    此篇将重点介绍几种常用的Android性能分析工具: 一.Logcat 日志 选取Tag=ActivityManager,可以粗略地知道界面Displaying的时间消耗.当我们打开一个Activit ...

  8. 11 个 Visual Studio 代码性能分析工具

    软件开发中的性能优化对程序员来说是一个非常重要的问题.一个小问题可能成为一个大的系统的瓶颈.但是对于程序员来说,通过自身去优化代码是十分困难的.幸运的是,有一些非常棒的工具可以帮助程序员进行代码分析和 ...

  9. 系统级性能分析工具 — Perf【转】

    转自:https://blog.csdn.net/zhangskd/article/details/37902159 版权声明:本文为博主原创文章,转载请注明出处. https://blog.csdn ...

随机推荐

  1. [AHOI2009]同类分布

    题目大意: 问在区间[l,r]内的正整数中,有多少数能被其个位数字之和整除. 思路: 数位DP. 极端情况下,每一位都是9,所以各位数字之和不超过9*18.(为了方便这里用了9*19) f[i][j] ...

  2. bzoj 2957 楼房重建 分块

    楼房重建 Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/problem.php?id=29 ...

  3. 普及向 ZKW线段树!

    啊,是否疲倦了现在的线段树 太弱,还递归! 那我们就欢乐的学习另外一种神奇的线段树吧!(雾 他叫做zkw线段树   这个数据结构灰常好写(虽然线段树本身也特别好写……) 速度快(貌似只在单点更新方面比 ...

  4. mybatis源码分析(3)-----SqlSessionHolder作用

    1. sqlSessionHolder 是位于mybatis-spring 包下面,他的作用是对于sqlSession和事务的控制 sqlSessionHolder 继承了spring的Resourc ...

  5. systemd-udevd

    描述:systemd-udevd是监听内核发出的设备事件,并根据udev规则处理每个事件. 选项: --daemon 脱离控制台,并作为后台守程运行. --debug 在标准错误上打印调试信息 --c ...

  6. Android开发利用Volley框架下载和缓存网络图片

    2013年Google I/O大会上推出了一种新的网络通信框架——Volley,Volley可是说是把AsyncHttpClient和Universal-Image-Loader的优点集于了一身,既可 ...

  7. Hibernate:有了 save,为什么还需要 persist?

    背景 万物皆自然,每个 API 的设计,无论是否正确,都有其意图.因此,在学习某些框架的时候,我们需要经常思考:这个 API 的设计意图是啥? 本文来探讨一下 Session 中 persist 的设 ...

  8. appium+python自动化60-windows上同时启动多个appium服务,让多个android机器并行运行

    前言 做android自动化的时候,启动一个appium服务,只能匹配一个手机去自动化执行.有时候想同一套代码,可以在不同的手机上执行,测下app在不同手机上兼容性. 这就需要启动多个appium服务 ...

  9. Interactive Messager

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. linux下的springboot项目启动文件

    启动springboot项目的脚本文件,启动时./startup.sh即可,会先关闭原进程,再启一个新进程. 创建startup.sh 写入内容 #!/bin/bash clear echo &quo ...