Click here to Skip to main content
15,901,849 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all
a query is:
SQL
if exists (select TOP 1 fname from names where fname like 'Sadra')
return 1
return 0

1 : is that correct?
2 : i don't want the engine to search all of database and just it investigate if table has an entity like that or no for save time?
thanks all
Posted

1 solution

Actually, it may surprise you:

SQL
if exists (select TOP 1 fname from names where fname like 'Sadra')
return 1
return 0
-- is less efficient than:
if exists (select * from names where fname like 'Sadra')
return 1
return 0


This does depend on the version of SQL Server you're using. If you're using pre 2005 then, well just don't ^_^

The first version actually asks the query to do something. It should automatically ignore that, but I would worry.

The second version can be optimised by the compiler easier. It won't search the whole table because it know the minimum required to fulfill the "EXISTS" operator.

also, your "LIKE" is wrong. you need to include wildcards ('%') or it is just a less efficient '='

Hope that helps ^_^
Andy
 
Share this answer
 
Comments
mohamadMahmodi 11-Aug-15 11:37am    
if "EXISTS" know ,my question is solved! and i don't any worry.
thanks Andy

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900