Another obvious issue is the ISNULL
precludes the use of indexes.
Run this setup:
create table T1 (
ID int not null primary key,
Column1 int null
)
go
create index IX_T1 on T1(Column1)
go
declare @i int
set @i = 10000
while @i > 0
begin
insert into T1 (ID,Column1) values (@i,CASE WHEN @i%1000=0 THEN NULL ELSE @i%1000 END)
set @i = @i - 1
end
go
Then turn on execution plans and run the following:
select * from T1 where Column1 is null
select * from T1 where ISNULL(Column1,0)=0
The first uses an index seek (using IX_T1
) and is quite efficient. The second uses an index scan on the clustered index - it has to look at every row in the table.
On my machine, the second query took 90% of the time, the first 10%.