C#、Linq2SQL:CommandTimeout 应该有多长以及应该在哪里设置?

发布于 2024-07-21 16:45:40 字数 550 浏览 5 评论 0原文

我们使用 C# 和 Linq2SQL 从数据库获取某些报告的数据。 在某些情况下,这需要一段时间。 超过 30 秒,这似乎是默认的 CommandTimeout

所以,我想我必须增加CommandTimeout。 但问题是,多少钱? 把它设置得很高是不是很糟糕? 如果客户正在尝试做某事,而仅仅因为他的数据库中的数据碰巧比普通客户多得多,他就因为超时而无法获取报告,这不是很糟糕吗? 但我怎么知道可能需要多长时间呢? 有什么方法可以将其设置为无穷大吗? 或者这被认为是非常糟糕的?

我应该在哪里设置它? 我有一个静态数据库类,当我需要时它会为我生成一个新的数据上下文。 我可以创建一个常量并在创建新数据上下文时设置它吗? 或者应该根据用例设置为不同的值? 对于根本不需要太多时间的事情设置较长的超时是不是很糟糕? 或者这真的不重要吗?

ConnectionTimeout 太高当然会更烦人。 但是否存在用户/客户希望某些内容超时的情况? SQL 服务器是否会冻结而导致命令永远无法完成?

We are using C# and Linq2SQL to get data from a database for some reports. In some cases this takes a while. More than 30 seconds, which seems to be the default CommandTimeout.

So, I guess I have to up the CommandTimeout. But question is, how much? Is it bad to just set it very high? Wouldn't it be bad if a customer was trying to do something and just because he happend to have a lot more data in his database than the average customer he couldn't get his reports out because of timeouts? But how can I know how much time it potentially could take? Is there some way to set it to infinity? Or is that considered very bad?

And where should I set it? I have a static database class which generates a new datacontext for me when I need it. Could I just create a constant and set it whenever I create a new datacontext? Or should it be set to different values depending on the usecase? Is it bad to have a high timeout for something that wont take much time at all? Or doesn't it really matter?

Too high ConnectionTimeout can of course be more annoying. But is there a case where a user/customer would like something to time out? Can the SQL server freeze so that a command never finishes?

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

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

发布评论

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

评论(4

四叶草在未来唯美盛开 2024-07-28 16:45:40

CommandTimeout 等确实应该只根据特定场景增加。 这可以避免意外的长时间阻塞等场景(或更糟糕的是:未检测到的死锁场景)。 至于高...查询需要多长时间? 添加一些净空,你就会得到答案。

当然,另一件事是减少查询所需的时间。 这可能意味着在存储过程中手动优化一些 TSQL,通常与检查索引策略相结合,也许还需要进行更大的更改,例如非规范化或其他模式更改。 这还可能涉及数据仓库策略,以便您可以将负载转移到单独的数据库(远离事务数据),并使用针对报告进行优化的架构。 也许是星型模式。

我不会将其设置为无穷大...我不希望花费很长时间来运行报告。 选择一个对报告有意义的数字。

是的,SQL Server 可能会冻结,从而导致命令永远无法完成。 一个开放的阻塞事务是最简单的......得到两个,你可能会死锁。 通常系统会检测到本地死锁 - 但并非总是如此,特别是在涉及 DTC 时(即非本地锁)。

CommandTimeout etc should indeed only be increased on per-specific-scenario basis. This can avoid unexpectedly long blocking etc scenarios (or worse: the undetected deadlock scenario). As for how high... how long does the query take? Add some headroom and you have your answer.

The other thing to do, of course, is to reduce the time the query takes. This might mean hand-optimising some TSQL in a sproc, usually in combination with checking the indexing strategy, and perhaps bigger changes such as denormalization, or other schema changes. This might also involve a data-warehousing strategy so you can shift load to a separate database (away from the transactional data), with a schema optimised for reporting. Maybe a star-schema.

I wouldn't set it to infinity... I don't expect it to take forever to run a report. Pick a number that makes sense for the report.

Yes, SQL Server can freeze so that a command never finishes. An open blocking transaction would be the simplest... get two and you can deadlock. Usually the system will detect a local deadlock - but not always, especially if DTC is involved (i.e. non-local locks).

吃素的狼 2024-07-28 16:45:40

恕我直言,供您的用户设置 ConnectionTimeout 值的高级选项将比您确定的任何常量值更好。

IMHO, An advanced option for your user to set the ConnectionTimeout value would be better than any constant value you determine.

对你的占有欲 2024-07-28 16:45:40

默认情况下,主键上有一个聚集索引。 我发现下面的脚本(我认为是在 msdn 上)将生成代码来创建 SQL Server 认为有用的任何索引(def 适用于 SQL2008,我认为这是在 2005 年引入的):

SELECT 

  migs.avg_total_user_cost * (migs.avg_user_impact / 100.0) * (migs.user_seeks + migs.user_scans) AS improvement_measure, 

  'CREATE INDEX [missing_index_' + CONVERT (varchar, mig.index_group_handle) + '_' + CONVERT (varchar, mid.index_handle) 

  + '_' + LEFT (PARSENAME(mid.statement, 1), 32) + ']'

  + ' ON ' + mid.statement 

  + ' (' + ISNULL (mid.equality_columns,'') 

    + CASE WHEN mid.equality_columns IS NOT NULL AND mid.inequality_columns IS NOT NULL THEN ',' ELSE '' END 

    + ISNULL (mid.inequality_columns, '')

  + ')' 

  + ISNULL (' INCLUDE (' + mid.included_columns + ')', '') AS create_index_statement, 

  migs.*, mid.database_id, mid.[object_id]

FROM sys.dm_db_missing_index_groups mig

INNER JOIN sys.dm_db_missing_index_group_stats migs ON migs.group_handle = mig.index_group_handle

INNER JOIN sys.dm_db_missing_index_details mid ON mig.index_handle = mid.index_handle

WHERE migs.avg_total_user_cost * (migs.avg_user_impact / 100.0) * (migs.user_seeks + migs.user_scans) > 10

ORDER BY migs.avg_total_user_cost * migs.avg_user_impact * (migs.user_seeks + migs.user_scans) DESC

Primary keys will have a clustered index on them by default. I found the following script (i think it was on msdn) that will generate the code to create any indexes SQL server thinks will be useful (def works on SQL2008, I think this was introduced in 2005):

SELECT 

  migs.avg_total_user_cost * (migs.avg_user_impact / 100.0) * (migs.user_seeks + migs.user_scans) AS improvement_measure, 

  'CREATE INDEX [missing_index_' + CONVERT (varchar, mig.index_group_handle) + '_' + CONVERT (varchar, mid.index_handle) 

  + '_' + LEFT (PARSENAME(mid.statement, 1), 32) + ']'

  + ' ON ' + mid.statement 

  + ' (' + ISNULL (mid.equality_columns,'') 

    + CASE WHEN mid.equality_columns IS NOT NULL AND mid.inequality_columns IS NOT NULL THEN ',' ELSE '' END 

    + ISNULL (mid.inequality_columns, '')

  + ')' 

  + ISNULL (' INCLUDE (' + mid.included_columns + ')', '') AS create_index_statement, 

  migs.*, mid.database_id, mid.[object_id]

FROM sys.dm_db_missing_index_groups mig

INNER JOIN sys.dm_db_missing_index_group_stats migs ON migs.group_handle = mig.index_group_handle

INNER JOIN sys.dm_db_missing_index_details mid ON mig.index_handle = mid.index_handle

WHERE migs.avg_total_user_cost * (migs.avg_user_impact / 100.0) * (migs.user_seeks + migs.user_scans) > 10

ORDER BY migs.avg_total_user_cost * migs.avg_user_impact * (migs.user_seeks + migs.user_scans) DESC
厌味 2024-07-28 16:45:40
//If you have an AseCommand object instance....
AseCommand _AseCommand = new AseCommand("procedure_test");

//You can set up the timeout infinite as follows
_AseCommand.CommandTimeout = System.Threading.Timeout.Infinite;
//If you have an AseCommand object instance....
AseCommand _AseCommand = new AseCommand("procedure_test");

//You can set up the timeout infinite as follows
_AseCommand.CommandTimeout = System.Threading.Timeout.Infinite;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文