sql server optimize for

Posted on 2011-03-14
Last Modified: 2012-05-11
hi experts, i am reading about optimize for

but i do not understand very well
can you tell me which scenario is necessary to use a query hint
Question by:enrique_aeo
LVL 39

Expert Comment

ID: 35128536
When you want the query plan to be optimized for a specific value.
LVL 28

Accepted Solution

Ryan McCauley earned 500 total points
ID: 35128780
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.

Author Comment

ID: 35131687

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SHOWPLAN permission denied in database 'AdventureWorks'. 13 111
kill process lock Sql server 9 64
SSRS 2013 - Creating a summarized report 19 49
Text file into sql server 5 32
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In a recent question ( here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 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