我有一个存储过程,当从我们的网站(通过网站连接池)调用时偶尔会超时。一旦超时,它就会一直被锁定在超时状态,直到从 Management Studio 会话中使用 drop/create 或 sp_recompile 重新编译该过程。
在超时时,使用 Management Studio 对相同过程使用相同参数不会超时。
通过 Management Studio 执行“ALTER PROCEDURE”并(相当彻底地)更改过程的内部执行并没有清除超时 - 在运行完整的 sp_recompile 之前它不会清除。
存储过程以OPTION (RECOMPILE)
该过程调用了两个函数,它们在产品的其余部分中普遍使用。使用这些功能(以类似方式)的其他程序都可以工作,即使在相关程序超时的期间也是如此。
如果有人可以就可能导致此超时的原因提供任何进一步的建议,将不胜感激。
存储过程如下:
ALTER PROCEDURE [dbo].[sp_g_VentureDealsCountSizeByYear] (
@DateFrom AS DATETIME = NULL
,@DateTo AS DATETIME = NULL
,@ProductRegion AS INT = NULL
,@PortFirmID AS INT = NULL
,@InvFirmID AS INT = NULL
,@SpecFndID AS INT = NULL
) AS BEGIN
-- Returns the stats used for Market Overview
DECLARE @IDs AS IDLIST
INSERT INTO @IDs
SELECT IDs
FROM dbo.fn_VentureDealIDs(@DateFrom,@DateTo,@ProductRegion,@PortFirmID,@InvFirmID,@SpecFndID)
CREATE TABLE #DealSizes (VentureID INT, DealYear INT, DealQuarter INT, DealSize_USD DECIMAL(18,2))
INSERT INTO #DealSizes
SELECT vDSQ.VentureID, vDSQ.DealYear, vDSQ.DealQuarter, vDSQ.DealSize_USD
FROM dbo.fn_VentureDealsSizeAndQuarter(@IDs) vDSQ
SELECT
yrs.Years Heading
,COUNT(vDSQ.VentureID) AS Num_Deals
,SUM(vDSQ.DealSize_USD) AS DealSize_USD
FROM tblYears yrs
LEFT OUTER JOIN #DealSizes vDSQ ON vDSQ.DealYear = yrs.Years
WHERE (
((@DateFrom IS NULL) AND (yrs.Years >= (SELECT MIN(DealYear) FROM #DealSizes))) -- If no minimum year has been passed through, take all years from the first year found to the present.
OR
((@DateFrom IS NOT NULL) AND (yrs.Years >= DATEPART(YEAR,@DateFrom))) -- If a minimum year has been passed through, take all years from that specified to the present.
) AND (
((@DateTo IS NULL) AND (yrs.Years <= (SELECT MAX(DealYear) FROM #DealSizes))) -- If no maximum year has been passed through, take all years up to the last year found.
OR
((@DateTo IS NOT NULL) AND (yrs.Years <= DATEPART(YEAR,@DateTo))) -- If a maximum year has been passed through, take all years up to that year.
)
GROUP BY yrs.Years
ORDER BY Heading DESC
OPTION (RECOMPILE)
END