Solved

sql server query hint

Posted on 2011-03-14
4
711 Views
Last Modified: 2012-08-14
Hi experts, i am reading about query hints.
I understand
Caution
Because the SQL Server query optimizer typically selects the best execution plan for a query, we recommend that hints be used only as a last resort by experienced developers and database administrators

can you tell me which scenario is necessary to use a query hint?
0
Comment
Question by:enrique_aeo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 22

Expert Comment

by:Om Prakash
ID: 35128438
Please check the following KB article:
http://msdn.microsoft.com/en-us/library/ms181714.aspx
0
 
LVL 39

Assisted Solution

by:BrandonGalderisi
BrandonGalderisi earned 83 total points
ID: 35128486
I have used query hints when I know that one of the tables in my joins will have a very few number of records matching my criteria and I want to force the join type.
0
 
LVL 28

Assisted Solution

by:Ryan McCauley
Ryan McCauley earned 83 total points
ID: 35128749
I typically use query hints not when the query optimizer picks a bad plan (which it seldom does), but when I want the query to do something counter-intuitive. For example, here are some examples recently of when I've used a few different ones:

WITH (READUNCOMMITTED): I have a table that has a lot of activity in it, and I needed to constantly check the size to see if it's over a certain limit (so I can run some other processes). My "SELECT COUNT(*)" was getting held up as it waited for transactions to commit, so it could give me a true, honest size of the table. Hwoever, I didn't care about the exact size - I only cared about the approximate size, so this hint says "Run my query, right now, without waiting for anything to complete - even if that means I'll view some data that's mid transaction and it may not be accurate. Speed is more important to me than perfect accuracy in this case, and I just want something close."

WITH (UPDLOCK): Even though I was only selecting a few rows, I intended to update them a few moments later, and I wanted to block other processes for reading those rows in the meantime. This hint basically says "Even though I'm only viewing these rows right now, treat them as if I'm enrolling them in a transaction and about to change them, so block other processes from viewing these rows until my transactions ends." Since I was doing this inside a stored procedure, it let go of the lock as soon as the SP completed.
WITH (OPTIMIZE FOR @Param=SomeValue): When SQL Server builds the execution plan, it does so blind to the variables you're using - it just builds the plan based on what it knows, which is only your database structure. However, you may be dealing with a column in one of your tables where 98% of the time, it's one value, so SQL decides it needs to do an INDEX SCAN to filter on that column. However, let's say you know you'll be searching for one of the values that make up the other 2% - even if you don't know which one, you can use the "OPTIMIZE FOR" hint. SQL will replace the variable in the query with the value you specify (in order to build a plan), which might cause it to use an INDEX SEEK instead, making the query much quicker. The value you specify is disregarded when you actually run the query - it's just used to build the plan.

Does those examples give some clear reasons why you might use hints? I've seen query hints used in a number of situations where they're not appropriate - mostly in cases where overzealous developers think they're better at building execution plans than the query engine itself (and as a result, a query will ignore an obviously better index because it's been told to use a specific one that's no longer optimal). Use caution when adding hints to your queries, but remember that the query engine only knows the queries you're submitted, not what you actually intend to do with the data. In cases where it might make a different decision if it knew what you were intended to do, query hints give you the ability to give the optimizer that addition information.
0
 
LVL 22

Accepted Solution

by:
8080_Diver earned 84 total points
ID: 35128838
can you tell me which scenario is necessary to use a query hint?

Like most other things in IT, this is one of those areas that reflects the "art" part of programming and has the answer of, "It kind of depends." ;-)  I am tempted to answer this one with, "Whenever it is needed."

Hints can prove to be beneficial when they are appropriate but they can also seriously degrade performance when they are not appropriate.  On top of that, unfortunately, the same hint can be beneficial for one set of parameters and quite the opposite for another set.  So, as BrandonGalderisi intimated, you really need to know your data and, perhaps more importantly, what data the query will be selecting/impacting.  

IMHO, if you are relatively new to the activity, I would be very cautious about specifying query hints.  I would suggest checking the estimated and then the actual query plans for the query both with and without the hint.



0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

749 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