I have some tables in database. Table with objects named ObjectTbl and table with types named TypesTbl.
Thats looks like:
ObjectTbl TypesTbl
objId | TypeId | objName | typeId | typeName |
-------------------------------- ----------------------
intObjId | typeId | objName | intTypeId | typeName |
TypeId column in table ObjectTbl is a Foreign Key from TypesTbl. TypesTbl contains ~200 records, ObjectTbl ~1000000
When I execute query:
SELECT * FROM ObjectTbl obj
join TypesTbl t ON t.typeName='Type_Name'
WHERE obj.TypeId=t.typeId and (obj.objName like '%expression%' or obj.objName like '%expression2%' or obj.objName like '%expression3%')
It's works more than 10 seconds. But when I use:
declare @typeId int
set @typeId=(select typeId from TypesTbl where typeName='Type_Name')
SELECT * FROM ObjectTbl obj
WHERE obj.TypeId=@typeId and (obj.objName like '%expression%' or obj.objName like '%expression2%' or obj.objName like '%expression3%')
Thats works less than 1 second. Can anybody explain me why so?

TypesTbl? Have a look at your query plan, find the places it says nasty things likeTable Scanand put an index there.