简体   繁体   English

来自C#.NET的SQL Server超时2000

[英]SQL server timeout 2000 from C# .NET

I have run into a strange problem using SQL Server 2000 and two linked server. 我在使用SQL Server 2000和两个链接服务器时遇到了一个奇怪的问题。 For two years now our solution has run without a hitch, but suddenly yesterday a query synchronizing data from one of the databases to the other started timing out. 两年来,我们的解决方案一直运行顺利,但是昨天突然突然出现了一个查询,该查询将数据库中一个数据库的数据同步到另一个数据库。

I connect to a server in the production network, which is linked to a server containing orders I need data from. 我连接到生产网络中的服务器,该服务器链接到包含需要数据的订单的服务器。

The query contains a few joins, but basically this summarizes what is done: 该查询包含一些联接,但是基本上,这总结了完成的工作:

INSERT INTO ProductionDataCache
   (column1, column2, ...)
   SELECT tab1.column1, tab1.column2, tab2.column1, tab3.column1 ...
       FROM linkedserver.database.dbo.Table1 AS tab1
       JOIN linkedserver.database.dbo.Table2 AS tab2 ON (...)
       JOIN linkedserver.database.dbo.Tabl32 AS tab3 ON (...)
       ...
       WHERE tab1.productionOrderId = @id
       ORDER BY ...

Obviously my first attempt to fix the problem was to increase the timeout limit from the original 5 minutes. 显然,我解决该问题的第一个尝试是将超时限制从原来的5分钟提高到原来的5分钟。 But when I arrived at 30 minutes and still got a timeout, I started to suspect something else was going on. 但是当我到达30分钟后仍然超时时,我开始怀疑还有其他事情发生。 A query just does not go from executing in less than 5 minutes to over 30 minutes over night. 查询只是不会从不到5分钟执行到整夜超过30分钟。

I outputted the SQL query (which was originally in the C# code) to my logs, and decided to execute the query in the Query Analyzer directly on the database server. 我将SQL查询(最初是C#代码)输出到日志中,并决定直接在数据库服务器上的查询分析器中执行查询。 To my big surprise, the query executed correctly in less than 10 seconds. 令我惊讶的是,查询在不到10秒的时间内正确执行了。

So I isolated the SQL execution in a simple test program, and observed the same query time out both on the server originally running this solution AND when running it locally on the database server. 因此,我在一个简单的测试程序中隔离了SQL执行,并观察到最初在运行此解决方案的服务器上以及在数据库服务器上本地运行该解决方案时,相同的查询超时。 Also I have tried to create a Stored Procedure and execute this from the program, but this also times out. 我也尝试创建一个存储过程并从程序中执行此操作,但这也超时。 Running it in Query Analyzer works fine in less than a few seconds. 在Query Analyzer中运行它可以在不到几秒钟的时间内正常运行。

It seems that the problem only occurs when I execute this query from the C# program. 看来仅当我从C#程序执行此查询时,才会出现此问题。 Has anyone seen such behavior before, and found a solution for it? 有没有人以前见过这种行为,并找到了解决方案?

UPDATE: I have now used SQL Profiler on the server. 更新:我现在在服务器上使用SQL事件探查器。 The obvious difference is that when executing the query from the .NET program, it shows up in the log as "exec sp_executesql N'INSERT INTO ...'", but when executing from Query Analyzer it occurs as a normal query in the log. 明显的区别是,从.NET程序执行查询时,它在日志中显示为“ exec sp_executesql N'INSERT INTO ...'”,但是从查询分析器执行时,它在日志中作为普通查询出现。 。

Further I tried to connect the SQL Query Analyzer using the same SQL user as the program, and this triggered the problem in Query Analyzer as well. 此外,我尝试使用与程序相同的SQL用户连接SQL Query Analyzer,这也触发了Query Analyzer中的问题。 So it seems the problem only occurs when connecting via TCP/IP using a sql user. 因此,似乎该问题仅在使用sql用户通过TCP / IP连接时出现。

There are a few things to investigate; 有几件事需要调查; the first is SET options; 第一个是SET选项; these can make a huge difference to some queries. 这些可以对某些查询产生巨大的影响 Look at the SET options in the trace, and repeat these when you are testing in Management Studio (or Query Analyzer). 查看跟踪中的SET选项,并在Management Studio(或查询分析器)中进行测试时重复这些步骤。 Note that you'd have to look at an actual profile session to really see these (not just what your code logs). 请注意,你必须看一下实际的轮廓会真正看到这些(不只是你的代码日志)。

The next thing I'd look at is transactions; 我接下来要看的是交易。 what transaction model are you using in your C#? 您在C#中使用哪种交易模型? Any? 任何? None? 没有? What isolation? 什么隔离? Serializable? 可序列化? Perhaps distributed? 也许是分布式的? It could be there is some obscure lock being held on the table. 桌子上可能藏着一些晦涩的锁。

Of course, if locking is an issue you might see something via sp_who / sp_who2 while the query is running. 当然,如果锁定是一个问题,则在运行查询时,您可能会通过sp_who / sp_who2看到一些信息。

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

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