什么可能导致 GetDate() 超时?

发布于 2024-12-17 02:47:00 字数 1040 浏览 2 评论 0原文

每隔几个月,网站的一些选定页面就会开始响应

超时已过。操作完成之前超时时间已过,或者服务器没有响应。

我运行 SQL Server Profiler 来查看数据库在哪里挂起。它位于特定的存储过程上。因此,我进行了存储过程调用并通过 Management Studio 运行它。在不到一秒的时间内返回几千行。

打开程序,我发现这只是一个简单的 select 语句。如果我取出 select 语句并在新的查询窗口中运行它,那么 Management Studio 将挂起。

因此,我开始分段运行它,并找到导致查询挂起的行

and GetDate() between EffectiveDate and ISNULL(ExpiryDate, @CurrentDate) 

GetDate() 应该是 @CurrentDate 无论如何,所以如果我切换它,它会运行得很好。为什么这很重要?据我了解,GetDate 不应该是一个昂贵的调用。我知道 IsNull 是这样,但这也没关系,因为如果我只是用 CurrentDate 替换 GetDate ,它就可以很好地处理查询。

我是否正确假设 getdate 导致此查询间歇性挂起?它如何/为什么要这样做?

一些背景知识,正如我所说,这个错误之前曾出现过,3 月 23 日、7 月 27 日、10 月 25 日和现在的 11 月 21 日。它似乎总是在月底附近发生。我确信这段时间没有发生任何事情(计划任务、维护等)。

这是在生产服务器上,所以我无法真正花时间来调试问题。一旦网站瘫痪,就必须将其恢复。我知道如何修复它,我只需要强制重新编译存储过程,但是有人知道可能导致此问题的原因吗?

SQL Server 还有另一个 UAT 数据库,相同的过程也将挂在该数据库上。但是具有完全相同的代码行的其他存储过程在两个数据库上运行良好。

编辑:我必须备份网站,所以我重新编译了存储过程。该网站现在运行良好。但该查询仍然不能自行运行。

Every few months a few select pages of a website will start responding with

Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

I ran SQL Server Profiler to see where the database is getting hung up. It's on a specific stored procedure. So I took the stored procedure call and ran it through Management Studio. Returns a few thousand rows in under a second.

Opening up the procedure, I see it's just a simple select statement. If I take the select statement out and run it in a new query window, Management Studio will hang.

So I start running it in pieces, and find the line that's causing the query to hang

and GetDate() between EffectiveDate and ISNULL(ExpiryDate, @CurrentDate) 

GetDate() is supposed to be @CurrentDate anyways, so if I switch it, it'll run fine. Why does this even matter though? As I understand, GetDate should not be an expensive call. I understand IsNull is, but that shouldn't matter either, as it can handle the query fine if I just swap out GetDate with CurrentDate.

Am I correct in assuming getdate is causing this query to hang intermittently? How/why would it do that?

A bit of background, as I said this error has popped up before, on March 23rd, July 27th, October 25th and now November 21st. It always seems to happen near the end of the month. I've been assured nothing is going on during this time (scheduled tasks, maintenance, etc.)

This is on a production server, so I haven't been able to actually take the time to debug the problem. As soon as the site goes down, it has to be brought back up. I know how to fix it, I just need to force a recompile on the stored procedure, but does anyone have any idea what could be causing this?

The SQL Server also has another UAT database, the same procedure will hang on that database as well. But other stored procedures that have that exact same line of code run fine on both databases.

Edit: I had to put the website back up, so I recompiled the stored procedure. The site now runs fine. The query still doesn't run on it's own though.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

埖埖迣鎅 2024-12-24 02:47:00

我误会了问题的原因。我分段运行的查询基本上是:

select *
from Item
left outer join category Subcategories on Subcategories.ItemKey = Item.Id,
Categories Category,
Availability
where Item.Id = @ItemId
  and GetDate() between Availability.EffectiveDate 
      and ISNULL(Availability.ExpiryDate, @CurrentDate) 

这显然不是在生产中运行的查询,只是一个匿名示例。

该查询将不会运行。为了让它运行,我最初注释掉了

EffectiveDate和ISNULL(ExpiryDate, @CurrentDate)之间的和GetDate()

如果我将GetDate()更改为它也会运行>@CurrentDate。我确信 GetDate 就是问题所在。但是,当我运行查询并注释掉 Availability 行时,它工作得很好。 Availability 是一个巨大的表。我运行了重新索引和统计更新,一切正常。我想我欠 GetDate 一个道歉。

编辑:这在完全不同的环境中再次发生。再次,GetDate 似乎是罪魁祸首。更改该行并运行该程序使一切再次正常工作。 GetDate 可能不是根本原因,但这绝对是出现问题的第一个迹象。

I was mistaken to the cause of the issue. The query I was running in pieces was basically:

select *
from Item
left outer join category Subcategories on Subcategories.ItemKey = Item.Id,
Categories Category,
Availability
where Item.Id = @ItemId
  and GetDate() between Availability.EffectiveDate 
      and ISNULL(Availability.ExpiryDate, @CurrentDate) 

This obviously is not the query being run in production, just an anonymized example.

This query would not run. To get it to run I originally commented out

and GetDate() between EffectiveDate and ISNULL(ExpiryDate, @CurrentDate)

It would also run if I changed GetDate() to @CurrentDate. I was sure GetDate was the problem. However, when I ran the query and commented out the Availablity line it worked fine. Availability is a massive table. I ran a re-index and a statistics update and everything works fine. I guess I owe GetDate an apology.

Edit: This happened again on a completely different environment. Again GetDate appears to have been the culprit. Changing that line and running the procedure got everything to work again. GetDate may not be to blame as the root cause, but it's definitely the first sign that something is going wrong.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文