sql server - How to pass sql function parameter by value -


this part of bigger selection, have stripped down essential question :

compare 2 sql queries - first works constant, second variable, both have same value (lets 180). 1 constant displays result (e.g. within milliseconds), 1 variable takes few seconds yield same result.

where catch ?

query 1:

select * table field > 180 

query 2:

declare @v int set @v = 180 select * table field > @v 

the catch lies in parameter sniffing. article mentioned:

"parameter sniffing occurs when parameterized query uses cached cardinality estimates make query plan decisions. problem occurs when first execution has atypical parameter values. each subsequent execution optimizer going assume estimates though estimates may way off. example, have stored procedure returns id values between 1 , 1000. if stored procedure executed large range of parameter values, optimizer going cache these atypical values, indirectly causes optimizer under estimate cardinality. problem typical execution may return few rows. “sniffing” can cause queries scan table oppose seek because optimizer assuming inaccurate cardinality estimates."


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -