简体   繁体   English

我在SQL Server 2008中调用存储过程时超时

[英]Timeout when I call a stored procedure in SQL Server 2008

From C# with EF, I call a long stored procedure with ExecuteStoreCommand 从带有EF的C#,我用ExecuteStoreCommand调用一个长存储过程

30 sec after the procedure starts, I have a timeout exception. 程序启动30秒后,我有一个超时异常。

How can I configure timeout ? 如何配置超时? On the server or in my C# client ? 在服务器上还是在我的C#客户端?

Thanks 谢谢

You could set the CommandTimeout on the underlying connection, but a much, much, much better idea would be to take the time and effort to diagnose why the timeout is happening in the first place. 您可以在底层连接上设置CommandTimeout,但是一个更好,更好,更好的想法是花费时间和精力来诊断超时发生的原因。

Even if you "solve" the problem by upping your CommandTimeout, you could potentially be causing other blocking issues in your database. 即使您通过增加CommandTimeout来“解决”问题,也可能会导致数据库中出现其他阻塞问题。 Look for blocking queries or poor query plans, or badly designed tables and indexes instead. 寻找阻塞查询或糟糕的查询计划,或者设计糟糕的表和索引。

        using (var context = new MyDbEntities())
        {
            context.CommandTimeout = 600;
            context.MyLongRunningStoredProc();
        }
using (var conn = new SqlConnection(ConnectionStrings.toMyDB))
{
    conn.Open();
    using (var cmd = new SqlCommand("myProc", conn))
    {
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandTimeout = 30; // Increase this to allow the proc longer to run
        cmd.Parameters.AddWithValue("@Param", myParam);
        cmd.ExecuteNonQuery();
    }
}

Anything you do, other than fixing the SQL in the stored procedure is just masking the real problem (the SQL). 除了在存储过程中修复SQL之外,您所做的任何事情都只是掩盖了真正的问题(SQL)。

You need to ask a question about speeding up your procedure, where you post your tables, and the stored procedure code so that it can be fixed once and for all. 您需要询问有关加快程序,发布表格的位置以及存储过程代码的问题,以便可以一劳永逸地修复它。

Using indexes solved my problem, I found out that executing the stored procedure with ExecuteStoreCommand has not the same time as in SQL. 使用索引解决了我的问题,我发现使用ExecuteStoreCommand执行存储过程的时间与SQL中的不同。

You can use the SQL Management Studio in order to find the index that you need, select you sql code for the stored procedure, right click and "Display Estimated Execution Plan" take the proposed index. 您可以使用SQL Management Studio查找所需的索引,选择存储过程的sql代码,右键单击“显示估计执行计划”,然后选择建议的索引。 This should optimize your stored procedure. 这应该优化您的存储过程。

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

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