I have the following SQL query:
SELECT top 100 Id
FROM TestTable
where
(Code like 'a000' + '%' or contains(Desc , 'a000*' ) )
AND (Active!='N' or Active is null)
AND substring(Code,1,1) in ('1','2','3','4','5','6','7','8','9','0')
ORDER BY Code
It takes almost 70 ms to run but I have set the active to 'N' when the first character of code is not numeric, so the last where clause is unnecessary but after removing that the following query takes 6 seconds to run!
SELECT top 100 Id
FROM TestTable
where
(Code like 'a000' + '%' or contains(Desc , 'a000*' ) )
AND (Active!='N' or Active is null)
ORDER BY Code
I also run this query with database engine tuning advisor but it doesn't have any recomandation.