我在从 SQL dB 检索产品的 SQL 存储过程中遇到间歇性问题。我们有大约 3500 个活跃用户,并且通常有大约 15-30 个用户使用该程序一次登录。在过去的一周里,我收到了超时过期的消息,如下所示:
System.Data.SqlClient.SqlException:超时已过期。在操作完成之前超时时间已过或服务器没有响应。在 System.Data.SqlClient.SqlConnection.OnError(SqlException 异常,Boolean breakConnection) 在 System.Data.SqlClient.SqlInternalConnection.OnError(SqlException 异常,Boolean breakConnection) 在 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) 在 System .Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) 在 System.Data.SqlClient.SqlDataReader.ConsumeMetaData() 在 System.Data.SqlClient.SqlDataReader.get_MetaData() 在 System .Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,RunBehavior runBehavior,
SP 一次带回 12 个产品(如果选择下拉框,则更多),当我手动运行时运行速度很快。在过去的一周中,这可能发生了 20 次,不同的用户,在一天中的不同时间,因此我在运行程序或使用测试帐户浏览网站时无法重新创建错误。我在某处读到添加 NOLOCK 或增加命令超时可能会解决它,但这只是一个快速掩盖,而不是解决根本问题(更不用说 NOLOCK 的不良数据影响)。
显示产品的 SQL 部分在这里:
INSERT INTO @Products (WWHEAD, WWPROD, WWDESC, CTNQTY, PRC1, PRC4, [FILENAME], DPQIS, QTYL, FileReference, LineId, Price5, PriceGroup, Dimensions)
(SELECT A.WWHEAD, A.WWPROD, A.WWDESC, A.CTNQTY,
CASE WHEN @IsPrice5 = 5 THEN LTRIM(RTRIM(A.Price5)) ELSE LTRIM(RTRIM(A.PRC1)) END,
CASE WHEN @IsPrice5 = 5 THEN A.Price5 ELSE A.PRC4 END,
[FILENAME],
(CAST((DPQIS-DPALOC) AS INT)),
D.QTY,
'WebImage',
LineId,
CASE WHEN @IsPrice5 = 5 OR A.Price5 = A.PRC4 THEN NULL ELSE A.PRICE5 END,
LTRIM(RTRIM(PG.PriceCode)) + ' ' + LTRIM(RTRIM(PG.GroupDesc)) + ' ' + LTRIM(RTRIM(PG.DiscDesc)),
Dimensions
FROM PRODUCTS A
INNER JOIN STOCK C ON A.WWPROD = C.DPPROD AND DPDEPO = 'CH'
INNER JOIN PROD_COPY W ON A.WWPROD = W.WWPROD
INNER JOIN PRODGRP PD ON PD.GPNUM = W.WWPRGP
LEFT OUTER JOIN ORDERS D ON D.OrderID = @OrdId AND A.WWPROD = D.ProdCode
LEFT OUTER JOIN PRGRP PG ON A.WWPCGP = PG.PriceCode
LEFT OUTER JOIN PRODALT_GRP X ON X.ProdCode = A.WWPROD
WHERE A.WWBONS = 'B'
AND A.WSID = 'WS'
AND (LTRIM(RTRIM(A.WWHEAD)) = LTRIM(RTRIM(@CatHead)) OR ISNULL(@CatHead, '0') = '0')
AND (LTRIM(RTRIM(A.WWSBHD)) = LTRIM(RTRIM(@CatSub)) OR ISNULL(@CatSub, '0') = '0')
AND (LEFT(LTRIM(RTRIM(A.WWPRGP)), 2) = LTRIM(RTRIM(@BrandT1)) OR ISNULL(@BrandT1, '0') = '0')
AND (LTRIM(RTRIM(A.GRPTR2)) = LTRIM(RTRIM(@BrandT2)) OR ISNULL(@BrandT2, '0') = '0')
AND (LTRIM(RTRIM(A.GRPTR3)) = LTRIM(RTRIM(@BrandT3)) OR ISNULL(@BrandT3, '0') = '0')
AND X.AltGroupCode = @ProductFilter
AND A.PRC1 BETWEEN @MinValue AND @MaxValue
AND A.WWHEAD <> 'B20'
AND (A.WWDESC Like ('%' + @SearchString + '%') OR A.WWPROD LIKE ('%' + @SearchString + '%')
OR A.WWPROD IN (SELECT Prodcode FROM WDS..Prod_Keywords INNER JOIN WDS..Keywords ON KeyID = KeywordID WHERE Keyword LIKE ('%' + @SearchString + '%')))
AND A.WWBRN NOT IN (SELECT * from WDS.dbo.f_BrandExclusionList(@WebUserName, NULL))
AND CAST(CASE WHEN NDATMM = '' THEN '01' ELSE NDATMM END + '-'
+ CASE WHEN NDATDD = '' THEN '01' ELSE NDATDD END + '-'
+ CASE WHEN NDATYY = '' THEN '1900' ELSE NDATYY END AS DATETIME) BETWEEN @threemonths AND GETDATE()
GROUP BY PD.GPSORT, A.WWHEAD, A.WWPROD, A.WWDESC, A.CTNQTY, A.PRC1, A.PRC4, [FILENAME], C.DPQIS, C.DPALOC, D.QTY,
LineId, A.Price5, PG.PriceCode, PG.GroupDesc, PG.DiscDesc, A.WWSBHD, A.WWBRN,
A.GRPTR2, A.GRPTR3, A.WWSORT, A.WWPCGP, Dimensions
HAVING ((A.PRC1 > 20.00 AND ((DPQIS-DPALOC) > 2)) OR (A.PRC1 <= 20.00 AND ((DPQIS-DPALOC) > 8))))
ORDER BY PD.GPSORT, A.WWBRN, A.GRPTR2, A.GRPTR3, A.WWPCGP DESC, A.WWHEAD, A.WWSBHD, A.WWSORT, A.WWPROD
有任何想法吗?提前致谢。