Solved

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

Posted on 2013-06-07
9
441 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

808 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