Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 2005
»
SQL Server 2005 General Discussion
»
defining query to use an existing index
defining query to use an existing index
Rate Topic
Display Mode
Topic Options
Author
Message
ganu.mumbai
ganu.mumbai
Posted Sunday, February 13, 2011 10:15 PM
Valued 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
pankushmehta
pankushmehta
Posted Sunday, February 13, 2011 10:36 PM
SSC 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
GilaMonster
GilaMonster
Posted Sunday, February 13, 2011 10:42 PM
SSC-Dedicated
Group: General Forum Members
Last Login: Today @ 3:01 PM
Points: 37,669,
Visits: 29,923
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
Evil Kraig F
Evil Kraig F
Posted Sunday, February 13, 2011 10:50 PM
SSCertifiable
Group: General Forum Members
Last Login: 2 days ago @ 10:09 PM
Points: 5,658,
Visits: 6,100
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
Grant Fritchey
Grant Fritchey
Posted Monday, February 14, 2011 5:18 AM
SSChampion
Group: General Forum Members
Last Login: Today @ 3:57 PM
Points: 13,371,
Visits: 25,152
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 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans
Product Evangelist for
Red Gate Software
Post #1063441
alen teplitsky
alen teplitsky
Posted Monday, February 14, 2011 12:44 PM
Ten Centuries
Group: General Forum Members
Last Login: Today @ 2:11 PM
Points: 1,409,
Visits: 4,506
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 »
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.