Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 To having or not to having

Author  Topic 

mistux
Yak Posting Veteran

90 Posts

Posted - 2005-11-15 : 14:03:26
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)
)

AS
SELECT [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].CustPO
FROM (([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 @CustomerPO
END) + 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 HERE
HAVING ((([T_SetupSheetHistoryCombinationsDetail].ComboCustPartNum) Like N'%' + @SearchItem + N'%')
AND (([T_SetupSheetHistoryHeader].CustPO) LIKE + N'%' + (CASE
WHEN @CustomerPO IS NULL
THEN '%'
ELSE @CustomerPO
END) + 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.



Michael
For all your DNC needs for your CNC machines. www.mis-group.com

Kristen
Test

22859 Posts

Posted - 2005-11-15 : 14:11:39
I believe SQL Server will automatically move a HAVING to the WHERE if it can be handled there.

Your wildcard means that all rows will be checked - there's no easy way to do WHERE MyColumn LIKE '%FOO'. However, when you put it in the HAVING its only got to test the matched results, rather every originating row. I can envisage situations where it is quicker to total the aggregates and then throw away some of them, rather than ONLY totalling "appropriate" rows.

Kristen
Go to Top of Page

mistux
Yak Posting Veteran

90 Posts

Posted - 2005-11-15 : 14:35:02
I did some testing and discovered this:

When I run the sp in Query Anylizer it run in 5 seconds.

When I run it from my Access 2003 direclty it times out after 60, and I am woundering if it has soemthing to do with how Access is passing a "no entry" to the sp.

On both tests, I entered all the paramters except for the @CustomerPO, well the QA makes the @CustomerPO as Null and in Access, I am not sure what it is passing when I simply hit enter when the prompt for it comes up. Access does have a dropdown for default or Null but even when I try selecting the Null from the dropdown it times out.

Does my Case...Null code need to be modified, or is ther ea better way of setting it to null if nothign is passed?

Any thoughts?

Michael
For all your DNC needs for your CNC machines. www.mis-group.com
Go to Top of Page
   

- Advertisement -