October 23, 2017 at 9:06 pm
Comments posted to this topic are about the item Parameter Sniffing In Action
October 23, 2017 at 10:01 pm
I like short "spackle" articles like this. Good job.
My only suggestion is that people like to run the code you've included in your article. Instead of using some canned database that a lot of people won't download, provide some code to generate the test data. It's pretty easy to quickly build a million row test table nowadays.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 24, 2017 at 1:09 am
Jeff Moden - Monday, October 23, 2017 10:01 PMI like short "spackle" articles like this. Good job.My only suggestion is that people like to run the code you've included in your article. Instead of using some canned database that a lot of people won't download, provide some code to generate the test data. It's pretty easy to quickly build a million row test table nowadays.
Thanks for feedback Jeff. I do agree with your suggestion.
October 24, 2017 at 2:02 am
The problem with Parameter Sniffing occurs where the execution plan is optimized for small data as a result (here SQL can be quite lazy) and you mostly want big data, where a good execution plan is better.
we had an instance of this a few months ago - execution plans were refreshed and the first parameter passed returned a few rows only when normally there are quite a few more rows expected. End result: The SP ran like a three legged dog dragging a Challenger tank when normally it ran like a greyhound. Took a while to track down why and to fix the problem so we wouldn't have this again. However, the recommendation is to let SQL sort out the Execution plan and that includes Parameter Sniffing - only 'fix' the sniffing if it is actually causing a problem. And to know that you need to do regular health checks.
October 24, 2017 at 8:48 am
Hi, how can I prevent this behavior?
Is it "recompile" option the right way?
Thank you in advance.
October 24, 2017 at 11:02 am
carjara - Tuesday, October 24, 2017 8:48 AMHi, how can I prevent this behavior?
Is it "recompile" option the right way?Thank you in advance.
It depends.
First, and as Nisarg mentioned at the end of the article, parameter sniffing is not always a bad thing. In fact, sometimes lack of parameter sniffing is a bad thing. Parameter sniffing is bad when the optimizer is choosing a terrible execution plan due to it.
I have found it's a good thing to always recompile store procedures that do ETL work and run on a schedule. I would not do it, however, on say - an proc that runs hundreds/thousands of times an hour.
All that said, I've always learned a lot about this topic from Grant Fritchey and Gail Shaw. Here's a couple good articles
https://www.scarydba.com/2016/12/12/optimize-hints-parameter-sniffing-turned-off
https://www.sqlinthewild.co.za/index.php/2007/11/27/parameter-sniffing/
This article (with perhaps the most uninspiring title ever) is also good: https://www.brentozar.com/archive/2013/06/optimize-for-unknown-sql-server-parameter-sniffing/
-- Itzik Ben-Gan 2001
October 24, 2017 at 11:26 am
Alan.B - Tuesday, October 24, 2017 11:02 AMcarjara - Tuesday, October 24, 2017 8:48 AMHi, how can I prevent this behavior?
Is it "recompile" option the right way?Thank you in advance.
It depends.
First, and as Nisarg mentioned at the end of the article, parameter sniffing is not always a bad thing. In fact, sometimes lack of parameter sniffing is a bad thing. Parameter sniffing is bad when the optimizer is choosing a terrible execution plan due to it.
I have found it's a good thing to always recompile store procedures that do ETL work and run on a schedule. I would not do it, however, on say - an proc that runs hundreds/thousands of times an hour.
All that said, I've always learned a lot about this topic from Grant Fritchey and Gail Shaw. Here's a couple good articles
https://www.scarydba.com/2016/12/12/optimize-hints-parameter-sniffing-turned-off
https://www.sqlinthewild.co.za/index.php/2007/11/27/parameter-sniffing/This article (with perhaps the most uninspiring title ever) is also good: https://www.brentozar.com/archive/2013/06/optimize-for-unknown-sql-server-parameter-sniffing/
Alan, thank you a lot!
October 25, 2017 at 5:29 pm
"with recompile" is one way to avoid parameter sniffing.
I think another way is using dynamic queries in SPs. This will cause extra plans in plan cache. So make sure "optimize for Ad hoc Workloads" are turned on.
October 25, 2017 at 5:58 pm
Grey Cat - Wednesday, October 25, 2017 5:29 PM"with recompile" is one way to avoid parameter sniffing.
I think another way is using dynamic queries in SPs. This will cause extra plans in plan cache. So make sure "optimize for Ad hoc Workloads" are turned on.
For large batch jobs, RECOMPILEs aren't so bad. Using "optimize for Ad hoc Workloads" actually masks the terrible problem of multiple renditions of the same code being just far enough off to require a separate and sometimes very expensive recompile of code coming from poorly formed embedded SQL, SQL generated from an ORM, and dynamic SQL. It's a real bitch when front end code (regardless of method) "only" takes 100ms to run but takes 2-22 seconds to execute and it's execute thousands of times per hour. Most folks never find that type of problem and just chalk it up to the "server being slow" when it's actually the fault of the code and the recompiles that are taking place.
To wit, "with recompile" should be used very, very sparingly and with great caution and understanding. It is NOT a panacea to avoid parameter sniffing and should only be used with the greatest of care.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 26, 2017 at 4:03 pm
steve.powell1 - Tuesday, October 24, 2017 2:02 AMThe problem with Parameter Sniffing occurs where the execution plan is optimized for small data as a result (here SQL can be quite lazy) and you mostly want big data, where a good execution plan is better.we had an instance of this a few months ago - execution plans were refreshed and the first parameter passed returned a few rows only when normally there are quite a few more rows expected. End result: The SP ran like a three legged dog dragging a Challenger tank when normally it ran like a greyhound. Took a while to track down why and to fix the problem so we wouldn't have this again. However, the recommendation is to let SQL sort out the Execution plan and that includes Parameter Sniffing - only 'fix' the sniffing if it is actually causing a problem. And to know that you need to do regular health checks.
We are talking about extreme situations. First, parameter sniffing is good in most cases (that's why it is implemented). But this makes things worse in some cases. This is why we need to find a way to avoid sniffing. Any way, Kimberly Tripp has very good sessions, articles on this topic.
October 26, 2017 at 9:51 pm
Grey Cat - Thursday, October 26, 2017 4:03 PMsteve.powell1 - Tuesday, October 24, 2017 2:02 AMThe problem with Parameter Sniffing occurs where the execution plan is optimized for small data as a result (here SQL can be quite lazy) and you mostly want big data, where a good execution plan is better.we had an instance of this a few months ago - execution plans were refreshed and the first parameter passed returned a few rows only when normally there are quite a few more rows expected. End result: The SP ran like a three legged dog dragging a Challenger tank when normally it ran like a greyhound. Took a while to track down why and to fix the problem so we wouldn't have this again. However, the recommendation is to let SQL sort out the Execution plan and that includes Parameter Sniffing - only 'fix' the sniffing if it is actually causing a problem. And to know that you need to do regular health checks.
We are talking about extreme situations. First, parameter sniffing is good in most cases (that's why it is implemented). But this makes things worse in some cases. This is why we need to find a way to avoid sniffing. Any way, Kimberly Tripp has very good sessions, articles on this topic.
Got any links to share?
--Jeff Moden
Change is inevitable... Change for the better is not.
October 26, 2017 at 10:48 pm
Jeff Moden - Thursday, October 26, 2017 9:51 PMGrey Cat - Thursday, October 26, 2017 4:03 PMsteve.powell1 - Tuesday, October 24, 2017 2:02 AMThe problem with Parameter Sniffing occurs where the execution plan is optimized for small data as a result (here SQL can be quite lazy) and you mostly want big data, where a good execution plan is better.we had an instance of this a few months ago - execution plans were refreshed and the first parameter passed returned a few rows only when normally there are quite a few more rows expected. End result: The SP ran like a three legged dog dragging a Challenger tank when normally it ran like a greyhound. Took a while to track down why and to fix the problem so we wouldn't have this again. However, the recommendation is to let SQL sort out the Execution plan and that includes Parameter Sniffing - only 'fix' the sniffing if it is actually causing a problem. And to know that you need to do regular health checks.
We are talking about extreme situations. First, parameter sniffing is good in most cases (that's why it is implemented). But this makes things worse in some cases. This is why we need to find a way to avoid sniffing. Any way, Kimberly Tripp has very good sessions, articles on this topic.
Got any links to share?
Building High Performance Stored Procedures
Stored Procedure Optimization with Kimberly Tripp - Alaska SQL User Group
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply