OPTIMIZE FOR query hint in SQL Server

Hi Geeks,

Many of us might have experienced that a query runs perfect for long time then suddenly it started performing very bad with no clues. After lot of investigation, we find that parameter that are supplied by application to run the respective SQL query performs well but sometime because of certain value the plan gets recompiled and same plan is stored in cache resulting poor query performance.

The OPTIMIZE FOR query hint comes into rescue since SQL Server 2005 which allows us to instruct query optimizer to optimize a query execution for the parameter we specify. This can be very useful at different times but may result in bad plan as well if not checked properly. I’ll now demonstrate this with the help of a simple query.

USE [AdventureWorks2012]

--Query 1
SELECT *
FROM Person.Address
WHERE Address.City = 'Bothell'

--Query 2
SELECT *
FROM Person.Address
WHERE Address.City = 'Paris'

QueryPlan_1

From above figure, the second query is slightly expensive than first query as it uses Clustered Index Scan which read through all rows. Let me now run the same set of statements using parameters.

USE [AdventureWorks2012]

DECLARE @City VARCHAR(20)

SET @City = 'Bothell'
SELECT *
FROM Person.Address
WHERE Address.City = @City

SET @City = 'Paris'
SELECT *
FROM Person.Address
WHERE Address.City = @City

QueryPlan_2

Above plan uses Clustered Index Scan for both the queries as it’s not sure about the values till the run time. Now, let me make some modification and include OPTIMIZE FOR query hint for the second query.

USE [AdventureWorks2012]

DECLARE @City VARCHAR(20)

SET @City = 'Paris'
SELECT *
FROM Person.Address
WHERE Address.City = @City
OPTION (OPTIMIZE FOR (@CITY = 'Bothell'))

QueryPlan_3

Paris have very low selectivity as there are many values (around 398) equals to that within the index. The OPTIMIZE FOR hint was able to instruct query optimizer to create plan as same as the one used for Bothell and this helped to improve query performance in our example.

This is an illustration and improper use of any query hints may cause negative impact.

You can find index to the execution plan series here and click on One operator a day to visit exclusive page for this series.

Happy Learning!

Regards,

Kanchan

Like us on FaceBook | Join the fastest growing SQL Server group on FaceBook | Follow me on Twitter | Follow me on FaceBook

Avatar

About Kanchan Bhattacharyya

Kanchan is an astute IT professional, a seasoned SQL Database Administrator with 13+ years of industry experience. A calculated risk taker with deep technical knowledge and has gained proficiency in database consulting and solutions across different environments. Kanchan holds MBA degree in IT and an International Executive MBA in Project Management. He is very passionate about SQL Server and holds MCSE (Data Platform), MCSA – SQL 2012, MCITP – SQL 2008 certifications. Currently he is focusing on cloud and latest releases of SQL Server. When not working, Kanchan likes to spend his time reading on new technical developments specifically on SQL Server and other related technologies.

View all posts by Kanchan Bhattacharyya →

Leave a Reply

Your email address will not be published. Required fields are marked *