February 13, 2011 at 10:15 pm
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
February 13, 2011 at 10:36 pm
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.
February 13, 2011 at 10:42 pm
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, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
February 13, 2011 at 10:50 pm
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.
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[/url] | Forum Netiquette
For index/tuning help, follow these directions.[/url] |Tally Tables[/url]
Twitter: @AnyWayDBA
February 14, 2011 at 5:18 am
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
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
February 14, 2011 at 12:44 pm
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.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply