Greetings
I want to optimize a SQL Query. The problem I have a database full of such Queries and I need help for optimizing those Queries the problem with the Query is that it contains dozens of CASE blocks and within those CASE blocks there are calculations the Query results in a 2185409 rows and it takes at least 04:30 minutes and on other machines 11:00 minutes and that is too much time :( I cannot publish the whole Query as I do not have it but I can show you part of it
SUM( CASE WHEN @Unit_Code > 0
THEN (ISNULL(Item_InStore.InStore_BegBalQty,0) / ISNULL([dbo].GetFactorByUnitInStore(@Unit_Code,InStore_ID),1))
ELSE ( case
when @Unit_Type = 1 then (ISNULL(Item_InStore.InStore_BegBalQty,0))
else (ISNULL(Item_InStore.InStore_BegBalQty,0) * ISNULL((1/LargeUnit_UnitData.UnitData_FinalFactor),0))
end)
END) AS Balance,0 AS BonusBalance,
CASE
WHEN @ItemPrice > 0
THEN
( CASE
WHEN @Unit_Code > 0
THEN ((ISNULL(dbo.GetItemPrice(IUD.UnitData_ID,@ItemPrice),0)) * ISNULL([dbo].GetFactorByUnitInStore(@Unit_Code,Item_InStore.InStore_ID),0))
ELSE ( case
when @Unit_Type = 1
then ((ISNULL(dbo.GetItemPrice(IUD.UnitData_ID,@ItemPrice),0)))
else ((ISNULL(dbo.GetItemPrice(IUD.UnitData_ID,@ItemPrice),0)) / ISNULL((1/LargeUnit_UnitData.UnitData_FinalFactor),1))
end )
END)
ELSE
(
CASE
WHEN @ShowItemCost = 1
Then
CASE
WHEN @CostType = 2
THEN
( CASE
WHEN @Unit_Code > 0
THEN ((ISNULL(Item_InStore.InStore_Avg,0) + (CASE WHEN ISNULL(Item_ImaginaryPercent,0) > 0 THEN ((ISNULL(Item_ImaginaryPercent,0) / 100)* ISNULL(Item_InStore.InStore_Avg,0) ) ELSE ISNULL(Item_ImaginaryAvg,0) END)) * ISNULL([dbo].GetFactorByUnitInStore(@Unit_Code,Item_InStore.InStore_ID),0))
ELSE ( case
when @Unit_Type = 1
then ISNULL(Item_InStore.InStore_Avg,0) + (CASE WHEN ISNULL(Item_ImaginaryPercent,0) > 0 THEN ((ISNULL(Item_ImaginaryPercent,0) / 100)* ISNULL(Item_InStore.InStore_Avg,0) ) ELSE ISNULL(Item_ImaginaryAvg,0) END)
else ((ISNULL(Item_InStore.InStore_Avg,0) + (CASE WHEN ISNULL(Item_ImaginaryPercent,0) > 0 THEN ((ISNULL(Item_ImaginaryPercent,0) / 100)* ISNULL(Item_InStore.InStore_Avg,0) ) ELSE ISNULL(Item_ImaginaryAvg,0) END))/ ISNULL((1/LargeUnit_UnitData.UnitData_FinalFactor),1))
end )
END)
WHEN @CostType = 3
THEN
( CASE
WHEN @Unit_Code > 0
THEN ((ISNULL(dbo.GetItemPrice(IUD.UnitData_ID,@ItemPriceID),0)))
ELSE ( case
when @Unit_Type = 1
then ((ISNULL(dbo.GetItemPrice(IUD.UnitData_ID,@ItemPriceID),0)))
else ((ISNULL(dbo.GetItemPrice(IUD.UnitData_ID,@ItemPriceID),0)) / ISNULL((1/LargeUnit_UnitData.UnitData_FinalFactor),1))
end )
END)
ELSE
( CASE
WHEN @Unit_Code > 0
THEN ((ISNULL(Item_InStore.InStore_Avg,0)) * ISNULL([dbo].GetFactorByUnitInStore(@Unit_Code,Item_InStore.InStore_ID),0))
ELSE ( case
when @Unit_Type = 1
then ((ISNULL(Item_InStore.InStore_Avg,0)))
else ((ISNULL(Item_InStore.InStore_Avg,0)) / ISNULL((1/LargeUnit_UnitData.UnitData_FinalFactor),1))
end )
END)
END
ELSE 0
END
)
END AS AVGCost,0 AS Weight
The Problem is that the Query contains 2 - 3 blocks of the one above and I need to optimize it any ideas of help from experts :)