2 related questions :
question #1 :
I have a myTable with id , name , lastname
id is seed (pk).
name has index on it. //not nullAble.
which query is faster ?
select id , name , lastname from mytable where name = name //(name has index on it!)
or
select id , name , lastname from mytable
question #2 : *now lets assume name is nullable.*
from the first sample
there is a select query sp which its parameter is @name + @lastName
(returning all rows where name = @name or lastName= @lastName)
which query is faster ?
select id , name , lastname from mytable where
(@name is null or name=@name)
and
(@lastName is null or lastName=@lastName)
here if @name or @lastName are null – so it wont compare name=@name
or
select id , name , lastname from mytable where
(name = isNull(@name,name))
and
(lastName= isNull(@lastName,lastName))
here , if @name or @lastName are null , so there STILL will be equalization
ofname= nameandlastName=lastName, which i think is bad
performance.
please help ?
As written, the first two queries are equivalent.
name = namewill always evaluate true, and will be a constant evaluation basically. Both of those queries will run a table scan.The second two queries should be equivalent as well.
ISNULLmay be slightly faster but I doubt it – the optimizer expands it out to aCASEstatement so it will likely short circuit.