Lock request time out period exceeded. (Microsoft SQL Server, Error: 1222)

问题: 正常情况下,执行Rebuild Index都会很快完成,可今天遇到Rebuild Index的Job一直Running。手动执行Rebuild,也一样失败,报“Lock request time out period exceeded. (Microsoft SQL Server, Error: 1222)”,如下图:

Lock request time out period exceeded. (Microsoft SQL Server, Error: 1222)_第1张图片

解决:

查看当前Running的SPID及SQL Text,特别是长时间运行的,找到与执行Rebuild Index的表相关的SPID,然后KILL掉。我当前遇到的是一个跑了数小时的SQL,KILL掉SPID后,就能正常Rebuild Index。

SELECT r.session_id, r.status, r.start_time, r.command, s.text, r.wait_time, r.cpu_time, 
r.total_elapsed_time, r.reads, r.writes, r.logical_reads, r.transaction_isolation_level 
FROM sys.dm_exec_requests r 
CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) s
WHERE R.STATUS='running'

另外,你可以查看Lock相关的信息:

select distinct object_name(a.rsc_objid), a.req_spid, b.loginame 
from master.dbo.syslockinfo a (nolock) join 
master.dbo.sysprocesses b (nolock) on a.req_spid=b.spid 
where object_name(a.rsc_objid) is not null

查看指定SPID

USE master;  
GO  
EXEC sp_who '267' --指定process_id;  
GO  

查询更详细的信息:

DECLARE @SessionID INT=63
SELECT
     SPID                = er.session_id 
     ,Status             = ses.status 
     ,[Login]            = ses.login_name 
     ,Host               = ses.host_name 
     ,BlkBy              = er.blocking_session_id 
     ,DBName             =DB_Name(er.database_id) 
     ,CommandType        = er.command 
     ,SQLStatement       = st.text 
     ,ObjectName         =OBJECT_NAME(st.objectid) 
     ,ElapsedMS          = er.total_elapsed_time 
     ,CPUTime            = er.cpu_time 
     ,IOReads            = er.logical_reads + er.reads 
     ,IOWrites           = er.writes 
     ,LastWaitType       = er.last_wait_type 
     ,StartTime          = er.start_time 
     ,Protocol           = con.net_transport 
     ,ConnectionWrites   = con.num_writes 
     ,ConnectionReads    = con.num_reads 
     ,ClientAddress      = con.client_net_address 
     ,Authentication     = con.auth_scheme 
 FROM sys.dm_exec_requests er 
 OUTER APPLY sys.dm_exec_sql_text(er.sql_handle) st 
 LEFT JOIN sys.dm_exec_sessions ses 
 ON ses.session_id = er.session_id 
 LEFT JOIN sys.dm_exec_connections con 
 ON con.session_id = ses.session_id 
 WHERE er.session_id > 50 
     AND @SessionID IS NULL OR er.session_id = @SessionID 
 ORDER BY
     er.blocking_session_id DESC
     ,er.session_id 



你可能感兴趣的:(SQL,SERVER)