• SQL日常维护的语句


    640?wx_fmt=jpeg&wxfrom=5&wx_lazy=1


    1、查询当前正在执行的语句:

    SELECT  

    der.[session_id],der.[blocking_session_id],  

    sp.lastwaittype,sp.hostname,sp.program_name,sp.loginame,  

    der.[start_time] AS '开始时间',  

    der.[status] AS '状态',  

    dest.[text] AS 'sql语句',  

    DB_NAME(der.[database_id]) AS '数据库名',  

    der.[wait_type] AS '等待资源类型',  

    der.[wait_time] AS '等待时间',  

    der.[wait_resource] AS '等待的资源',  

    der.[logical_reads] AS '逻辑读次数'  

    FROM sys.[dm_exec_requests] AS der  

    INNER JOIN master.dbo.sysprocesses AS sp ON der.session_id=sp.spid  

    CROSS APPLY  sys.[dm_exec_sql_text](der.[sql_handle]) AS dest  

    --WHERE [session_id]>50 AND session_id<>@@SPID  

    ORDER BY der.[session_id]  

    GO

    2、是否堵塞:

    SELECT spid, blocked, waittime, waittype, waitresource

    , p.dbid, cpu, physical_io, memusage, open_tran AS status

    , login_time, last_batch, hostname, program_name, hostprocess

    , loginame, cmd, text

    FROM master.dbo.sysprocesses p

    CROSS APPLY sys.dm_exec_sql_text(p.sql_handle) s

    WHERE blocked > 0

    OR spid IN (

    SELECT blocked

    FROM master.dbo.sysprocesses

    WHERE blocked > 0

    )

    GO

    3、是否有未提交事物:

    SELECT spid, blocked, waittime, waittype, waitresource

    , p.dbid, cpu, physical_io, memusage, open_tran

    , status, login_time, last_batch, hostname, program_name

    , hostprocess, loginame, cmd, text

    FROM master.dbo.sysprocesses p

    CROSS APPLY sys.dm_exec_sql_text(p.sql_handle) s

    WHERE open_tran <> 0

    GO

    4、 死锁: 

    --drop table #deadlock


    CREATE TABLE #deadlock(LogDate DATETIME,ProcessInfo VARCHAR(20),Text VARCHAR(2000))  

    go  

    INSERT INTO #deadlock EXEC xp_readerrorlog 0,1,'deadlock victim',NULL,'2016-07-20 14:00:00','2016-07-30','DESC'    

    go



  • 相关阅读:
    机器人
    仙岛求药(一)
    YZM 二分查找
    珠心算测验升级版
    博客正在施工
    【其他】16年12月博客阅读索引^_^
    博客有新家了!
    POJ No.3617【B008】
    POJ No.2386【B007】
    【刷题记录】部分和问题
  • 原文地址:https://www.cnblogs.com/hgmyz/p/12352368.html
Copyright © 2020-2023  润新知