• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 843
  • Last Modified:

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.
0
enigmasolutions
Asked:
enigmasolutions
  • 2
1 Solution
 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now