1.首先要有一个trace文件

2. 打开trace文件

3. 另存为跟踪表

4.登录你要保存到的目标sqlserver服务器

5. 选择要保存的数据库和表名称

6. 保存完成(左下角出现进度直到显示“已完成”)

7. 在数据库中找到该表(在第5步选择的数据库中找)

8.查看部分结果(TextData就是查询的sql语句,Duration就是查询的时间,这里duration除以1000才是毫秒)

9. 然后我们来分析TextData,如何找到相同的语句,不同的参数。我的分析,TextData主要有3种

1)带参数sql语句(以 exec sp_executesql N' 打头,以 ',N' 结尾可以找出对应的sql语句),如下图

2)存储过程(类似 exec porc_user_insert @username,  exec 和 @ 之间为存储过程名),如下图

3)不带参数的sql语句

10. 对trace表的数据进行处理前的一些准备:

1)update trace_20130910 set duration = duration / 1000 where duration is not null  -- 时间改为毫秒

2)修改 textdata 为 nvarchar(max)类型,因为textdata默认保存为ntext类型,处理不方便

alter table trace_20130910 alter column textdata nvarchar(max)

3)新增两个字段

alter table [trace_20130910] add proc_sql nvarchar(max) -- 保存该textdata调用的存储过程,原始sql等;

alter table [trace_20130910] add proc_sql_id int -- 为存储过程和原始sql指定一个编号

11. 处理trace数据 

1)找出执行的sql脚本(带参数) ,更新到 proc_sql 字段

update [trace_20130910]

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

2)找出执行的存储过程,更新到 proc_sql 字段

update [trace_20130910]

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

3)找出没有参数的sql脚本,更新到 proc_sql 字段

update [trace_20130910] set proc_sql = textdata where proc_sql is null and textdata is not null

12. 统计

1)新建表,用于保存统计数据,trace_20130910每个proc_sql对应一行

create table [trace_20130910_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 -- 所耗时间百分比

)

2)生成统计数据,存入1)步的表中 trace_20130910_stat]

;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 [trace_20130910]

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 [trace_20130910_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

3)更新记录表[trace_20130910]的 proc_sql_id

update [trace_20130910] set proc_sql_id = b.id

from [trace_20130910] a inner join [trace_20130910_stat] b

on a.databaseid = b.databaseid and a.proc_sql = b.proc_sql

13. 查询统计结果

1)查出最耗时的语句或过程

select * from [trace_20130910_stat] order by total_duration desc

2)查询某个过程或者sql语句详情

select  *  from  [trace_20130910]  where proc_sql_id = 1

这是根据duration排序,稍微改下就可以按reads排序,因为步骤毕竟多,而且经常会用到,所以整理成一个存储过程。方便以后分析性能问题。

找了半天发现不能上传附件。

五分钟打造自己的sql性能分析工具的更多相关文章

  1. Hi,腾讯WeTest联合Unity官方打造的性能分析工具UPA,今日全新发布!

    早在2016年ChinaJoy开始,WeTest曾受邀出席过Unity中国的线下性能场的活动,介绍我们的自动化框架和王者荣耀的故事.当时的活动很成功,期间我们收到了不少Unity开发者的好评,也为我们 ...

  2. SQL性能分析之执行计划

    一直想找一些关于SQL语句性能调试的权威参考,但是有参考未必就能够做好调试的工作.我深信实践中得到的经验是最珍贵的,书本知识只是一个引导.本篇来源于<Inside Microsoft SQL S ...

  3. Mysql高级操作学习笔记:索引结构、树的区别、索引优缺点、创建索引原则(我们对哪种数据创建索引)、索引分类、Sql性能分析、索引使用、索引失效、索引设计原则

    Mysql高级操作 索引概述: 索引是高效获取数据的数据结构 索引结构: B+Tree() Hash(不支持范围查询,精准匹配效率极高) 树的区别: 二叉树:可能产生不平衡,顺序数据可能会出现链表结构 ...

  4. Linux 性能分析工具汇总合集

    出于对Linux操作系统的兴趣,以及对底层知识的强烈欲望,因此整理了这篇文章.本文也可以作为检验基础知识的指标,另外文章涵盖了一个系统的方方面面.如果没有完善的计算机系统知识,网络知识和操作系统知识, ...

  5. [转]Linux性能分析工具汇总合集

    出于对Linux操作系统的兴趣,以及对底层知识的强烈欲望,因此整理了这篇文章.本文也可以作为检验基础知识的指标,另外文章涵盖了一个系统的方方面面.如果没有完善的计算机系统知识,网络知识和操作系统知识, ...

  6. 超全整理!Linux性能分析工具汇总合集

    转自:http://rdc.hundsun.com/portal/article/731.html?ref=myread 出于对Linux操作系统的兴趣,以及对底层知识的强烈欲望,因此整理了这篇文章. ...

  7. (转)超全整理!Linux性能分析工具汇总合集

    超全整理!Linux性能分析工具汇总合集 原文:http://rdc.hundsun.com/portal/article/731.html 出于对Linux操作系统的兴趣,以及对底层知识的强烈欲望, ...

  8. linux下常见的性能分析工具

    转载于:http://bian5399.blog.51cto.com/3848702/834715 性能调优的主要目的是使系统能够有效的利用各种资源,最大的发挥应用程序和系统之间的性能融合,使应用高效 ...

  9. Linux 性能分析 工具命令

    背景知识:具备背景知识是分析性能问题时需要了解的.比如硬件 cache:再比如操作系统内核.应用程序的行为细节往往是和这些东西互相牵扯的,这些底层的东西会以意想不到的方式影响应用程序的性能,比如某些程 ...

随机推荐

  1. [转]Android 延迟执行

    开启新线程 new Thread(new Runnable(){ public void run(){ Thread.sleep(XXXX); handler.sendMessage(); //告诉主 ...

  2. linux spi 设备节点 读写

    本文记录spi设备节点的操作方法. SPI总线设备文件名通常为/dev/spidevN.P(N=0.1.2--,P=0.1.2--), 其中N表示第几路SPI总线,而P表示在该路SPI总线中使用哪个C ...

  3. TermServDevices报错导致服务器死机(远程服务使用者必读)

    事件类型: 错误 事件来源: TermServDevices 事件 ID: 1111 描述:打印机 !!192.168.99.6!HP LaserJet 3050 Series PCL 5e 所需的驱 ...

  4. LeetCode Lowest Common Ancestor of a Binary Tree

    原题链接在这里:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ 题目: Given a binary tr ...

  5. SQL截取字符串函数

    A.截取从字符串左边开始N个字符 以下是代码片段:    Declare @S1 varchar(100)  Select @S1='http://www.xrss.cn'  Select Left( ...

  6. Oracle数据库之PL/SQL基础

    介绍PL/SQL之前,先介绍一个图像化工具:Oracle SQL Developer 在oracle的开发过程中, 我们难免会使用第三方开发的软件来辅助我们书写SQL, pl/sql是一个不错的sql ...

  7. RouterOS软路由设置固定IP+PPPOE

    内网: IP:192.168.10.254/24 网关:192.168.10.254 外网: IP:218.17.172.17/28 子网掩码:255.255.255.240 网关:218.17.17 ...

  8. RAC GI安装,报"Task resolv.conf Integerity"验证失败

    安装12.1.0.2 rac测试环境的时候,报"Task resolv.conf Integerity"验证失败 解决方案: 因为测试环境,没有使用DNS,删除resolv.con ...

  9. Hdu-3487 Splay树,删除,添加,Lazy延迟标记操作

    HDU_3487 题意:给出n和q,n代表1-n的序列,接下来q有两种操作,Cut a b c:表示把区间[a,b]截掉然后放在第c个数的后面,Flip a b 表示把区间[a,b]反转,经过一系列的 ...

  10. 常见http代码错误原因及处理

    常见的HTTP错误可以分为以下四大类.每一大类又细分为很多类小错误.当您打不开网站或者打开网站报错时首先检查您输入的网站是否有误,检查网络是否有问题或者虚拟主机的DNS是否可以解析.确定没有问题时再看 ...