Table 使用 PrimaryKey 值在 Delete 上取出表锁。可能是索引损坏,我该如何解决?

问题描述 投票:0回答:0

使用 SQL Server 2017,我有一个多年来一直无故障运行的表。

我开始随机出现数据库锁定,这似乎是由于某个特定表上的表锁被取消所致。

我使用了一个锁报告,它在这个 SQL 上识别了 7 个锁:

DELETE FROM WasteSchedule WHERE WasteScheduleId=@P

删除是使用参数化 SQL 语句从 PHP8 发起的。

WasteScheduleId
是主键。

我认为除非 5000 行受到影响,否则不应进行表级锁定。在某些情况下,这可能会更低,但在这种情况下,我在整个表中只有 9k 行,并且我使用单个主键值来删除单行。

我检查了指标;一个用于主键,另一个用于外键。

我尝试

Rebuild
主键索引并得到以下错误:

===================================

One or more errors occurred. For details click hyperlink associated with error message. 
(SqlManagerUI)

------------------------------
Program Location:

   at Microsoft.SqlServer.Management.SqlManagerUI.IndexRebuild.OnRunNow(Object sender)
   at Microsoft.SqlServer.Management.SqlMgmt.PanelExecutionHandler.Run(RunType runType, Object sender)
   at Microsoft.SqlServer.Management.SqlMgmt.SqlMgmtTreeViewControl.DoPreProcessExecutionAndRunViews(RunType runType)
   at Microsoft.SqlServer.Management.SqlMgmt.SqlMgmtTreeViewControl.ExecuteForSql(PreProcessExecutionInfo executionInfo, ExecutionMode& executionResult)
   at Microsoft.SqlServer.Management.SqlMgmt.SqlMgmtTreeViewControl.Microsoft.SqlServer.Management.SqlMgmt.IExecutionAwareSqlControlCollection.PreProcessExecution(PreProcessExecutionInfo executionInfo, ExecutionMode& executionResult)
   at Microsoft.SqlServer.Management.SqlMgmt.ViewSwitcherControlsManager.RunNow(RunType runType, Object sender)

我尝试

rebuild
外键索引并得到同样的错误。

然后我尝试删除外键索引,又报错:

TITLE: Microsoft SQL Server Management Studio
------------------------------

Drop failed for Index 'WasteOrderHeader_FK'.  (Microsoft.SqlServer.Smo)

For help, click: https://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=16.100.47021.0&EvtSrc=Microsoft.SqlServer.Management.Smo.ExceptionTemplates.FailedOperationExceptionText&EvtID=Drop+Index&LinkId=20476

------------------------------
ADDITIONAL INFORMATION:

An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo)

------------------------------

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

For help, click: https://docs.microsoft.com/sql/relational-databases/errors-events/mssqlserver-1222-database-engine-error

------------------------------
BUTTONS:

OK
------------------------------

看起来我有损坏的索引,但在这个阶段我确定解决这个问题的最佳行动顺序是什么。

编辑:这成为一个问题的原因是删除锁只是坐在那里而不是释放;导致使用它的应用程序出现各种问题。

sql-server
© www.soinside.com 2019 - 2024. All rights reserved.