DISABLE_PARAMETER_SNIFFING

Published May 31 2021 09:00 AM 1,088 Views
Microsoft

According to MSDN article, 'DISABLE_PARAMETER_SNIFFING' instructs Query Optimizer to use average data distribution while compiling a query with one or more parameters. This instruction makes the query plan independent on the parameter value that was first used when the query was compiled. This hint name is equivalent to trace flag 4136 or Database Scoped Configuration setting PARAMETER_SNIFFING = OFF.

 

 

Looks like it's a pretty good hint, However, it doesn't means you can resolve all parameter sniffing issue by using this query hint.

 

Actually,  the sentence 'Query Optimizer to use average data distribution while compiling a query with one or more parameters' is not 100% correct. It really depends on what symbol you used in the where clause.

 

'DISABLE_PARAMETER_SNIFFING' is a replacement of variable, and it has same effect as 'OPTIMIZE FOR UNKNOWN' , These three have exactly same effect.  If you are not familiar with selectivity of variable, please review  my post Selectivity and Estimated Row: Variable - Microsoft Tech Community

I'm going to use AdventureWorks 2019 in this post.

--------------------Please run this script---------------

use AdventureWorks2019

go

IF exists(select 1 from sys.tables where name='SalesOrderDetail' and schema_id=schema_id('dbo'))

      drop table SalesOrderDetail

go

select * into SalesOrderDetail from [Sales].[SalesOrderDetail]

go

create statistics iProductID ON SalesOrderDetail(productid) with fullscan

Go

dbcc traceon(3604,2363)

--------------------Please run this script---------------

 

 

 

For example, following three stored procedures return exactly same Estimated rows 456.

 

create proc ptest1

@pid int

as

select * from SalesOrderDetail where productid=@pid option(use hint('DISABLE_PARAMETER_SNIFFING'))

go

create proc ptest2

@pid int

as

select * from SalesOrderDetail where productid=@pid option(recompile,OPTIMIZE FOR UNKNOWN)

Go

create proc ptest3

@pid int

as

declare @pid1 int =@pid

select * from SalesOrderDetail where productid=@pid1

Go

Liwei_0-1623105186885.png

 

 

 

 

 

Trace flag 2363 displays more detail about the selectivity.

Liwei_1-1622085421402.png

 

 

----------------------------------trace flag 2363 output----------------------------------

Begin selectivity computation

Input tree:

  LogOp_Select

      CStCollBaseTable(ID=1, CARD=121317 TBL: Sales.SalesOrderDetail)

      ScaOp_Comp x_cmpEq

          ScaOp_Identifier QCOL: [AdventureWorks2019].[Sales].[SalesOrderDetail].ProductID

          ScaOp_Identifier COL: @pid

Plan for computation:

  CSelCalcHistogramComparison(POINT PREDICATE)

Loaded histogram for column QCOL: [AdventureWorks2019].[Sales].[SalesOrderDetail].ProductID from stats with id 3

Selectivity: 0.0037594

Stats collection generated:

  CStCollFilter(ID=2, CARD=456.079)

      CStCollBaseTable(ID=1, CARD=121317 TBL: Sales.SalesOrderDetail)

End selectivity computation

----------------------------------trace flag 2363 output----------------------------------

 121317*0.0037594=456

 

 

Please review Selectivity and Estimated Row: Variable - Microsoft Tech Community for other inequations.

%3CLINGO-SUB%20id%3D%22lingo-sub-2389975%22%20slang%3D%22en-US%22%3EDISABLE_PARAMETER_SNIFFING%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2389975%22%20slang%3D%22en-US%22%3E%3CP%3EAccording%20to%20MSDN%20article%2C%20'DISABLE_PARAMETER_SNIFFING'%20instructs%20Query%20Optimizer%20to%20use%20average%20data%20distribution%20while%20compiling%20a%20query%20with%20one%20or%20more%20parameters.%20This%20instruction%20makes%20the%20query%20plan%20independent%20on%20the%20parameter%20value%20that%20was%20first%20used%20when%20the%20query%20was%20compiled.%20This%20hint%20name%20is%20equivalent%20to%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Ft-sql%2Fdatabase-console-commands%2Fdbcc-traceon-trace-flags-transact-sql%3Fview%3Dsql-server-ver15%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Etrace%20flag%3C%2FA%3E%26nbsp%3B4136%20or%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Ft-sql%2Fstatements%2Falter-database-scoped-configuration-transact-sql%3Fview%3Dsql-server-ver15%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EDatabase%20Scoped%20Configuration%3C%2FA%3E%26nbsp%3Bsetting%26nbsp%3BPARAMETER_SNIFFING%20%3D%20OFF.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ELooks%20like%20it's%20a%20pretty%20good%20hint%2C%20However%2C%20it%20doesn't%20means%20you%20can%20resolve%20all%20parameter%20sniffing%20issue%20by%20using%20this%20query%20hint.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EActually%2C%26nbsp%3B%20the%20sentence%20'Query%20Optimizer%20to%20use%20%3CSTRONG%3Eaverage%20data%20distribution%3C%2FSTRONG%3E%20while%20compiling%20a%20query%20with%20one%20or%20more%20parameters'%20is%20not%20100%25%20correct.%20It%20really%20depends%20on%20what%20symbol%20you%20used%20in%20the%20where%20clause.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E'DISABLE_PARAMETER_SNIFFING'%20is%20a%20replacement%20of%20variable%2C%20and%20it%20has%20same%20effect%20as%20'OPTIMIZE%20FOR%20UNKNOWN'%20%2C%20These%20three%20have%20exactly%20same%20effect.%20%26nbsp%3BIf%20you%20are%20not%20familiar%20with%20selectivity%20of%20variable%2C%20please%20review%26nbsp%3B%20my%20post%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fsql-server-support%2Fselectivity-and-estimated-row-variable%2Fba-p%2F2389886%22%20target%3D%22_blank%22%3ESelectivity%20and%20Estimated%20Row%3A%20Variable%20-%20Microsoft%20Tech%20Community%3C%2FA%3E%3C%2FP%3E%0A%3CP%3EI'm%20going%20to%20use%20AdventureWorks%202019%20in%20this%20post.%3C%2FP%3E%0A%3CP%3E--------------------Please%20run%20this%20script---------------%3C%2FP%3E%0A%3CP%3Euse%20AdventureWorks2019%3C%2FP%3E%0A%3CP%3Ego%3C%2FP%3E%0A%3CP%3EIF%20exists(select%201%20from%20sys.tables%20where%20name%3D'SalesOrderDetail'%20and%20schema_id%3Dschema_id('dbo'))%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20drop%20table%20SalesOrderDetail%3C%2FP%3E%0A%3CP%3Ego%3C%2FP%3E%0A%3CP%3Eselect%20*%20into%20SalesOrderDetail%20from%20%5BSales%5D.%5BSalesOrderDetail%5D%3C%2FP%3E%0A%3CP%3Ego%3C%2FP%3E%0A%3CP%3Ecreate%20statistics%20iProductID%20ON%20SalesOrderDetail(productid)%20with%20fullscan%3C%2FP%3E%0A%3CP%3EGo%3C%2FP%3E%0A%3CP%3Edbcc%20traceon(3604%2C2363)%3C%2FP%3E%0A%3CP%3E--------------------Please%20run%20this%20script---------------%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20example%2C%20following%20three%20stored%20procedures%20return%20exactly%20same%20Estimated%20rows%20456.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200in%3B%20font-family%3A%20Calibri%3B%20font-size%3A%2011.0pt%3B%22%3E%3CFONT%20color%3D%22%233366FF%22%3Ecreate%20proc%20ptest1%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200in%3B%20font-family%3A%20Calibri%3B%20font-size%3A%2011.0pt%3B%22%3E%3CFONT%20color%3D%22%233366FF%22%3E%40pid%20int%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200in%3B%20font-family%3A%20Calibri%3B%20font-size%3A%2011.0pt%3B%22%3E%3CFONT%20color%3D%22%233366FF%22%3Eas%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200in%3B%20font-family%3A%20Calibri%3B%20font-size%3A%2011.0pt%3B%22%3E%3CFONT%20color%3D%22%233366FF%22%3Eselect%20*%20from%20SalesOrderDetail%20where%20productid%3D%40pid%20option(use%20hint('DISABLE_PARAMETER_SNIFFING'))%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200in%3B%20font-family%3A%20Calibri%3B%20font-size%3A%2011.0pt%3B%22%3E%3CFONT%20color%3D%22%233366FF%22%3Ego%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200in%3B%20font-family%3A%20Calibri%3B%20font-size%3A%2011.0pt%3B%22%3E%3CFONT%20color%3D%22%233366FF%22%3Ecreate%20proc%20ptest2%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200in%3B%20font-family%3A%20Calibri%3B%20font-size%3A%2011.0pt%3B%22%3E%3CFONT%20color%3D%22%233366FF%22%3E%40pid%20int%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200in%3B%20font-family%3A%20Calibri%3B%20font-size%3A%2011.0pt%3B%22%3E%3CFONT%20color%3D%22%233366FF%22%3Eas%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200in%3B%20font-family%3A%20Calibri%3B%20font-size%3A%2011.0pt%3B%22%3E%3CFONT%20color%3D%22%233366FF%22%3Eselect%20*%20from%20SalesOrderDetail%20where%20productid%3D%40pid%20option(recompile%2COPTIMIZE%20FOR%20UNKNOWN)%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200in%3B%20font-family%3A%20Calibri%3B%20font-size%3A%2011.0pt%3B%22%3E%3CFONT%20color%3D%22%233366FF%22%3EGo%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200in%3B%20font-family%3A%20Calibri%3B%20font-size%3A%2011.0pt%3B%22%3E%3CFONT%20color%3D%22%233366FF%22%3Ecreate%20proc%20ptest3%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200in%3B%20font-family%3A%20Calibri%3B%20font-size%3A%2011.0pt%3B%22%3E%3CFONT%20color%3D%22%233366FF%22%3E%40pid%20int%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200in%3B%20font-family%3A%20Calibri%3B%20font-size%3A%2011.0pt%3B%22%3E%3CFONT%20color%3D%22%233366FF%22%3Eas%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200in%3B%20font-family%3A%20Calibri%3B%20font-size%3A%2011.0pt%3B%22%3E%3CFONT%20color%3D%22%233366FF%22%3Edeclare%20%40pid1%20int%20%3D%40pid%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200in%3B%20font-family%3A%20Calibri%3B%20font-size%3A%2011.0pt%3B%22%3E%3CFONT%20color%3D%22%233366FF%22%3Eselect%20*%20from%20SalesOrderDetail%20where%20productid%3D%40pid1%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200in%3B%20font-family%3A%20Calibri%3B%20font-size%3A%2011.0pt%3B%22%3E%3CFONT%20color%3D%22%233366FF%22%3EGo%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Liwei_0-1623105186885.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F287028iDF867346488777DC%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Liwei_0-1623105186885.png%22%20alt%3D%22Liwei_0-1623105186885.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETrace%20flag%202363%20displays%20more%20detail%20about%20the%20selectivity.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Liwei_1-1622085421402.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F284203i7112CA272ECA42AB%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Liwei_1-1622085421402.png%22%20alt%3D%22Liwei_1-1622085421402.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E----------------------------------trace%20flag%202363%20output----------------------------------%3C%2FP%3E%0A%3CP%3EBegin%20selectivity%20computation%3C%2FP%3E%0A%3CP%3EInput%20tree%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20LogOp_Select%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20CStCollBaseTable(ID%3D1%2C%20CARD%3D121317%20TBL%3A%20Sales.SalesOrderDetail)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20ScaOp_Comp%26nbsp%3B%3CSTRONG%3Ex_cmpEq%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20ScaOp_Identifier%20QCOL%3A%20%5BAdventureWorks2019%5D.%5BSales%5D.%5BSalesOrderDetail%5D.ProductID%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20ScaOp_Identifier%20COL%3A%20%40pid%3C%2FP%3E%0A%3CP%3EPlan%20for%20computation%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20CSelCalcHistogramComparison(POINT%20PREDICATE)%3C%2FP%3E%0A%3CP%3ELoaded%20histogram%20for%20column%20QCOL%3A%20%5BAdventureWorks2019%5D.%5BSales%5D.%5BSalesOrderDetail%5D.ProductID%20from%20stats%20with%20id%203%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3ESelectivity%3A%200.0037594%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EStats%20collection%20generated%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20CStCollFilter(ID%3D2%2C%20CARD%3D%3CSTRONG%3E456.079%3C%2FSTRONG%3E)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20CStCollBaseTable(ID%3D1%2C%20CARD%3D121317%20TBL%3A%20Sales.SalesOrderDetail)%3C%2FP%3E%0A%3CP%3EEnd%20selectivity%20computation%3C%2FP%3E%0A%3CP%3E----------------------------------trace%20flag%202363%20output----------------------------------%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%26nbsp%3B121317*0.0037594%3D456%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CEM%3EPlease%20review%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fsql-server-support%2Fselectivity-and-estimated-row-variable%2Fba-p%2F2389886%22%20target%3D%22_blank%22%3ESelectivity%20and%20Estimated%20Row%3A%20Variable%20-%20Microsoft%20Tech%20Community%3C%2FA%3E%20%3C%2FEM%3E%3CEM%3Efor%20other%20inequations.%3C%2FEM%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2389975%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPerformance%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Co-Authors
Version history
Last update:
‎Jun 07 2021 03:33 PM
Updated by: