Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


defining query to use an existing index


defining query to use an existing index

Author
Message
ganu.mumbai
ganu.mumbai
SSC Journeyman
SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)

Group: General Forum Members
Points: 84 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
pankushmehta
pankushmehta
SSC Veteran
SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)

Group: General Forum Members
Points: 231 Visits: 4106
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.
GilaMonster
GilaMonster
SSC Guru
SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)

Group: General Forum Members
Points: 72082 Visits: 44958
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

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


Evil Kraig F
Evil Kraig F
SSCertifiable
SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)

Group: General Forum Members
Points: 7803 Visits: 7660
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
Grant Fritchey
Grant Fritchey
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27866 Visits: 32482
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 and SQL Server Execution Plans
Product Evangelist for Red Gate Software
alen teplitsky
alen teplitsky
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2258 Visits: 4654
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.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search