Groups | Blog | Home
all groups > sql server (microsoft) > february 2006 >

sql server (microsoft) : More opimized query between these two EXISTS and TOP


binoj7 NO[at]SPAM gmail.com
2/22/2006 12:01:38 PM
Which would be more optimized of the two queries given below when using
T-Sql


IF (SELECT TOP 1 1 FROM table_name WHERE column_name = 'xxx')
BEGIN
-- code here
END

OR

IF EXISTS (SELECT 1 FROM table_name WHERE column_name = 'xxx')
BEGIN
-- code here
END


Of course IF EXISTS (SELECT TOP 1 1 FROM table_name WHERE column_name =
'xxx') should be the fastest, but among the two given which would be
faster
Jens
2/22/2006 11:13:04 PM
Hi,

there is no difference between using

IF EXISTS (SELECT 1 FROM table_name WHERE column_name = 'xxx')
BEGIN
-- code here
END

Or

IF EXISTS (SELECT * FROM table_name WHERE column_name = 'xxx')
BEGIN
-- code here
END

There is only a check for existence as no row is given back.

HTH, Jens Suessmeyer.
AddThis Social Bookmark Button