我正在尝试从我的 C# Web 应用程序中运行插入查询。当我从 SQL Server Management Studio 运行查询时,插入查询大约需要五分钟才能完成。从应用程序运行时,它会在三十分钟后超时(是的分钟,而不是秒)。
我已经从 VS 调试器中获取了实际的 SQL 语句并从 Mgmt Studio 运行它,它工作正常。
所有这些都是在我的开发环境中运行的,而不是生产环境。查询正在进行时没有其他 SQL Server 活动。我正在使用 SQL Server 2008 R2 进行开发。MS VS 2010 Express,Asp.Net 4.0。SQL Server 管理工作室 10。
有一个类似的问题从未得到回答:SQL server timeout 2000 from C# .NET
以下是 SET 选项:dbcc useroptions
Option MgtStudio Application
----------------------- -------------- --------------
textsize 2147483647 -1
language us_english us_english
dateformat mdy mdy
datefirst 7 7
lock_timeout -1 -1
quoted_identifier SET SET
arithabort SET NOT SET
ansi_null_dflt_on SET SET
ansi_warnings SET SET
ansi_padding SET SET
ansi_nulls SET SET
concat_null_yields_null SET SET
isolation level read committed read committed
只有 textsize 和 arithabort 不同。
任何想法为什么查询执行时间存在如此差异以及我可以做些什么来缩小这种差异?
我不确定包含查询会有多大用处,特别是因为包含模式太多了。无论如何,这里是:
INSERT INTO GeocacherPoints
(CacherID,
RegionID,
Board,
Control,
Points)
SELECT z.CacherID,
z.RegionID,
z.Board,
21,
z.Points
FROM (SELECT CacherID,
gp.RegionID,
Board=gp.Board + 10,
( CASE
WHEN (SELECT COUNT(*)
FROM Geocache g
JOIN GeocacheRegions r
ON ( r.CacheID = g.ID )
WHERE r.RegionID = gp.RegionID
AND g.FinderPoints >= 5) < 20 THEN NULL
ELSE (SELECT SUM(y.FinderPoints) / 20
FROM (SELECT x.FinderPoints,
ROW_NUMBER() OVER (ORDER BY x.FinderPoints DESC, x.ID) AS Row
FROM (SELECT g.FinderPoints,
g.ID
FROM Geocache g
JOIN Log l
ON ( l.CacheID = g.ID )
JOIN Geocacher c
ON ( c.ID = l.CacherID )
JOIN GeocacheRegions r
ON ( r.CacheID = g.ID )
WHERE YEAR(l.LogDate) = @Year
AND g.FinderPoints >= 5
AND c.ID = gp.CacherID
AND r.RegionID = gp.RegionID) x) y
WHERE y.Row <= 20)
END ) Points
FROM GeocacherPoints gp
JOIN Region r
ON r.RegionID = gp.RegionID
WHERE gp.Control = 21
AND r.RegionType IN ( 'All', 'State' )
AND gp.Board = @Board - 10) z
WHERE z.Points IS NOT NULL
AND z.Points >= 1