I'd like to clean up the results of a SQL query that is always run manually from with Management Studio. But my if/then loop is taking much longer than the individual elements.
Currently, this Select statement runs instantly (less than 1 second) and is usually empty:
Select * from A join B on A.id=B.id
Instead of an empty result set, I wanted to display a message if there were no results (this is part of a larger multi-part query so the clarity would help). I changed it to this:
If (Select count(*) from A join B on A.id=B.id)>0
begin
Select * from A join B on A.id=B.id
end
else
Select 'No Results'
Since both Select statements in there run near instantly (I checked), I expect this entire snippet to run in the same amount of time. Instead, it takes EIGHT seconds. Why is this taking so much longer and is there a simple way around it?