什么会导致单个表超时

时间:2011-07-05 12:22:54

标签: 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

表格定义:

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]

系统状况
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

4 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

检查查询是否被阻止,请注意SQL语句或proc的sessionID(在查询窗口的底部看到),然后在查询下面触发,检查您的sessionID的BlkBy列是否为空
  
EXEC SP_WHO2 
  

还可以在一个窗口中执行查询,然后在另一个窗口中执行以下查询以查看是否正在运行任何其他正在锁定表的进程

   
 select text, session_id,
start_time, status, db_name(database_id) as DBName, blocking_session_id,
wait_type, wait_resource
from sys.dm_Exec_requests der
cross apply
sys.dm_exec_sql_text (der.sql_handle) 

还测试并更改事务隔离级别,允许脏读作为最终选项,如下所示:

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

现在,您还可以使用此DMV检查当前正在运行的任何事务,并查找相关表上任何Xclusive锁的request_mode列。

SELECT * FROM SYS.DM_TRAN_LOCKS

答案 2 :(得分:0)

某些内容很可能会锁定您选择中的某个表格。您可以在执行proc时运行以下查询来测试是否阻止了proc。

select spid, blocked, login_time, nt_username, hostname, program_name 
from sys.sysprocesses

希望这有帮助。

答案 3 :(得分:0)

除非您担心来自表tReportingPeriod的脏读,否则您可以使用:

FROM ReportingPeriod WITH (NOLOCK)
在您的查询中