我们的查询已运行超过3年,没有任何问题。
查询是我们的ERP系统(Microsoft Dynamics AX)的标准查询,它是这样的:
SELECT SUM(B.COSTAMOUNTPOSTED),SUM(B.QTY) FROM INVENTTRANSPOSTING A,INVENTTRANS B
WHERE ((A.DATAAREAID=N'01') AND (((A.ITEMID=@P1) AND (A.INVENTTRANSPOSTINGTYPE=@P2)) AND (A.TRANSDATE>@P3)))
AND ((B.DATAAREAID=N'01') AND ((((B.VOUCHER=A.VOUCHER) AND (B.DATEFINANCIAL=A.TRANSDATE))
AND (B.INVENTTRANSID=A.INVENTTRANSID)) AND (B.PACKINGSLIPRETURNED=@P4)))
AND EXISTS (SELECT 'x' FROM INVENTDIM C WHERE ((C.DATAAREAID=N'01') AND ((((((((((C.INVENTDIMID=B.INVENTDIMID) AND (C.CONFIGID=@P5)) AND (C.INVENTSIZEID=@P6)) AND (C.INVENTCOLORID=@P7)) AND (C.INVENTSITEID=@P8)) AND (C.INVENTLOCATIONID=@P9)) AND (C.INVENTBATCHID=@P10)) AND (C.WMSLOCATIONID=@P11)) AND (C.WMSPALLETID=@P12)) AND (C.INVENTSERIALID=@P13)))
或以更易读的形式:
select TableId from inventTransPosting
where inventTransPosting.ItemId == itemId &&
inventTransPosting.InventTransPostingType == InventTransPostingType::Financial &&
inventTransPosting.TransDate > perDate
join sum(CostAmountPosted),sum(Qty) from inventTrans
where inventTrans.Voucher == inventTransPosting.Voucher &&
inventTrans.DateFinancial == inventTransPosting.TransDate &&
inventTrans.InventTransId == inventTransPosting.InventTransId &&
inventTrans.PackingSlipReturned == NoYes::No
exists join inventDim
where inventDim.inventDimId == inventTrans.inventDimId &&
inventDim.configId == configId &&
inventDim.inventColorId == inventColorId;
现在看来这个查询改变了它的执行计划,从一毫秒的执行开始,一次运行需要10到20分钟。
我尝试重新索引查询中涉及的所有3个表,但没有成功。
如果我查看查询计划,我发现索引查找已成为关键查找,但查询,索引或表中没有任何更改!
为什么会这样?
它有什么用?
答案 0 :(得分:0)
正如评论中所述,优化器可能正在估算小基数并仅使用嵌套循环。
由于我无法编辑查询文本添加重新编译,使用索引或任何(查询直接从ERP创建),解决问题的唯一方法是:
plan_guide 重构查询
我决定选择后者。我已经拆分了删除连接的查询,现在它正常工作。 (即使这不是最佳解决方案。我不喜欢MS SQL在这里工作的方式)