场景
数据库因非法断电导致出现分配页上不一致,运行完DBCC CHECKDB后出现以下错误:
表错误: 表 't_pc_id' (ID 277576027)。数据行在索引 'last_login_date' (ID 7)中没有匹配的索引行。与以下数据行匹配的索引行的键可能丢失或
关闭运行DBCC CHECKDB所在的SSSM时出现错误
重新打开SSMS ,查看索引last_login_date使用情况发现该索引没有被任何查询使用到,于是准备先禁用索引再后续删除
在一登陆界面上使用SSMS UI来禁用索引,结果发现UI无法操作(当时CPU使用率较高)
切换到另一登陆界面,在SSMS上运行以下语句:
ALTER INDEX [last_login_date] ON [dbo].[t_pc_id] DISABLE
语句运行半分钟没有结束
运行以下语句查看阻塞
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
发现阻塞回话为113,命令类型为DBCC,阻塞语句为:
(@_msparam_0 nvarchar(4000),@_msparam_1 nvarchar(4000),@_msparam_2 nvarchar(4000),@_msparam_3 nvarchar(4000)
,@_msparam_4 nvarchar(4000))
declare @database_id int select @database_id = db_id()
SELECT fi.avg_fragmentation_in_percent AS [AverageFragmentation]
FROM sys.tables AS tbl INNER JOIN sys.indexes AS i ON (i.index_id > @_msparam_0
and i.is_hypothetical = @_msparam_1)
AND (i.object_id=tbl.object_id)
INNER JOIN sys.dm_db_index_physical_stats(@database_id, NULL, NULL, NULL, 'LIMITED')
AS fi ON fi.object_id=CAST(i.object_id AS int) AND fi.index_id=CAST(i.index_id AS int)
WHERE (i.name=@_msparam_2)
and((tbl.name=@_msparam_3 and SCHEMA_NAME(tbl.schema_id)=@_msparam_4))
检查所有本地SSMS开打窗口,没有发现回话113
KILL回话113 后禁用索引语句立即运行完成
导致原因猜想:
1. 由禁用索引的UI导致(可能性较低)
2. 由之前运行结束的DBCC CHECKDB(有一定可能性)