Skip to main content
added 873 characters in body
Source Link
paparazzo
  • 5.1k
  • 1
  • 19
  • 32

as for

and TableFour.Column6 In (Select TableFour.Column6 
                                from TableFour,TableEightteen,TableNineTeen 
                                Where TableFour.Column7 = TableNineTeen.Column7 
                                and TableEightteen.Column17 = TableNineTeen.Column17 
                                and TableFour.Column10 = TableNineTeen.Column10 
                                and TableEightteen.Column18 > TableFour.Column16
                                ) 

pretty sure that can be

join TableEightteen on  TableEightteen.Column18 > TableFour.Column16 
join TableNineTeen  on  TableFour.Column7  = TableNineTeen.Column7  
                    and TableFour.Column10 = TableNineTeen.Column10  
                    and TableEightteen.Column17 = TableNineTeen.Column17

as for

and TableFour.Column6 In (Select TableFour.Column6 
                                from TableFour,TableEightteen,TableNineTeen 
                                Where TableFour.Column7 = TableNineTeen.Column7 
                                and TableEightteen.Column17 = TableNineTeen.Column17 
                                and TableFour.Column10 = TableNineTeen.Column10 
                                and TableEightteen.Column18 > TableFour.Column16
                                ) 

pretty sure that can be

join TableEightteen on  TableEightteen.Column18 > TableFour.Column16 
join TableNineTeen  on  TableFour.Column7  = TableNineTeen.Column7  
                    and TableFour.Column10 = TableNineTeen.Column10  
                    and TableEightteen.Column17 = TableNineTeen.Column17
Source Link
paparazzo
  • 5.1k
  • 1
  • 19
  • 32

Let's just look at TableOne, TableTwo, and TableFour
Most of the condition in the "in where" in are already covered in the join
And suggest you use join on for all

Select TableTwo.Column1, DatabaseTwo..
from TableOne  
inner join TableTwo  on TableOne.Column4 = TableTwo.Column4 
inner join TableFour on TableOne.Column6 = TableFour.Column6         
where 1 = 1
--- Section 2
and TableOne.Column6 in  (Select TableOne.Column6 
                            from TableOne, TableTwo, TableFour  
                            Where TableOne.Column4 = TableTwo.Column4  -- covered in base join 
                            and TableOne.Column6 = TableFour.Column6   -- covered in base join
                            and TableFour.Column16 >= @StartDate 
                            and TableFour.Column16 <  @EndDate )

Pretty sure this is the same query
Not sure if it is faster but I doubt is is slower
The query optimizer has a better chance of filtering early

Select TableTwo.Column1, DatabaseTwo..
from TableOne  
inner join TableTwo  on  TableOne.Column4 = TableTwo.Column4 
inner join TableFour on  TableOne.Column6 = TableFour.Column6   
                     and TableFour.Column16 >= @StartDate 
                     and TableFour.Column16 <  @EndDate