nested query vs jOINs

前端 未结 4 390
醉话见心
醉话见心 2021-01-12 14:08

whos could be much efficient if I use nestted subquery, JOINs Or maybe temp tables .. another question : in subqueries if i use IN Cl

4条回答
  •  旧巷少年郎
    2021-01-12 14:38

    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
                    ) 
    

提交回复
热议问题