I have recently encountered performance problem with one of my production servers due to parameter sniffing in SQL Server 2008. It actually took us quite a bit time to find out the problem and I thought it would be really useful to share it. So what is parameter sniffing?
The simplest of all the solutions is to switch of the Parameter Sniffing using the trace flag 4136. The DBCC syntax is given below
We can force the SQL optimizer to temporarily suspend the Parameter Sniffing by using local variables substitution for the parameters and use only the local variables in the query as shown below
CREATE PROCEDURE pGetPatientName
(
@P_PatientID VARCHAR(50),
@P_Name VARCHAR(50)
)
AS
BEGIN
SurName
FROM
Patient
WHERE PatientID = @V_PatientID
AND ForeName LIKE @V_Name+'%'
END
“Parameter sniffing is the process whereby SQL Server creates an optimal plan for a stored procedure by using the calling parameters that are passed the first time a stored procedure is executed”. In Other words the plan is never regenerated / optimized after the first execution which forces the optimizer to use the same plan irrespective of the parameters.
Though there are many ways to identify the parameter sniffing the simplest behavior is there is a considerable difference in the query execution times when executed using SQL Query Analyzer and actual stored procedure call from the application.I have given some of the commonly used solutions to solve the “Parameter sniffing”
Trace FlagThe simplest of all the solutions is to switch of the Parameter Sniffing using the trace flag 4136. The DBCC syntax is given below
DBCC TRACEON (4136)
Local Variable Substitution We can force the SQL optimizer to temporarily suspend the Parameter Sniffing by using local variables substitution for the parameters and use only the local variables in the query as shown below
CREATE PROCEDURE pGetPatientName
(
@P_PatientID VARCHAR(50),
@P_Name VARCHAR(50)
)
AS
BEGIN
DECLARE @V_PatientID VARCHAR(50)
DECLARE @V_Name VARCHAR(50)SET @V_Name = @P_Name
SET @V_PatientID = @P_PatientIDSELECT
ForeName,SurName
FROM
Patient
WHERE PatientID = @V_PatientID
AND ForeName LIKE @V_Name+'%'
END
Recompile & Query Hints
You can also make the optimizer to force for the plan compilation using the options such as
1. Execute the query “With Recompile” option2. Alter the Procedure / Drop & Re Create Indexes
3. Specifying Query Hints
12 comments:
I could not refrain from commenting. Exceptionally well written!
My web site: http://www.daily-wet-tshirt.com/erika-kurisu-horny-asian-babe-gets-a-rear-fucking/
Hello, I do think your site could be having browser compatibility issues.
Whenever I take a look at your blog in Safari, it looks fine but when opening in Internet Explorer, it's got some overlapping issues. I just wanted to provide you with a quick heads up! Besides that, excellent blog!
My homepage ... www.teenpornpost.com
Wonderful, what a blog it is! This weblog provides valuable data
to us, keep it up.
Also visit my web-site ; naked girls
It is not my first time to pay a visit this website,
i am browsing this web site dailly and obtain good data from here all the time.
Here is my homepage :: naked girls
What's up to every one, the contents existing at this web site are in fact amazing for people experience, well, keep up the good work fellows.
Review my website ; Free Teen Porn
Very good post. I am dealing with some of these issues as
well..
Review my webpage ... Videos
Hey! Do you use Twitter? I'd like to follow you if that would be okay. I'm definitely enjoying your blog and look forward to
new updates.
My website ... porn
That is a very good tip especially to those fresh to the
blogosphere. Simple but very precise info… Thanks for sharing this one.
A must read article!
My webpage - Orlando contractors
louis vuitton outlet online, kate spade handbags, ray ban sunglasses, kate spade outlet online, prada outlet, michael kors outlet, coach outlet store online, nike shoes, nike air max, jordan shoes, nike free, polo ralph lauren, christian louboutin shoes, longchamp outlet, michael kors outlet online, longchamp handbags, gucci handbags, longchamp outlet online, polo ralph lauren outlet, michael kors outlet store, michael kors outlet online, oakley vault, red bottom shoes, prada handbags, tory burch outlet, cheap oakley sunglasses, michael kors outlet online, burberry outlet online, chanel handbags, oakley sunglasses, burberry outlet online, coach purses, louis vuitton outlet, christian louboutin outlet, tiffany jewelry, coach outlet, nike air max, tiffany and co jewelry, ray ban outlet, louboutin shoes, louis vuitton handbags, michael kors outlet online, louis vuitton, coach outlet, louis vuitton outlet
ninest123 16.03
ray ban sunglasses, cheap oakley sunglasses, replica watches, michael kors outlet, oakley sunglasses, jordan shoes, prada handbags, nike outlet, nike air max, nike free, louis vuitton, tiffany and co, ugg boots, louboutin outlet, ray ban sunglasses, louis vuitton outlet, burberry outlet online, michael kors outlet, chanel handbags, uggs on sale, tiffany jewelry, louis vuitton outlet, michael kors outlet, oakley sunglasses, oakley sunglasses, louboutin shoes, prada outlet, ugg boots, ray ban sunglasses, ugg boots, burberry, replica watches, michael kors outlet, tory burch outlet, longchamp outlet, michael kors outlet, louis vuitton, michael kors, polo ralph lauren outlet, longchamp outlet, louis vuitton, polo ralph lauren outlet, louboutin, oakley sunglasses, gucci outlet, ugg boots, christian louboutin outlet, nike air max, longchamp
north face, hogan, coach outlet, air force, hollister pas cher, michael kors, coach outlet, lululemon, burberry, sac guess, nike roshe, nike free, true religion jeans, air jordan pas cher, lacoste pas cher, ralph lauren pas cher, michael kors, kate spade outlet, ralph lauren uk, michael kors, new balance pas cher, true religion jeans, ray ban uk, nike roshe run, converse pas cher, michael kors, hermes, mulberry, vans pas cher, louboutin pas cher, true religion outlet, ray ban pas cher, nike air max, oakley pas cher, air max, timberland, nike free run uk, kate spade handbags, nike air max, hollister, tn pas cher, replica handbags, nike air max, vanessa bruno, abercrombie and fitch, sac longchamp, true religion jeans, nike blazer, coach purses, longchamp pas cher, north face
ugg,uggs,uggs canada, canada goose, ugg,ugg australia,ugg italia, canada goose, hollister, wedding dresses, moncler, louis vuitton, canada goose outlet, swarovski, pandora charms, pandora jewelry, moncler outlet, moncler, sac louis vuitton pas cher, replica watches, louis vuitton, marc jacobs, louis vuitton, vans, moncler, swarovski crystal, pandora jewelry, toms shoes, moncler, juicy couture outlet, doudoune canada goose, karen millen, converse, lancel, canada goose outlet, coach outlet, gucci, ray ban, thomas sabo, moncler, supra shoes, canada goose uk, juicy couture outlet, links of london, barbour jackets, louis vuitton, canada goose, barbour, ugg boots uk, pandora charms, bottes ugg, converse outlet, moncler, montre pas cher, canada goose, ugg pas cher, moncler
ninest123 16.03
Post a Comment