Solved

Use of index hints in MS SQL. Required? How does this work with hibernate?

Posted on 2013-06-07
9
434 Views
Last Modified: 2013-10-07
I have a query like this:

select * from table1,table2 where table2.table1id = table1.id;

table2.table1id is a foreign key that points to a corresponding entry in table1. table1 and table2 have primary keys, both called id.

table2 is also a secondary index, called myindex. Here is my problem. During tests,

Microsoft SQL Server wants to use the primary key of table2 (id) and is scanning the table1 table for the matching id to internally perform the join, instead of using the “table1id” index directly.

I know I can formulate my query using a hint to tell SQL to use the secondary key directly.

Here are my questions:

Is this necessary or are their ways to set up the database indexes so that hints are unnecessary.
This is currently written in straight SQL embedded in my Java program. If I move to hibernate, does hibernate automatically generate the hints for me?
0
Comment
Question by:efamilant
9 Comments
 
LVL 18

Expert Comment

by:UnifiedIS
ID: 39230396
If you use a join statement does it still use the other index?
select * from table1 t1
INNER JOIN table2  t2
ON t2.table1id = t1.id
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39230957
you definitely should be using ANSI join syntax (like that shown by UnifiedIS above)
and/or reversing the table order might influence results

how did you determine that mssql was using the index on table2.id and attempting to match through this?
0
 

Author Comment

by:efamilant
ID: 39231015
Using inner join syntax makes no difference in execution time.    

I provided simplified example of the query in my original post.   Here is the real query:

select doc_table.doctype,doc_table.create_date, icdtable.icdcode,code_table.token, code_table.did, code_table.pos_start,
code_table.pos_end, icdtable.icdid, icdtable.cid, icdtable.status,icdtable.kstatus from icdtable,code_table,doc_table where doc_table.acct= '2958404_017'
and doc_table.id=code_table.did and code_table.cid=icdtable.cid and icdtable.status <> 'X'

And the version of the query using inner joins:

select doc_table.doctype,doc_table.create_date, icdtable.icdcode,code_table.token, code_table.did, code_table.pos_start,
code_table.pos_end, icdtable.icdid, icdtable.cid, icdtable.status,icdtable.kstatus from doc_table INNER JOIN code_table ON doc_table.id = code_table.did
INNER JOIN icdtable ON code_table.cid =icdtable.cid  where doc_table.acct= '2958404_017' and icdtable.status <> 'X'

The execution time is identical in each case.  So whether I explicitly use inner joins in my statement or implicitly use them, as I did in the original query, the execution time is the same.
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39231030
can you post the execution plan xml? (as a file)

regardless of execution timings - I'd advise use of ANSI syntax for maintainability if nothing else

{+edit sorry fixed spelling}
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 48

Expert Comment

by:PortletPaul
ID: 39231037
perhaps try mixing it up a little too? e.g.
SELECT
      doc_table.doctype
    , doc_table.create_date
    , icdtable.icdcode
    , code_table.token
    , code_table.did
    , code_table.pos_start
    , code_table.pos_end
    , icdtable.icdid
    , icdtable.cid
    , icdtable.STATUS
    , icdtable.kstatus
FROM code_table
INNER JOIN doc_table ON code_table.did = doc_table.id
    AND doc_table.acct = '2958404_017'
INNER JOIN icdtable ON code_table.cid = icdtable.cid
    AND icdtable.STATUS <> 'X'

Open in new window

0
 

Author Comment

by:efamilant
ID: 39231573
As requested.
executionplan.txt
0
 

Author Comment

by:efamilant
ID: 39231578
If I use this version of the query, with the hint as part of it, the query executes instantly.

select doc_table.doctype,doc_table.create_date, icdtable.icdcode,code_table.token, code_table.did, code_table.pos_start,
code_table.pos_end, icdtable.icdid, icdtable.cid, icdtable.status,icdtable.kstatus from icdtable WITH (INDEX=IX_icdtable_cid),code_table,doc_table where doc_table.acct= '999999999'
and doc_table.id=code_table.did and code_table.cid=icdtable.cid and icdtable.status <> 'X'

But my question is, is there a way to do this without using hints.
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 39232395
how current are your statistics? perhaps these should be updated?

the most costly operation in that execution plan is a clustered index seek in the doc_table searching for the '2058404_017' (node 4), and index IX_icdtable_cid is already being used (node 5).
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39233603
As indicated previously and without looking at your Execution Plan I would be surprised if it was not as simple as some stale statistics.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
The viewer will learn how to implement Singleton Design Pattern in Java.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now