zoukankan      html  css  js  c++  java
  • sql server性能分析检测数据库阻塞语句

    create PROCEDURE [dbo].[auto_checkblocks] AS
    set nocount on
    if exists ( select * from master..sysprocesses where blocked <> 0 )

    begin
        
    /* show top blockers, but no duplicates */
        
    select '请尝试使用KILL [SPID] 来杀进程'
      
    --      select '请尝试使用SP_LOCK [SPID]来显示锁信息,用OBJECT_NAME(ID)来显示锁对象名称或用sp_who [SPID] 来显示信息'
       --     select '在使用OBJECT_NAME显示对象名称时请注意对应的db_id'  
           select '以下是引起阻塞的语句' 
        
    select distinct
        
    '进程ID'              = str( a.spid, 4 ),
        
    '进程ID状态'          = convertchar(10), a.status ),
        
    '分块进程的进程ID'    = str( a.blocked, 2 ),
        
    '工作站名称'          = convertchar(10), a.hostname ),
        
    '执行命令的用户'      = convertchar(10), suser_name( a.uid ) ),
        
    '数据库名'            = convertchar(10), db_name(a.dbid ) ),       
        
    '应用程序名'          = convertchar(10), a.program_name ),
        
    '正在执行的命令'      = convertchar(16), a.cmd ),
        
    '累计CPU时间'         = str( a.cpu, 7 ),
        
    'IO'                  = str( a.physical_io, 7 ),
            
    '登录名'              = a.loginame,
        
    '执行语句'=b.text
        
    from master..sysprocesses a
        
    cross apply sys.dm_exec_sql_text(a.sql_handle) b
        
    where spid in ( select blocked from master..sysprocesses )
        
    and blocked = 0
        
    order by str(spid,4)

        
    /* 显示阻塞牺牲品 */
        
    select '以下是被阻塞的等待执行的语句' 
            
    select 
        
    '进程ID[SPID]'        = str( a.spid, 4 ),
        
    '进程ID状态'          = convertchar(10), a.status ),
        
    '分块进程的进程ID'    = str( a.blocked, 2 ),
        
    '工作站名称'          = convertchar(10), a.hostname ),
        
    '执行命令的用户'      = convertchar(10), suser_name( a.uid ) ),
        
    '数据库名'            = convertchar(10), db_name( a.dbid ) ),       
        
    '应用程序名'          = convertchar(10), a.program_name ),
        
    '正在执行的命令'      = convertchar(16), a.cmd ),
        
    '累计CPU时间'         = str( a.cpu, 7 ),
        
    'IO'                  = str( a.physical_io, 7 ),
            
    '登录名'              = a.loginame,
        
    '执行语句'=b.text
        
    from master..sysprocesses a
        
    cross apply sys.dm_exec_sql_text(a.sql_handle) b
        
    where blocked <> 0
        
    order by spid
    end

    else
    begin
        
    select '恭喜!当前没有阻塞,当前的进程信息如下.'convert (char(24),getdate(),13)
            
    select 
        
    '进程ID'              = str( spid, 4 ),
        
    '进程ID状态'          = convertchar(10), status ),
        
    '分块进程的进程ID'    = str( blocked, 2 ),
        
    '工作站名称'          = convertchar(10), hostname ),
        
    '执行命令的用户'      = convertchar(10), suser_name( uid ) ),
        
    '数据库名'            = convertchar(10), db_name( dbid ) ),       
        
    '应用程序名'          = convertchar(10), program_name ),
        
    '正在执行的命令'      = convertchar(16), cmd ),
        
    '累计CPU时间'         = str( cpu, 7 ),
        
    'IO'                  = str( physical_io, 7 ),
            
    '登录名'              = loginame
        
    from master..sysprocesses
        
    where blocked = 0
        
    order by spid
    end

    return
  • 相关阅读:
    python3 使用opencv 画基本图形
    python 打印 九九表
    Python Date 1–Hello world print
    Linux下 Nginx+vsftpd搭建FTP服务器详细步骤
    Linux 开启端口命令
    No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK Maven异常解决方案
    React Fullpage
    Mint-UI组件 MessageBox为prompt 添加判断条件
    简易搭建本地静态服务器
    Mint-UI Picker 三级联动
  • 原文地址:https://www.cnblogs.com/luluping/p/1530553.html
Copyright © 2011-2022 走看看