代码不断超时

发布于 2024-10-09 18:37:52 字数 2063 浏览 0 评论 0原文

因此,我们得到的这组代码由于某种原因一直超时。它运行的不是存储过程,因为它运行良好。另外,如果我们从 C# 代码中删除该参数,代码就会运行。该参数不断中断(导致超时),我们无法找出原因。

C#:

public static PTWViewList GetList(int studynumber) 
        {
            PTWViewList tempList = new PTWViewList();
            using (SqlConnection myConnection = new SqlConnection(AppConfiguration.cnARDB))
            {
                string spName = "ardb.PTWViewSelect";
                SqlCommand myCommand = new SqlCommand(spName, myConnection);
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.AddWithValue("@study", studynumber); 

                myConnection.Open();
                using (NullableDataReader myReader = new NullableDataReader(myCommand.ExecuteReader())) /*this is where the code times out*/
                {
                    tempList = new PTWViewList();
                    while (myReader.Read())
                    {
                        tempList.Add(FillDataRecord(myReader));
                    }
                    myReader.Close();
                }
            }

            tempList.ListCount = tempList.Count;
            return tempList;
        }

存储过程:

CREATE PROCEDURE [ardb].[PTWViewSelect] 
    @studynumber int = NULL,
    @quoteid uniqueidentifier = NULL,
    @lineitemid uniqueidentifier = NULL
AS
BEGIN
    SET NOCOUNT ON;

    SELECT
        [Study]
        ,[LineItemID]
        ,[QuoteID]
        ,[Total]
        ,[COOP]
        ,[VendorCost]
        ,[CustCost]
        ,[LineItemNumber]
        ,[StudyTypeCode]
        ,[GroupLeader]
        ,[PTWDate]
        ,[PONumber]
        ,[POStatus]
        ,[StudyDirector]
        ,[SL_DESC_L]
        ,[SL_Code]
        ,ProjectDescription
        ,CreatedBy
        ,chARProcess
        ,CODate
    FROM
        [ARDB].[dbo].[PTWView]
    WHERE
        (@studynumber is null or StudyNumber=@studynumber)
        AND (@quoteid is null or QuoteID=@quoteid)
        AND (@lineitemid is null or LineItemID = @lineitemid)
END

So, we've got this set of code that, for some reason, keeps timing out. It's not the stored procedure that it's running, because that runs fine. Also, if we remove the parameter from the c# code, the code runs. The parameter keeps breaking (causing it to time out) and we can't figure out why.

c#:

public static PTWViewList GetList(int studynumber) 
        {
            PTWViewList tempList = new PTWViewList();
            using (SqlConnection myConnection = new SqlConnection(AppConfiguration.cnARDB))
            {
                string spName = "ardb.PTWViewSelect";
                SqlCommand myCommand = new SqlCommand(spName, myConnection);
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.AddWithValue("@study", studynumber); 

                myConnection.Open();
                using (NullableDataReader myReader = new NullableDataReader(myCommand.ExecuteReader())) /*this is where the code times out*/
                {
                    tempList = new PTWViewList();
                    while (myReader.Read())
                    {
                        tempList.Add(FillDataRecord(myReader));
                    }
                    myReader.Close();
                }
            }

            tempList.ListCount = tempList.Count;
            return tempList;
        }

stored procedure:

CREATE PROCEDURE [ardb].[PTWViewSelect] 
    @studynumber int = NULL,
    @quoteid uniqueidentifier = NULL,
    @lineitemid uniqueidentifier = NULL
AS
BEGIN
    SET NOCOUNT ON;

    SELECT
        [Study]
        ,[LineItemID]
        ,[QuoteID]
        ,[Total]
        ,[COOP]
        ,[VendorCost]
        ,[CustCost]
        ,[LineItemNumber]
        ,[StudyTypeCode]
        ,[GroupLeader]
        ,[PTWDate]
        ,[PONumber]
        ,[POStatus]
        ,[StudyDirector]
        ,[SL_DESC_L]
        ,[SL_Code]
        ,ProjectDescription
        ,CreatedBy
        ,chARProcess
        ,CODate
    FROM
        [ARDB].[dbo].[PTWView]
    WHERE
        (@studynumber is null or StudyNumber=@studynumber)
        AND (@quoteid is null or QuoteID=@quoteid)
        AND (@lineitemid is null or LineItemID = @lineitemid)
END

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

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

发布评论

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

评论(4

偏爱自由 2024-10-16 18:37:52

你有没有尝试过

myCommand.Parameters.AddWithValue("@studynumber", studynumber);

而不是:

myCommand.Parameters.AddWithValue("@study", studynumber); 

have you tried

myCommand.Parameters.AddWithValue("@studynumber", studynumber);

instead of:

myCommand.Parameters.AddWithValue("@study", studynumber); 
娜些时光,永不杰束 2024-10-16 18:37:52

编辑
如果传递参数是问题所在,那么问题就归结为存储过程执行所需的时间。 SQL Server 的默认超时通常为 120 秒。您可以添加“连接超时”以增加数据库连接字符串中的超时并签出。

** 旧答案 - 忽略 **
如果没有堆栈跟踪,并且相信您所说的存储过程很好,我猜测它由于连接失败而超时。该代码无法连接到您的数据库服务器,因此超时。

EDIT
If passing parameters is the problem, then it comes down to how much time the stored procedure takes to execute. Default timeout for SQL server is usually 120 secs. You can add "Connect Timeout" to increase timeout in your DB connection string and check out.

** Old Answer -- Ignore **
Without stack trace, and taking your word that the stored procedure is fine, I am guessing that it is timing out due to the connection failure. The code is unable to connect to your DB server and hence timing out.

落在眉间の轻吻 2024-10-16 18:37:52

将 arithabort 设置为关闭使 sp 需要 45 秒,而不是 1。将其重新设置为 1。我更新了存储过程以将其设置为打开,应用程序中没有任何更改。改成关闭了,没啥变化。然后我删除了更新,然后应用程序运行正常。

我相信发生的事情是更新存储过程导致它重新编译,从而解决了问题。不过我对此并不是 100% 确定。

setting arithabort off made the sp take 45 seconds as opposed to 1. setting it back on changed it back to 1. I updated the stored procedure to set it on, no change in the app. Changed it to off, no change. I then removed the update and then the app worked fine.

I believe what happened is that updating the stored procedure caused it to recompile, fixing the issue. I'm not 100% sure on this though.

无人问我粥可暖 2024-10-16 18:37:52

一件事可能是 ARITHABORT 设置,将其设置为 ON...NET 默认为 OFF

在 SSMS 中运行进程,将 ARITHABORT 设置为 OFF,看看它现在是否运行得更慢,就像 .NET

示例

MyConnection.Execute "SET ARITHABORT ON"

中的那样另一件事是您的 WHERE 子句不是最佳的,请查看 您在 WHERE 子句中使用 Column=@Param OR @Param IS NULL 吗?不,它不执行

使用SSMS中的参数,过程是否运行缓慢?可以展示一下执行计划吗?

One thing could be the ARITHABORT setting, set it to ON...NET defaults to OFF

run the proc in SSMS with ARITHABORT set to OFF and see if it runs slower now like from .NET

example

MyConnection.Execute "SET ARITHABORT ON"

Another thing is that your WHERE clause is not optimal, take a look at Do you use Column=@Param OR @Param IS NULL in your WHERE clause? Don't, it doesn't perform

does the proc run slow with parameters in SSMS? Can you show the execution plan?

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