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
 General SQL Server Forums
 New to SQL Server Programming
 subqueries performances

Author  Topic 

mathmax
Yak Posting Veteran

95 Posts

Posted - 2008-06-15 : 15:25:11
Hello,

Lets consider two tables : CUSTOMERS and ORDERS.

I would like to know which of the following query is the fastest:

select * from customers C
where exists (select 0 from ORDERS O
where C.Name like 'A%' and O.Charged = 1)


select * from customers C
where exists (select 0 from ORDERS O
where O.Charged = 1) and C.Name like 'A%'


I don't want to use a JOIN clause. I just want to know if there is a difference of efficiency when the condition on C.Name is inside or outside the sub query.

Thank you in advance for any advices,

regards,

mathmax

LoztInSpace
Aged Yak Warrior

940 Posts

Posted - 2008-06-15 : 20:07:38
Have you tried looking at the query plans? They look like they would end up the same to me. I suspect this is not actually doing what you intend because there is no correlation between the sub query and the outer one.
Go to Top of Page

mathmax
Yak Posting Veteran

95 Posts

Posted - 2008-06-15 : 23:13:05
Sorry,

I forgot the condition c.ID = o.Customer_Id in both queries.

But don't you think the second query will be fastest ? In the first one, will the condition C.Name like 'A%' be evaluated for each record in Orders as it is in the sub query ?
Go to Top of Page
   

- Advertisement -