EXISTS vs. COUNT in TSql
Sometimes, programmers will perform a something like the below in TSql:
DECLARE @CountSum int
SET @CountSum = (SELECT COUNT(COL1) FROM TABLE WHERE ......)
IF @CountSum = 0
BEGIN
//Perform some TSql operations..........
END
with this code, you are really looking for the existense of records that match a criteria. If that's the case, an alternative would be to use EXISTS below
DECLARE @CountSum int
IF EXISTS(SELECT COL1 FROM TABLE WHERE.......)
BEGIN
//Perform some TSql operations..........
END
I just did a fairly quick performance test and I found that in my test scenario, the EXISTS code was a little bit faster, even with a small bit of data. As the amount of data increases, I think the performance difference should increase.
Wally