SQL Server ignores Index hints

I have a sub query to produces 560 rows of data and the results are instantly displayed.
I now want to join a table (called T_Trans) to this query using the primary key.  
The new query takes forever even if I explicitely tell it to use the primary index of the joined table.

This query runs instantly...

Select substring(cast(Script as varchar(200)),73,11) as ref, MsgId,SourceId,CreDate,ArriveDate
  , substring(cast(Script as varchar(200)),37,4) as cno
  , substring(cast(Script as varchar(200)),50,6) as tno
from QueueIn
where Processed=0
and CreDate<='17-Nov-2008'
and ArriveDate>='17-Nov-2008'

This query takes forever (more than 30 seconds)...

Select a.*
from
(
Select substring(cast(Script as varchar(200)),73,11) as ref, MsgId,SourceId,CreDate,ArriveDate
  , substring(cast(Script as varchar(200)),37,4) as cno
  , substring(cast(Script as varchar(200)),50,6) as tno
from QueueIn
where Processed=0
and CreDate<='17-Nov-2008'
and ArriveDate>='17-Nov-2008'
) a  
join T_Trans  with (index (PK_T_Trans))
on T_Trans.CompanyNo=a.cno
and T_Trans.TransNo=a.tno

The T_Trans table has two fields (CompanyNo and TransNo) as part of its primary key.  And no this can not be changed.

I would have thought that SQL join the record in the T_Trans table instantly by looking up the primary key.
LVL 1
enigmasolutionsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

enigmasolutionsAuthor Commented:
BTW Script is a Text field in my QueueIn table.
I believe I have worked out what my problem is.

In trying to cast CNo as in integer I found that some CNo values were not integers.
So this problem will no doubt be solves when I work that out.
I will post an answer if I find it.
0
enigmasolutionsAuthor Commented:
Yes that was it.

The index hints get ignored when the type of the joining fields do not match up.  And in fact it would appear that no attempt is made to use the primary key index in this case.

The solution was to cast CNo and TNo as integers.

I also found that some results for CNo were text that could not be converted to integers - so I had to add extra criteria to filter them out to enable the casting.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.