views:

73

answers:

4

whos could be much efficient if I use nestted subquery, JOINs Or maybe temp tables .. another question : in subqueries if i use IN Clause twice with the same query it should be execute a twice too !? like this :

Select ...
From X 
Where Exists( Select 1  From Y Where Idx = Y.SomeColumn ) 
Or Exists( Select 1 From Y Idy = Y.SomeColumn )

how many times the sub-query SELECT * FROM Y could be executed in this query !
and what if I use this way to do so :

With XX As
(
Select ...
From Y
)
Select ...
From X
Where Exists ( Select 1 From XX Where Idx = XX.SomeColumn )
Or Exists ( Select 1 From XX Where Idy = XX.SomeColumn )

thanx :)

A: 

Joins are far quicker than the other suggestions you made.

Joins will perform the ON condition for every record whereas doing selects with a WHERE will pull in ALL records first, then perform the filter, thus being much slower.

Joins all the way !!

Paul Dragoonis
but if I should use distinct with it , it will not still fast as much as the others .. right ?
Rawhi
@Paul Curses, you beat me to it.
Andy Evans
AHA ....... !!!!
Rawhi
That is not true. The optimizer will convert one form to the other if it is really equivalent and it thinks that the other is more efficient.
Frank
the others !? like tem tables for instance !?
Rawhi
@Rawhi - Can you correct the syntax in your question? It makes no sense at the moment.
Martin Smith
It depends on your implementation wether you're doing live lookups or temp table lookups. Temp table lookups are quicker to get data from but they're not live so it's up to you.
Paul Dragoonis
+2  A: 

Use the execution plan in SQL Server Management Studio and see for yourself what runs faster against your database.

Adam
i tried to download it , but i failed hh !!
Rawhi
Adam
+2  A: 

First, your syntax is probably incorrect.Thus, the two formats would look like:

Select ...
From X 
Where Exists( Select 1  From Y Where Idx = Y.SomeColumn ) 
    Or Exists( Select 1 From Y Idy = Y.SomeColumn )

And

With XX As
    (
    Select ...
    From Y
    )
Select ...
From X
Where Exists ( Select 1 From XX Where Idx = XX.SomeColumn )
    Or Exists ( Select 1 From XX Where Idy = XX.SomeColumn )

Note the Exists statements. They are not Where Col Exists(... but instead are just Where Exists( ....

Second, the efficiency and speed will depend on the data, statistics, indexes and, at the end of the day, what the optimizer is able to make more efficient. Thus, you really need to look at the execution plan to know which is faster. Now, another form might be:

Select ...
From X 
Where Exists    (
                Select 1  
                From Y 
                Where Idx = Y.SomeColumn 
                Union All
                Select 1
                From Y
                Where Idy = Y.SomeColumn
                ) 
Thomas
thanx ....... :D
Rawhi
A: 

The two queries are equivalent, and should produce identical plans. It's a misconception that CTEs are compiled only once, providing a performance benefit. Non-recursive CTEs are just syntactic sugar for derived tables/inline views (IMO mistakenly referred to as subqueries).

Secondly, JOINs vs IN/EXISTS can produce different results. JOINs risk duplicated data, if there's two or more supporting records. EXISTS is best used if there are duplicate criteria, because it returns true on the first encounter of the criteria - making it potentially faster than IN or JOIN. There's no data duplication risk when using EXISTS or IN.

OMG Ponies
thanx a lot man :)
Rawhi