什么会导致单表超时
仅当在 ReportingPeriod 表上运行的查询时,我才会收到以下错误(见下文)。即使我在另一个表上注释掉相关的 LEFT JOIN ,也会发生这种情况。表中的记录不超过 200 条。此外,我是否直接运行查询或使用存储过程运行它并不重要。最后,其他表格和程序运行良好。
知道什么可能导致这种情况吗?
版本
Sql-Server 2005
错误:
超时已过。操作完成之前超时时间已过,或者服务器未响应。
原始查询:
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 有 2 组 #875866187served_pa
ge_count = 17 使用页数 = 11 行计数 = 306
保留页计数 = 2 使用页数 = 2 行数 = 306
I am getting the following error ONLY for queries run on the ReportingPeriod table (see below). This happens even when I comment-out the related LEFT JOIN on another table. There aren't more than 200 records in the table. Additionally, it doesn't matter if I run the query outright or run it using the stored procedure. Lastly, other tables & procedures run fine.
Any idea what may cause this?
VERSION
Sql-Server 2005
ERROR:
Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
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
TABLE DEFINITION:
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]
SYSTEM STATS
There are 2 sets for object_id #875866187
reserved_page_count = 17
used_page_count = 11
row_count = 306
reserved_page_count = 2
used_page_count = 2
row_count = 306
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您是否尝试过检查可能锁定表的开放事务?
Have you tried checking for open transactions that may be locking the table?
同时在一个窗口中执行查询并然后在另一个窗口中执行下面的查询,以查看是否有任何其他进程正在运行,并且该进程在表上有锁。
同时测试并更改事务隔离级别,允许脏读作为最终选项,如下所示:
现在您还可以检查任何当前正在运行的进程使用此 DMV 进行交易并查找 request_mode相关表上任何独占锁的列。
Also execute your query in one window and then in another window execute below query to see if any other processes are running that are having a lock on the table
Also test and change the transaction isolation level allowing dirty reads as a final option as below:
Now you can also check for any currently running transactions using this DMV and look for request_mode column for any Xclusive locks on the table concerned.
某些东西很可能锁定了您选择的表之一。您可以在 proc 执行时运行以下查询来测试您的 proc 是否被阻止
希望这会有所帮助。
Something most likely has a lock on one of the tables in your select. You can test whether your proc is being blocked by running the below query while you proc is executing
Hope this helps.
除非您担心表 tReportingPeriod 中的脏读,否则您可以
在查询中使用:。
Unless you are worried about dirty reads from the table tReportingPeriod you could just use:
in your query.