简体   繁体   English

所有突然的Sql Server超时

[英]All of a Sudden , Sql Server Timeout

We got a legacy vb.net applicaction that was working for years But all of a sudden it stops working yesterday and gives sql server timeout Most part of application gives time out error , one part for example is below code : 我们有一个已经使用了多年的遗留vb.net应用程序,但是突然之间,它昨天停止工作并给出了sql server timeout大部分应用程序给出了超时错误,例如下面的代码部分:

command2 = New SqlCommand("select * from Acc order by AccDate,AccNo,AccSeq", SBSConnection2)
        reader2 = command2.ExecuteReader()
    If reader2.HasRows() Then
        While reader2.Read()
            If IndiAccNo <> reader2("AccNo") Then
                CAccNo = CAccNo + 1
                CAccSeq = 10001
                IndiAccNo = reader2("AccNo")
            Else
                CAccSeq = CAccSeq + 1
           End If
            command3 = New SqlCommand("update Acc Set AccNo=@NewAccNo,AccSeq=@NewAccSeq where AccNo=@AccNo and AccSeq=@AccSeq", SBSConnection3)
            command3.Parameters.Add("@AccNo", SqlDbType.Int).Value = reader2("AccNo")
        command3.Parameters.Add("@AccSeq", SqlDbType.Int).Value = reader2("AccSeq")
          command3.Parameters.Add("@NewAccNo", SqlDbType.Int).Value = CAccNo
        command3.Parameters.Add("@NewAccSeq", SqlDbType.Int).Value = CAccSeq

      command3.ExecuteNonQuery()
    End While
End If

It was working and now gives time out in command3.ExecuteNonQuery() Any ideas ? 它正在工作,现在在command3.ExecuteNonQuery()中超时。有什么想法吗?

~~~~~~~~~~~ Some information : ~~~~~~~~~~~~一些信息:

There isnt anything that has been changed on network and the app uses local database The main issue is that even in development environment it donest work anymore 网络上没有任何更改,该应用程序使用本地数据库。主要问题是,即使在开发环境中,它也不再起作用

I'll state the obvious - something changed. 我要说的很明显-有些改变。 It could be an upgrade that isn't having the desired effect - it could be a network component going south - it could be a flakey disk - it could be many things - but something in the access path has changed. 可能是升级没有取得预期的效果-可能是网络组件向南-可能是Flakey磁盘-可能有很多东西-但访问路径中的某些内容已更改。 What other problem indications are you seeing, including problems not directly related to this application? 您还看到其他什么问题迹象,包括与该应用程序不直接相关的问题? Where is the database stored (local disk, network storage box, written by angels on the head of a pin, other)? 数据库存储在哪里(本地磁盘,网络存储盒,用针头上的天使写的其他)? Has your system administrator "helped" or "improved" things somehow? 您的系统管理员是否以某种方式“帮助”或“改进”了事情? The code has not worn out - something else has happened. 代码还没用完-发生了其他事情。

Is it possible that this query has been getting slower over time and is now just exceeded the default timeout? 随着时间的流逝,查询可能变得越来越慢,现在刚超过默认超时时间吗?

How many records would be in the acc table and are there indexes on AccNo and AccSeq? acc表中将有几条记录,并且AccNo和AccSeq上有索引吗? Also what version of SQL are you using? 您还使用什么版本的SQL?

How long since you updated statistics and rebuilt indexes? 您更新统计信息和重建索引有多长时间了?

How much has your data grown? 您的数据增长了多少? Queries that work fine for small datasets can be bad for large ones. 对小型数据集有效的查询可能对大型数据集不利。

Are you getting locking issues? 您是否遇到锁定问题? [AMJ] Have you checked activity monitor to see if there are locks when the timeout occurs? [AMJ]您是否检查了活动监视器以查看发生超时时是否有锁?

Have you run profiler to grab the query that is timing out and then run it directly onthe server? 您是否运行了探查器以捕获正在超时的查询,然后直接在服务器上运行它? Is it faster then? 然后更快吗? Could also be network issues in moving the information from the database server to the application. 将信息从数据库服务器移动到应用程序时,也可能是网络问题。 That would at least tell you if it s SQl Server issue or a network issue. 至少可以告诉您是SQl Server问题还是网络问题。

And like Bob Jarvis said, what has recently changed on the server? 就像鲍勃·贾维斯(Bob Jarvis)所说,服务器上最近发生了什么变化? Has something changed in the database structure itself? 数据库结构本身发生了什么变化? Has someone added a trigger? 有人添加了触发器吗?

I would suggest that there is a lock on one of the records that you are trying to update, or there are transactions that haven't been completed. 我建议您尝试更新的一条记录上有一个锁,或者有尚未完成的事务。

I know this is not part of your question, but after seeing your sample code i have to make this comment: is there any chance you could change your method of executing sql on your database? 我知道这不是您的问题的一部分,但是在看到示例代码后,我必须发表以下评论:您是否有可能更改在数据库上执行sql的方法? It is bad on so many levels. 在很多层面上都是不好的。

any chances of a "quotes" as part of the strings you are passing to queries? 您传递给查询的字符串中是否有“引号”的机会?

any chances of date dependent queries where a special condition is not working anymore? 在特殊条件不再起作用的情况下,是否有任何可能依赖日期的查询?

Perhaps should you set the CommandTimeout property to a higher delay? 也许您应该将CommandTimeout属性设置为更高的延迟?

Doing so will allow your command to wait a little longer for the underlying database to respond. 这样做将使您的命令等待更长的时间,以便基础数据库响应。 As I see it, perhaps are you not letting time enough for your database engine to perform all what is required before creating another command to perform your update . 如我所见,也许您没有在创建另一个命令来执行update之前为数据库引擎提供足够的时间来执行所有必需的操作。

Know that the SqlDataReader continues to "SELECT" while feeding the in-memory objects. 知道SqlDataReader在提供内存中对象的同时继续"SELECT" Then, while reading, you require your code to update some other table, which your DBE just can't handle, by the time your SqlCommand requires, than times out. 然后,在读取时,您需要代码更新SqlCommand要求的时间,而DBE不能处理的其他表超时。

Have you tested the obvious? 你测试了明显吗?

Have you run the "update Acc Set AccNo=@NewAccNo,AccSeq=@NewAccSeq where AccNo=@AccNo and AccSeq=@AccSeq" query directly on your SQL Server Management Studio? 您是否直接在SQL Server Management Studio上运行了“更新Acc Set AccNo = @ NewAccNo,AccSeq = @ NewAccSeq,其中AccNo = @ AccNo和AccSeq = @ AccSeq”查询? (Please replace the variables with some hard coded values) (请用一些硬编码值替换变量)

Have you run the same test on another colleague's PC? 您是否在另一位同事的PC上运行了相同的测试?

Can we make sure that the SQLConnection is working fine. 我们能否确保SQLConnection正常工作。 It could be the case that SQL login criteria is changed and connection is getting a timeout. 可能是因为更改了SQL登录标准并且连接超时。 It will be probably more helpful if you post the error message here. 如果您在此处发布错误消息,可能会更有帮助。

You can rewrite the update as a single query. 您可以将更新重写为单个查询。 This will run much faster than the original query. 这将比原始查询快得多。

UPDATE subquery
SET AccNo = NewAccNo, AccSeq = NewAccSeq
FROM
    (SELECT AccNo, AccSeq, 
        DENSE_RANK() OVER (PARTITION BY AccNo ORDER BY AccNo) NewAccNo,
        ROW_NUMBER() OVER (PARTITION BY AccNo ORDER BY AccDate, AccSeq)
            + 10000 NewAccSeq
     FROM Acc) subquery

After HLGEM's suggestions, I would check the data and make sure it is okay. 根据HLGEM的建议,我将检查数据并确保一切正常。 In cases like this, 95% of the time it is the data. 在这种情况下,有95%的时间是数据。

Make sure disk is defragged. 确保磁盘已碎片整理。 Yes, I know, but it does make a difference. 是的,我知道,但这确实有所作为。 Not the built-in defragger. 不是内置的碎片整理程序。 One that defrags and optimizes like PerfectDisk . PerfectDisk一样进行碎片整理和优化的软件。

This may be a bit of a long shot, but if your entire application has stopped working, have you run out of space for the transaction log in your database? 这可能会花费很多时间,但是如果您的整个应用程序都已停止工作,那么数据库中事务日志的空间是否用完了? Either it's been specified to an absolute size, and that has been reached, or your disk is just full. 已将其指定为绝对大小,并且已经达到了绝对大小,或者磁盘已满。

May be your tables include more information, and defined SqlConnection.ConnectionTimeout property value in config file with little value. 可能是您的表包含更多信息,并且配置文件中定义的SqlConnection.ConnectionTimeout属性值很少。 And this value isn't necessary to execute your queries. 而且此值对于执行查询不是必需的。 you can trying optimize your queries, and also rebuilt indexes. 您可以尝试优化查询,还可以重建索引。

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

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