SQL Server 2016 : WHERE statement evaluation -


i have this

declare @par int = null  select      count(distinct(t1.id))      my_table t1 left outer join      some_other t2 on t1.a = t2.b      @par null or t2.c = @par 

@par null. where clause commented out, query fast, when uncommented - slow. result same.

why? can't spot first part of or statement true , not evaluate second? how best fix (make fast when @par null).

sql server 2016

try this,

declare @par int = null  if @par null begin     select count(distinct(t1.id))     my_table t1 end else begin     select count(distinct(t1.id))     my_table t1          inner join some_other t2 on t1.a=t2.b     t2.c = @par end 

if wish use left join approach try below, prevent loading join table when parameter has null value.

select count(distinct(t1.id)) my_table t1     left outer join some_other t2 on t1.a=t2.b         , @par not null    -- prevent loading table when @par null      @par null or t2.c = @par 

Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

mapreduce - Resource manager does not transit to active state from standby -

serialization - Convert Any type in scala to Array[Byte] and back -