Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

defining query to use an existing index Expand / Collapse
Author
Message
Posted Sunday, February 13, 2011 10:15 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Saturday, July 23, 2011 12:40 AM
Points: 68, Visits: 88
Hi Guys,

Yesterday, in an interview, i was asked an question, that suppose i have a query that uses a table which have some existing define indexes. but somehow the query does not uses the existing index and result in slower performance. You are assured that if that query uses that index the performance would improve. Now how can we define the query to use that particular defined index in that schema/table.

--ganu

Post #1063274
Posted Sunday, February 13, 2011 10:36 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Sunday, May 29, 2011 9:01 AM
Points: 211, Visits: 4,106
You can specify table hints to specify use of a particular index.

Select ID
FROM TableA
WITH (INDEX(TableA_NCI_ID))

Check "Table Hints" in BOL.
Post #1063281
Posted Sunday, February 13, 2011 10:42 PM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 4:16 AM
Points: 40,194, Visits: 36,599
I'm sure the interviewer was looking for the previous answer, but in reality the solution would start with evaluating why the query is not using the index that is apparently best. There will be a reason, the query optimiser is very good these days.

A poorly chosen hint will hurt performance terribly.



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1063283
Posted Sunday, February 13, 2011 10:50 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 4:51 PM
Points: 5,401, Visits: 7,513
What Pankush said above is correct.

However, when you do review table hints, you'll always want to throw in a comment about them being done only as a last resort, and only when you're very, very sure about current and expected future data distribution.

Overriding the optimizer should only be done sparingly, with express purpose, and with full knowledge of the consequences. That said, they're incredibly handy, especially when testing different indexes for usefulness and speed gains, as well as isolation controls.



- Craig Farrell

Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

For better assistance in answering your questions | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
Post #1063285
Posted Monday, February 14, 2011 5:18 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 6:53 AM
Points: 13,890, Visits: 28,285
Personally, I'd want to have a chat with the interviewer. Most of the time, and by that I mean 99.several nines, you have something wrong in the query that is preventing index use, or the stats are messed up enough that the index is not looking useful, or the index isn't really all that useful. For example, I've seen people who were getting a clustered index scan and insisted that the nonclustered index should have worked fine, so they put a hint in to force the use of the nonclustered index. Now, performance improved, a little, but only because instead of scanning the cluster, it scanned the index which had fewer pages. In fact they didn't fix performance in any way with the hint, just slightly propped it up.

----------------------------------------------------
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood..." Theodore Roosevelt
The Scary DBA
Author of: SQL Server Query Performance Tuning
SQL Server 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #1063441
Posted Monday, February 14, 2011 12:44 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, October 14, 2014 12:10 PM
Points: 1,414, Visits: 4,541
index hints are evil

after a few years you may want to get rid of the index to improve performance and it will like pulling teeth getting people to change code.


https://plus.google.com/100125998302068852885/posts?hl=en
http://twitter.com/alent1234
x-box live gamertag: i am null
[url=http://live.xbox.com/en-US/MyXbox/Profile[/url]
Post #1063807
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse