Solved

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

Posted on 2013-06-07
9
446 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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
 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

739 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