I am trying to improve the speed of one of my sp so I moved a Having clause to the where clause and it got slower. It hought it was supposed to go the other way?(@ReleaseDate nvarchar(20),@SearchItem nvarchar(50),@CustomerPO nvarchar(30))ASSELECT [T_SetupSheetHistoryHeader].WOID, [T_SetupSheetHistoryCombinationsDetail].ComboCustPartNum AS ItemID, [T_SetupSheetHistoryCombinationsDetail].ComboColor AS OrdCol, [T_SetupSheetHistoryCombinationsDetail].ComboColorActual AS Color, [T_SetupSheetHistoryCombinationsDetail].ComboLength AS OrdLen, [T_SetupSheetHistoryCombinationsDetail].ComboLengthActual AS Length, [T_SetupSheetHistoryCombinationsDetail].ComboQty AS OrderQyt, Sum([T_SetupSheetHistoryProduction].QtyGood) AS QtyPrdcd, vShipping_WOIDItem_Balance_NOKIT.BalLng AS Balance, [T_SetupSheetHistoryHeader].PartDescription, [T_SetupSheetHistoryHeader].CustPOFROM (([T_SetupSheetHistoryHeader] INNER JOIN [T_SetupSheetHistoryCombinationsDetail] ON ([T_SetupSheetHistoryHeader].ECN = [T_SetupSheetHistoryCombinationsDetail].ECN) AND ([T_SetupSheetHistoryHeader].WOIDSub = [T_SetupSheetHistoryCombinationsDetail].WOIDSub) AND ([T_SetupSheetHistoryHeader].WOID = [T_SetupSheetHistoryCombinationsDetail].WOID) AND ([T_SetupSheetHistoryHeader].QuoteID = [T_SetupSheetHistoryCombinationsDetail].QuoteID)) LEFT JOIN vShipping_WOIDItem_Balance_NOKIT ON ([T_SetupSheetHistoryCombinationsDetail].ComboCustPartNum = vShipping_WOIDItem_Balance_NOKIT.Item) AND ([T_SetupSheetHistoryCombinationsDetail].WOID = vShipping_WOIDItem_Balance_NOKIT.WOID)) LEFT JOIN [T_SetupSheetHistoryProduction] ON ([T_SetupSheetHistoryCombinationsDetail].ECN = [T_SetupSheetHistoryProduction].ECN) AND ([T_SetupSheetHistoryCombinationsDetail].WOIDSub = [T_SetupSheetHistoryProduction].WOIDSub) AND ([T_SetupSheetHistoryCombinationsDetail].WOID = [T_SetupSheetHistoryProduction].WONum) AND ([T_SetupSheetHistoryCombinationsDetail].QuoteID = [T_SetupSheetHistoryProduction].QuoteID) AND ([T_SetupSheetHistoryCombinationsDetail].ComboCustPartNum = [T_SetupSheetHistoryProduction].ComboCustPartNum)WHERE ((([T_SetupSheetHistoryHeader].Revision)=0) AND (([T_SetupSheetHistoryHeader].ReleasedDate)>@ReleaseDate))--MOVED TO HERE AND ((([T_SetupSheetHistoryCombinationsDetail].ComboCustPartNum) Like N'%' + @SearchItem + N'%') AND (([T_SetupSheetHistoryHeader].CustPO) LIKE + N'%' + (CASE WHEN @CustomerPO IS NULL THEN '%' ELSE @CustomerPOEND) + N'%' ))GROUP BY [T_SetupSheetHistoryHeader].WOID, [T_SetupSheetHistoryCombinationsDetail].ComboCustPartNum, [T_SetupSheetHistoryCombinationsDetail].ComboColor, [T_SetupSheetHistoryCombinationsDetail].ComboColorActual, [T_SetupSheetHistoryCombinationsDetail].ComboLength, [T_SetupSheetHistoryCombinationsDetail].ComboLengthActual, [T_SetupSheetHistoryCombinationsDetail].ComboQty, vShipping_WOIDItem_Balance_NOKIT.BalLng, [T_SetupSheetHistoryHeader].PartDescription, [T_SetupSheetHistoryHeader].CustPO, [T_SetupSheetHistoryHeader].DueDate, [T_SetupSheetHistoryHeader].WOID/* FROM HEREHAVING ((([T_SetupSheetHistoryCombinationsDetail].ComboCustPartNum) Like N'%' + @SearchItem + N'%') AND (([T_SetupSheetHistoryHeader].CustPO) LIKE + N'%' + (CASE WHEN @CustomerPO IS NULL THEN '%' ELSE @CustomerPOEND) + N'%' ))*/ORDER BY [T_SetupSheetHistoryCombinationsDetail].ComboCustPartNum, [T_SetupSheetHistoryHeader].WOID
It takes 5 second if I leave in the Having, but 30 if I put it in the Where.MichaelFor all your DNC needs for your CNC machines. www.mis-group.com