CREATE TABLE big_table
(id                 int         NOT NULL CONSTRAINT PK_bigtable PRIMARY KEY CLUSTERED
,non_indexed_column varchar(50) NOT NULL
,indexed_column     varchar(50) NOT NULL
)

insert into big_table values (1,'does exist','does exist')
insert into big_table values (2,newid(),newid())
declare @i int
set @i=2
while @i<1200000 begin
  insert into big_table select id+@i,newid(),newid() from big_table
  set @i=@i*2
end
checkpoint
-- 2,097,152 rows, 191,440 KB
-- exec sp_spaceused big_table

create index IX_bigtable_indexedcolumn ON big_table(indexed_column)
go

-- 1. Three OR'ed predicates
declare @a varchar(50)
set @a=CONVERT(varchar(50),CURRENT_TIMESTAMP,121)
IF @a LIKE '%q%'
   OR (@a NOT LIKE '%q%'
       AND EXISTS (
         SELECT *
         FROM  big_table
         WHERE non_indexed_column = 'does not exist'
         )
      )
   OR (@a NOT LIKE '%q%'
       AND EXISTS (
         SELECT *
         FROM  big_table
         WHERE indexed_column = 'does exist'
         )
      )
  SELECT DATEDIFF(ms,@a,CURRENT_TIMESTAMP)
go

-- 2. Three OR'ed predicates, cheapest predicate first
declare @a varchar(50)
set @a=CONVERT(varchar(50),CURRENT_TIMESTAMP,121)
IF @a LIKE '%q%'
   OR (@a NOT LIKE '%q%'
       AND EXISTS (
         SELECT *
         FROM  big_table
         WHERE indexed_column = 'does exist'
         )
      )
   OR (@a NOT LIKE '%q%'
       AND EXISTS (
         SELECT *
         FROM  big_table
         WHERE non_indexed_column = 'does not exist'
         )
      )
  SELECT DATEDIFF(ms,@a,CURRENT_TIMESTAMP)
go

-- 3. Nested predicates
declare @a varchar(50)
set @a=CONVERT(varchar(50),CURRENT_TIMESTAMP,121)
IF @a LIKE '%q%'
   OR (@a NOT LIKE '%q%'
       AND (
         EXISTS (
           SELECT *
           FROM  big_table
           WHERE non_indexed_column = 'does not exist'
         )
         OR EXISTS (
           SELECT *
           FROM  big_table
           WHERE indexed_column = 'does exist'
         )
       )
      )
  SELECT DATEDIFF(ms,@a,CURRENT_TIMESTAMP)
go

-- 4. Nested predicates, cheapest predicate first
declare @a varchar(50)
set @a=CONVERT(varchar(50),CURRENT_TIMESTAMP,121)
IF @a LIKE '%q%'
   OR (@a NOT LIKE '%q%'
       AND (
         EXISTS (
           SELECT *
           FROM  big_table
           WHERE indexed_column = 'does exist'
         )
         OR EXISTS (
           SELECT *
           FROM  big_table
           WHERE non_indexed_column = 'does not exist'
         )
       )
      )
  SELECT DATEDIFF(ms,@a,CURRENT_TIMESTAMP)
go

-- 5. Case expression, with cheapest predicate first
declare @a varchar(50)
set @a=CONVERT(varchar(50),CURRENT_TIMESTAMP,121)
IF CASE WHEN @a LIKE '%q%' THEN 'true'
        WHEN EXISTS (
               SELECT *
               FROM  big_table
               WHERE indexed_column = 'does exist' ) THEN 'true'
        WHEN EXISTS (
               SELECT *
               FROM  big_table
               WHERE non_indexed_column = 'does not exist' ) THEN 'true'
   END IS NOT NULL
  SELECT DATEDIFF(ms,@a,CURRENT_TIMESTAMP)
go

drop table big_table
