简体   繁体   English

是什么原因导致LINQ to SQL中的Timeout过期SqlExceptions?

[英]What causes Timeout expired SqlExceptions in LINQ to SQL?

My application keeps running into Timeout Expired SqlExceptions. 我的应用程序一直运行到Timeout Expired SqlExceptions。 The thing is that this query is one that will simply have to run for a decent amount of time. 问题是这个查询只需要运行相当长的时间。 I'm having trouble figuring out where this exception is occurring though. 我无法弄清楚这个异常发生在哪里。 Is this a timeout that's created at the database server or is it happening in my program? 这是在数据库服务器上创建的超时还是在我的程序中发生? Or if it could be both, how do I figure out which one it is? 或者如果两者兼而有之,我该如何确定它是哪一个?

And lastly, how do I extend the timeout period? 最后,如何延长超时时间?

increase timeout = BAD 增加超时=
fix query = GOOD 修复查询=

I'm not sure of all the details involved, but in general the followng applies: 我不确定所涉及的所有细节,但一般来说,以下适用:

when a query runs slow do the following in management studio: 当查询运行缓慢时,请在管理工作室中执行以下操作:

  • run SET ShowPlan_All ON 运行SET ShowPlan_All ON
  • run your query 运行您的查询
  • look at the output for the word "scan". 查看“扫描”一词的输出。 your problem is there. 你的问题就在那里。

"Scan" = touch each row (table or index). “扫描”=触摸每一行(表格或索引)。 Would you like to "scan" a phone book looking for one number or use the index? 您想“扫描”电话簿寻找一个号码还是使用索引?

It is likely that you are running over the CommandTimeout set on your DataContext. 您可能正在运行DataContext上的CommandTimeout集。 This defaults to 30 seconds. 默认为30秒。

All you need to do is set the property on the DataContext before you execute your query. 您需要做的就是在执行查询之前在DataContext上设置属性。

This is probally occuring becuase the default timeout is 30 seconds. 这是偶然发生的,因为默认超时是30秒。 You can change this by setting a Timeout attribute on the connection string. 您可以通过在连接字符串上设置Timeout属性来更改此设置。 Alternatley you can set this on the DataContext . Alternatley你可以在DataContext上设置它。

Here's an article on code project about this. 这是关于此代码项目的文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM