Top 10 SQL server Performance tuning tips - 3

Saturday, 5 September 2009

Use of OPTIMIZE FOR

OPTIMIZE FOR a little known but powerful SQL Server feature.


DECLARE @ProdCode NVARCHAR(20)
SET @ProdCode = N’100’
SELECT ProdId,MfnDate
FROM Product
WHERE ProdCode = @ProdCode
OPTION (OPTIMIZE FOR (@ ProdCode = N’100’))

The OPTIMIZE FOR hint will force an index seek when the specific value is provided.

More about OPTIMIZE FOR/OPTIMIZE FOR UNKNOWN here

0 comments: