SQL Server:过程,视图和函数的临时超时

时间:2018-01-10 08:39:05

标签: sql-server function stored-procedures view timeout

在我们的数据库中,我们有不同的函数,过程和视图,在我们的应用程序中抛出超时异常。即使在SSMS中,脚本也很慢。问题只发生在早上7点到9点之间,特别是在星期一早上。其中一些脚本非常短,下午时间不到一秒。

任何其他会话都不会阻止运行缓慢的会话。在此期间有一些预定的代理作业正在运行,但这些作业也在下午运行。由于视图也受到影响,因此不能进行参数嗅探,也不能查询计划的任何其他问题。我们不知道是什么导致这些波动。

其中一个受影响的视图如下所示:

create view [dbo].[View01]
as
select
 A.Id                                                                                                               as Id
,A.Client_Id                                                                                                        as Client_Id
,A.Status                                                                                                           as Status
,(select count(Id) from [dbo].[Table01] where Client_Id = A.Client_Id and Adress_Id = A.Id and Status = A.Status)   as CountTable01
,(select count(Id) from [dbo].[Table02] where Client_Id = A.Client_Id and Adress_Id = A.Id and Status = A.Status)   as CountTable02
,(select count(Id) from [dbo].[Table03] where Client_Id = A.Client_Id and Adress_Id = A.Id and Status = A.Status)   as CountTable03
,(select count(Id) from [dbo].[Table04] where Client_Id = A.Client_Id and Adress_Id = A.Id and Status = A.Status)   as CountTable04
,(select count(Id) from [dbo].[Table05] where Client_Id = A.Client_Id and Adress_Id = A.Id and Status = A.Status)   as CountTable05
,(select count(Id) from [dbo].[Table06] where Client_Id = A.Client_Id and Adress_Id = A.Id and Status = A.Status)   as CountTable06
,(select count(Id) from [dbo].[Table07] where Client_Id = A.Client_Id and Adress_Id = A.Id and Status = A.Status)   as CountTable07
,(select count(Id) from [dbo].[Table08] where Client_Id = A.Client_Id and Adress_Id = A.Id and Status = A.Status)   as CountTable08
from [dbo].[Address] as A;

此视图上查询的默认持续时间约为0.2秒。

修改

在表格上设置以下索引:

Table01     nonclustered index on (Client_Id, Status) include (Adress_Id)
Table02     nonclustered index on (Client_Id, Status) include (Adress_Id)
Table03     nonclustered index on (Client_Id, Status) include (Adress_Id)
Table04     nonclustered index on (Client_Id, Adress_Id)
Table05     nonclustered index on (Client_Id, Status) include (Adress_Id)
Table06     no index
Table07     nonclustered index on (Client_Id, Status) include (Adress_Id)
Table08     nonclustered index on (Client_Id, Adress_Id)

2 个答案:

答案 0 :(得分:0)

这可能恰恰是由于锁定。尝试添加表提示:“with(nolock)”到你的查询,如果它可以做脏读。例如:

create view [dbo].[View01]
as
select
 A.Id                                                                                                               as Id
,A.Client_Id                                                                                                        as Client_Id
,A.Status                                                                                                           as Status
,(select count(PK) from [dbo].[Table01] with (nolock) where Client_Id = A.Client_Id and Adress_Id = A.Id and Status = A.Status)   as CountTable01
,(select count(PK) from [dbo].[Table02] with (nolock) where Client_Id = A.Client_Id and Adress_Id = A.Id and Status = A.Status)   as CountTable02
,(select count(PK) from [dbo].[Table03] with (nolock) where Client_Id = A.Client_Id and Adress_Id = A.Id and Status = A.Status)   as CountTable03
,(select count(PK) from [dbo].[Table04] with (nolock) where Client_Id = A.Client_Id and Adress_Id = A.Id and Status = A.Status)   as CountTable04
,(select count(PK) from [dbo].[Table05] with (nolock) where Client_Id = A.Client_Id and Adress_Id = A.Id and Status = A.Status)   as CountTable05
,(select count(PK) from [dbo].[Table06] with (nolock) where Client_Id = A.Client_Id and Adress_Id = A.Id and Status = A.Status)   as CountTable06
,(select count(PK) from [dbo].[Table07] with (nolock) where Client_Id = A.Client_Id and Adress_Id = A.Id and Status = A.Status)   as CountTable07
,(select count(PK) from [dbo].[Table08] with (nolock) where Client_Id = A.Client_Id and Adress_Id = A.Id and Status = A.Status)   as CountTable08
from [dbo].[Address] as A with (nolock);

答案 1 :(得分:0)

你应该在"慢慢地"期间使用sys.dm_os_waiting_tasks。看看哪个会话正在等待什么。

一旦您发现了等待时间,您可以在这里查询它们的含义:std::any

请使用此DMV的结果更新您的问题,以便我们能够找到原因。