什么会导致单个表超时

Pri*_*ERO 4 sql sql-server timeout

我只对在ReportingPeriod表上运行的查询收到以下错误(见下文).即使我在另一个表上注释掉相关的LEFT JOIN,也会发生这种情况.表中的记录不超过200条.此外,如果我直接运行查询或使用存储过程运行它也无关紧要.最后,其他表和程序运行正常.

知道是什么原因引起的吗?

版本
Sql-Server 2005

错误:
超时已过期.操作完成之前经过的超时时间或服务器没有响应.

RAW QUERY:

DECLARE @ProjectsKey INT
SET @ProjectsKey = 1234
-----------------------------
    SELECT 
        ReportingPeriodKey
        ,ReportingPeriod.ProjectsKey
        --,Phase.PhaseKey AS PhaseKey
        --,Phase.Name AS PhaseName
        ,[Type]
        ,ReportingPeriodStart
        ,ReportingPeriodEnd
    FROM   
        ReportingPeriod
--  LEFT JOIN
--  (
--      SELECT
--          PhaseKey
--          ,ProjectsKey
--          ,Name
--      FROM dbo.Phase
--  ) AS Phase ON Phase.PhaseKey = dbo.ReportingPeriod.PhaseKey
    WHERE
        ((@ProjectsKey IS NOT NULL AND ReportingPeriod.ProjectsKey = @ProjectsKey) OR @ProjectsKey IS NULL)
    ORDER BY
        ReportingPeriodStart
Run Code Online (Sandbox Code Playgroud)

表定义:

SET ANSI_NULLS ON GO SET
QUOTED_IDENTIFIER ON 
GO 
CREATE TABLE [dbo].[ReportingPeriod](
    [ReportingPeriodKey] [int] IDENTITY(1,1) NOT NULL,  
    [ProjectsKey] [int] NOT NULL,   
    [PhaseKey] [int] NOT NULL, [Type] [nvarchar](250) NOT NULL,     
    [ReportingPeriodStart] [datetime] NOT NULL CONSTRAINT [DF_ReportPeriod_Start]  DEFAULT (getdate()),     
    [ReportingPeriodEnd] [datetime] NOT NULL CONSTRAINT [DF_ReportPeriod_End]  DEFAULT(getdate()),  
    [CreatedBy] [nvarchar](100) NOT NULL,
    [CreatedDate] [datetime] NOT NULL CONSTRAINT [DF_ReportingPeriod_CreatedDate] DEFAULT (getdate()),  
    [ModifiedBy] [nvarchar](100) NULL,  
    [ModifiedDate] [datetime] NULL,  

    CONSTRAINT [PK_ReportPeriod] PRIMARY KEY CLUSTERED  ( [ReportingPeriodKey] ASC )WITH (PAD_INDEX  = OFF,
    STATISTICS_NORECOMPUTE  = OFF,
    IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, 
    ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY] ) ON [PRIMARY]

GO 
ALTER TABLE [dbo].[ReportingPeriod] WITH CHECK 
ADD  CONSTRAINT [FK_ReportingPeriod_attrReportingPeriodType]
FOREIGN KEY([Type]) REFERENCES [dbo].[attrReportingPeriodType] ([FullName]) 

GO 
ALTER TABLE [dbo].[ReportingPeriod]  
CHECK CONSTRAINT [FK_ReportingPeriod_attrReportingPeriodType]

GO 
ALTER TABLE [dbo].[ReportingPeriod] WITH CHECK 
ADD  CONSTRAINT [FK_ReportingPeriod_Phase] 
FOREIGN KEY([PhaseKey]) 
REFERENCES [dbo].[Phase] ([PhaseKey]) 

GO 
ALTER TABLE [dbo].[ReportingPeriod] 
CHECK CONSTRAINT [FK_ReportingPeriod_Phase]

GO 
ALTER TABLE [dbo].[ReportingPeriod] WITH CHECK 
ADD  CONSTRAINT [FK_ReportingPeriod_Projects] 
FOREIGN KEY([ProjectsKey]) 
REFERENCES [dbo].[Projects] ([ProjectsKey]) 

GO
ALTER TABLE [dbo].[ReportingPeriod]
CHECK CONSTRAINT
[FK_ReportingPeriod_Projects]
Run Code Online (Sandbox Code Playgroud)

SYSTEM STATS
object_id#875866187有2套

reserved_pa​​ge_count = 17 used_pa​​ge_count = 11 row_count = 306

reserved_pa​​ge_count = 2 used_pa​​ge_count = 2 row_count = 306

Mat*_*lie 10

您是否尝试检查可能锁定表的打开事务?

dbcc opentran

exec sp_who 69
-- Where the id is the SPID from DBCC OPENTRAN

exec sp_lock 69
-- Where the id is the SPID from DBCC OPENTRAN

select * from sys.objects where object_id = 2089058478
-- Where the id is the ObjID from sp_lock
Run Code Online (Sandbox Code Playgroud)

  • 我很喜欢用于识别阻塞 SPID 的“sp_who2”。`BlkBy` 字段告诉您阻塞的 SPID,然后您可以使用 `DBCC INPUTBUFFER(<blocking_spid>)` 来确定导致阻塞发生的查询。 (2认同)