• 【监控笔记】【1.1】监控事件系列——SQL Server Profiler


    声明:本系列是书目《sql server监控与诊断读书笔记》 

    sql server Profile:https://docs.microsoft.com/en-us/sql/tools/sql-server-profiler/sql-server-profiler-templates?view=sql-server-ver15

    联机丛书监控:https://docs.microsoft.com/en-us/sql/relational-databases/performance/monitor-and-tune-for-performance?view=sql-server-2017

    联机丛书监控组件与监控工具:https://docs.microsoft.com/en-us/sql/relational-databases/performance/performance-monitoring-and-tuning-tools?view=sql-server-2017

    【1】SQL Server事件探查器(SQL Server Profiler)

    【1.1】打开SQL Server Profiler

      

      

    【1.2】 新建跟踪模板

      

    【1.3】事件选择,列筛选

      

    【1.4】运行、停止、查找

      这些列可以在【1.3】中点击右下角的列筛选器和组织列来做选择

      

    【1.5】导出服务端跟踪脚本,以及如何使用

       

      文件内容

      

    /****************************************************/
    /* Created by: SQL Server 2012  Profiler          */
    /* Date: 2019/05/17  12:02:18         */
    /****************************************************/
    
    
    -- Create a Queue
    declare @rc int
    declare @TraceID int
    declare @maxfilesize bigint
    set @maxfilesize = 5 
    
    -- Please replace the text InsertFileNameHere, with an appropriate
    -- filename prefixed by a path, e.g., c:MyFolderMyTrace. The .trc extension
    -- will be appended to the filename automatically. If you are writing from
    -- remote server to local drive, please use UNC path and make sure server has
    -- write access to your network share
    
    exec @rc = sp_trace_create @TraceID output, 0, N'InsertFileNameHere', @maxfilesize, NULL 
    if (@rc != 0) goto error
    
    -- Client side File and Table cannot be scripted
    
    -- Set the events
    declare @on bit
    set @on = 1
    exec sp_trace_setevent @TraceID, 14, 1, @on
    exec sp_trace_setevent @TraceID, 14, 9, @on
    exec sp_trace_setevent @TraceID, 14, 10, @on
    exec sp_trace_setevent @TraceID, 14, 11, @on
    exec sp_trace_setevent @TraceID, 14, 6, @on
    exec sp_trace_setevent @TraceID, 14, 12, @on
    exec sp_trace_setevent @TraceID, 14, 14, @on
    exec sp_trace_setevent @TraceID, 15, 11, @on
    exec sp_trace_setevent @TraceID, 15, 6, @on
    exec sp_trace_setevent @TraceID, 15, 9, @on
    exec sp_trace_setevent @TraceID, 15, 10, @on
    exec sp_trace_setevent @TraceID, 15, 12, @on
    exec sp_trace_setevent @TraceID, 15, 13, @on
    exec sp_trace_setevent @TraceID, 15, 14, @on
    exec sp_trace_setevent @TraceID, 15, 15, @on
    exec sp_trace_setevent @TraceID, 15, 16, @on
    exec sp_trace_setevent @TraceID, 15, 17, @on
    exec sp_trace_setevent @TraceID, 15, 18, @on
    exec sp_trace_setevent @TraceID, 17, 1, @on
    exec sp_trace_setevent @TraceID, 17, 9, @on
    exec sp_trace_setevent @TraceID, 17, 10, @on
    exec sp_trace_setevent @TraceID, 17, 11, @on
    exec sp_trace_setevent @TraceID, 17, 6, @on
    exec sp_trace_setevent @TraceID, 17, 12, @on
    exec sp_trace_setevent @TraceID, 17, 14, @on
    exec sp_trace_setevent @TraceID, 10, 9, @on
    exec sp_trace_setevent @TraceID, 10, 2, @on
    exec sp_trace_setevent @TraceID, 10, 10, @on
    exec sp_trace_setevent @TraceID, 10, 6, @on
    exec sp_trace_setevent @TraceID, 10, 11, @on
    exec sp_trace_setevent @TraceID, 10, 12, @on
    exec sp_trace_setevent @TraceID, 10, 13, @on
    exec sp_trace_setevent @TraceID, 10, 14, @on
    exec sp_trace_setevent @TraceID, 10, 15, @on
    exec sp_trace_setevent @TraceID, 10, 16, @on
    exec sp_trace_setevent @TraceID, 10, 17, @on
    exec sp_trace_setevent @TraceID, 10, 18, @on
    exec sp_trace_setevent @TraceID, 12, 1, @on
    exec sp_trace_setevent @TraceID, 12, 9, @on
    exec sp_trace_setevent @TraceID, 12, 11, @on
    exec sp_trace_setevent @TraceID, 12, 6, @on
    exec sp_trace_setevent @TraceID, 12, 10, @on
    exec sp_trace_setevent @TraceID, 12, 12, @on
    exec sp_trace_setevent @TraceID, 12, 13, @on
    exec sp_trace_setevent @TraceID, 12, 14, @on
    exec sp_trace_setevent @TraceID, 12, 15, @on
    exec sp_trace_setevent @TraceID, 12, 16, @on
    exec sp_trace_setevent @TraceID, 12, 17, @on
    exec sp_trace_setevent @TraceID, 12, 18, @on
    exec sp_trace_setevent @TraceID, 13, 1, @on
    exec sp_trace_setevent @TraceID, 13, 9, @on
    exec sp_trace_setevent @TraceID, 13, 11, @on
    exec sp_trace_setevent @TraceID, 13, 6, @on
    exec sp_trace_setevent @TraceID, 13, 10, @on
    exec sp_trace_setevent @TraceID, 13, 12, @on
    exec sp_trace_setevent @TraceID, 13, 14, @on
    
    
    -- Set the Filters
    declare @intfilter int
    declare @bigintfilter bigint
    
    exec sp_trace_setfilter @TraceID, 10, 0, 7, N'SQL Server Profiler - 5443439d-5d24-4615-9120-f20f801bfaf2'
    -- Set the trace status to start
    exec sp_trace_setstatus @TraceID, 1
    
    -- display trace id for future references
    select TraceID=@TraceID
    goto finish
    
    error: 
    select ErrorCode=@rc
    
    finish: 
    go
    默认模板导出SQL Trace代码

    导出来的文件可以通过这种方式查阅

    select * from fn_trace_gettable('d:dba_toolsslow_query1.trc',default)

     【1.6】阀值跟踪过滤、文本关键字跟踪过滤

      

      

    【1.7】跟踪重放、重播

      打开跟踪文件,可以进行重播操作

      

    【2】服务器端跟踪(Server Side Trace)

    【2.1】概念与使用

    SQL跟踪任务:https://docs.microsoft.com/en-us/sql/relational-databases/sql-trace/sql-trace?view=sql-server-2017

    1. 使用sp_trace_create创建跟踪

    2. 使用sp_trace_setevent添加事件

    3. (可选)使用sp_trace_setfilter设置过滤器

    4. 使用sp_trace_setstatus启动跟踪

    5. 使用sp_trace_setstatus停止跟踪

    6. 使用sp_trace_setstatus关闭跟踪

    sp_trace_create(Transact-SQL)
    sp_trace_setevent(Transact-SQL)
    sp_trace_setfilter(Transact-SQL)
    sp_trace_setstatus(Transact-SQL)

     【2.2】服务端跟踪死锁案例

    --【2.2.1】开启设置跟踪
    use
    db_tank go --创建跟踪文件返回值 declare @rc int --创建一个跟踪句柄 declare @TraceID int --创建跟踪文件路径 declare @TraceFilePath nvarchar(500) set @TraceFilePath=N'D:DBA_TOOLSdb_deadLock_log' --跟踪文件的大小 单位M declare @maxfilesize bigint set @maxfilesize=200 --设置停止的时间 declare @EndTime datetime set @EndTime=null --设置系统默认的操作 declare @options int set @options=2 --设置默认滚动文件的数目 declare @filecount int set @filecount=5 exec @rc=sp_trace_Create @TraceID output, @options, @TraceFilePath, @maxfilesize, @EndTime, @filecount if(@rc=0) declare @on bit set @on = 1 --下述语句中的148指的是locks:deadlock graph事件(参见sys.trace_events),12指的是spid列(参见sys.trace_columns) exec sp_trace_setevent @TraceID, 148, 12, @on exec sp_trace_setevent @TraceID, 148, 11, @on exec sp_trace_setevent @TraceID, 148, 4, @on exec sp_trace_setevent @TraceID, 148, 14, @on exec sp_trace_setevent @TraceID, 148, 26, @on exec sp_trace_setevent @TraceID, 148, 64, @on exec sp_trace_setevent @TraceID, 148, 1, @on -- 启动跟踪 exec sp_trace_setstatus @TraceID, 1 -- 记录下跟踪ID,以备后面使用 select TraceID = @TraceID goto finish error: select ErrorCode=@rc finish: go --【2.2.2】启停与重启跟踪 -----默认重启实例后,跟踪会取消,所以写一个SP做实例启动执行 use master go create proc StartBlackBoxTrace as begin 【2.2.1】中代码 END GO --将该存储过程设置为SQL Server服务启动时自动启动 EXEC sp_procoption 'StartBlackBoxTrace','STARTUP','ON' print 'ok' GO --查看跟踪 select * from sys.traces --删除,exec sp_trace_setstatus @TraceID, 1 --0为停止跟踪,1为启用跟踪,2为删除跟踪,
    --下面的2,为sys.traces中查出来的TraceId
    exec sp_trace_setstatus 2, 0
    exec sp_trace_setstatus 2, 2

    --用T-SQL以表的方式查看跟踪文件
    select * from fn_trace_gettable('D:DBA_TOOLSdb_deadLock_log',default)
     
  • 相关阅读:
    简历的快速复制
    使用stringstream对象简化类型转换
    猴子吃桃
    new和delete运算符
    绘制正余弦曲线
    计算学生的平均成绩
    判断是否为回文字符串
    统计各种字符个数
    验证用户名
    回溯法(挑战编程)
  • 原文地址:https://www.cnblogs.com/gered/p/10879938.html
Copyright © 2020-2023  润新知