我有這個疑問在一個存儲過程:功能的select語句使我的查詢運行得非常緩慢
SELECT
*,
ISNULL(dbo.ReturnShortageByItemCodeLinePackage(LineId, TestPackageId, MaterialDescriptionId), 0) AS Shortage
FROM
dbo.ViewMTO
我使用的查詢中的函數來計算的整數值,你可以在這裏看到:
ALTER FUNCTION [dbo].[ReturnShortageByItemCodeLinePackage]
(@lineId int,@testpackId int, @MaterialDescriptionId int)
RETURNS float
AS
BEGIN
DECLARE @shortageQuantity float
DECLARE @MIVQuantity float
DECLARE @totalQuantity float
DECLARE @spoolQuantity float
DECLARE @ExistInSiteQuantity float
DECLARE @BeforeDoneQuantity float
SELECT
@totalQuantity = Quantity,
@spoolQuantity = QuantitySpool,
@ExistInSiteQuantity = QuantityExistInSite,
@BeforeDoneQuantity = QuantityBeforeDone
FROM
[SPMS2].[dbo].Materials
WHERE
LineId = @lineId
AND TestPackageId = @testpackId
AND MaterialDescriptionId = @MaterialDescriptionId
SELECT
@MIVQuantity = SUM(QuantityDeliver)
FROM
MaterialIssueVoucherDetails miv
JOIN
MaterialRequestContractorDetails mrc ON miv.MaterialRequestContractorDetailId = mrc.Id
WHERE
TestPackageId = @testpackId
AND LineId = @lineId
AND miv.MaterialDescriptionId = @MaterialDescriptionId
IF @MIVQuantity IS NULL
BEGIN
SET @MIVQuantity = 0
END
SET @shortageQuantity = @totalQuantity - (@BeforeDoneQuantity + @ExistInSiteQuantity + @spoolQuantity + @MIVQuantity)
RETURN round(@shortageQuantity, 3)
END
我的查詢在3分鐘內執行,對我的用戶來說是災難性的!有沒有更好的解決方案?
是ViewMTO的一種視圖嗎? – scsimon