我有结算应用程序,它具有一些报告生成功能。在运行6-7个月后,我面临有线问题。我的sql语句在管理工作室编辑器中运行完美(4秒执行),但是当我将相同的查询放到SP时(1分9秒执行相同的查询)。表有150K行。下面是我在SP中使用的查询。我的应用显示超时已过期异常消息。我尝试将连接字符串中的超时设置为180秒。 (对于临时解决方案)但没有积极的结果。
ALTER PROCEDURE [dbo].[rpt_GetShiftEndReport](
@BillDate varchar(10),
@JobShift int,
@MonthStartDate varchar(10),
@MonthEndDate varchar(10)
)
AS
--begin tran
SET NOCOUNT ON;
SELECT Products.pCode AS ProductCode, MIN(Products.pName) AS ProductName, MIN(Products.pSize) AS ItemSize, MIN(I.gName) AS GroupName, Sales_Trans.Price,
SUM(Sales_Trans.Sales_Qty) AS SalesQty, SUM(Sales_Trans.Sales_Value) AS SalesValue, SUM(Sales_Trans.Break_Qty) AS BreakQty,
SUM(Sales_Trans.Break_Value) AS BreakValue, SUM(Sales_Trans.Return_Qty) AS ReturnQty, SUM(Sales_Trans.Return_Value) AS ReturnValue,
MIN(Products.CloseStock) AS Stock, MIN(Products.pGroup) AS GroupCode,
dbo.GetCummulativeSales(@MonthStartDate, @MonthEndDate, Products.pCode) AS CummSales
FROM Products INNER JOIN
(SELECT SalesLog_1.ProductCode, SalesLog_1.Price, SalesLog_1.Quantity AS Sales_Qty, SalesLog_1.Price * SalesLog_1.Quantity AS Sales_Value,
0 AS Break_Qty, 0 AS Break_Value, 0 AS Return_Qty, 0 AS Return_Value
FROM SalesLog AS SalesLog_1 INNER JOIN
Sales ON SalesLog_1.MemoNo = Sales.MemoNo
WHERE (SalesLog_1.BillDate = @BillDate) AND (Sales.JobShift = @JobShift)
UNION ALL
SELECT ProductCode, Price, 0 AS Sales_Qty, 0 AS Sales_Value, 0 AS Break_Qty, 0 AS Break_Value, Quantity AS Return_Qty,
Price * Quantity AS Return_Value
FROM SalesReturn
WHERE (BillDate = @BillDate) AND (JobShift = @JobShift)
UNION ALL
SELECT ProductCode, Price, 0 AS Sales_Qty, 0 AS Sales_Value, Quantity AS Break_Qty, Price * Quantity AS Break_Value, 0 AS Return_Qty,
0 AS Return_Value
FROM Breakages
WHERE (BillDate = @BillDate) AND (JobShift = @JobShift)) AS Sales_Trans ON Products.pCode = Sales_Trans.ProductCode INNER JOIN
ItemGroup AS I ON I.gCode = Products.pGroup
GROUP BY Products.pCode, Sales_Trans.Price
ORDER BY GroupCode, ItemSize DESC;
任何人都可以建议我现在该做什么。我不认为它的查询问题可能没有。行。
答案 0 :(得分:3)
如果您确定sql完全相同且params相同,那么您可能会遇到参数嗅探问题。
这是一个非常罕见的问题。我曾经只在我身上发生过一次,从那时起我就一直把这个问题编码好了。
从这里开始快速了解问题:
http://blogs.msdn.com/b/queryoptteam/archive/2006/03/31/565991.aspx
http://elegantcode.com/2008/05/17/sql-parameter-sniffing-and-what-to-do-about-it/
尝试在sp中声明一些局部变量,并将参数的值分配给它们。使用局部变量代替参数。
这是一个功能不是一个错误,但它让你走了@“$ @
答案 1 :(得分:0)
尝试删除该过程,然后使用WITH RECOMPILE
选项再次创建该过程。这会强制SQLServer不缓存过程的查询计划。作为一项规则,这是一件坏事(因为缓存计划可以节省时间,并且是存储过程往往比等效SQL语句更高效的主要原因之一),但在某些情况下,查询计划并不适合所有值,因而导致问题。
答案 2 :(得分:0)
我的朋友帮助我解决了这个问题,它是索引谁是罪魁祸首,他创造了新的指数,现在事情是光滑的黄油。
感谢所有SO成员,过上了美好的生活。