March 29, 2022 at 7:48 am
Hi All,
Need some direction on query tuning a stored procedure. The stored proc has multiple sql statements inside it and ~500 lines of code which includes MERGE statement.
What's happening is , In Production, this stored procedure is taking more than an hour.
Note : the parameters passed to this stored proc changes.
I asked the dev team to provide some test cases for a GOOD run and BAD run. I have attached those as part of zip file which also contains Actual execution plans for both runs.
The good run case, It took ~ 5 secs.
The bad run case which they claim it taking more than 60 mins , it took around 4 mins when I ran it SSMS query window.
Not sure why there is big difference from 60 mins to 4mins.
I want to understand why it is taking 4mins for bad run.
The app team expectation for this query is 60 secs. Anything more than that, application calls are TIMING OUT.
Now they came back to DB team, to know why query was taking more than an hour. I said, it could be blocking during that time frame.
Could be some parameter sniffing issues. I am not sure. I am not able to tell confirmly. App team wanted to run a trace on prod.
I am thinking about to enabling a trace during that time frame especially when are seeing the issue.
So, Looking for some help on below.
1. Need help to see if there is any difference in the plans based on different parameters.
2. If I had to run a trace for live troubleshooting , what all events I need to select to run the trace?
what columns to select and what filters to keep to minimize the overhead of the trace. I should be able to capture blocking info, stored proc statements , if any parameter sniffing issues,
any missing indexes, all that stuff I should capture.
3. Also, what all questions or data I should be collecting from the user/dev team so that we can narrow down the issue and take it to resolution or workaround?
SQL Server version is Microsoft SQL Server 2017 (RTM-CU23)
Thank you.
Sam
March 29, 2022 at 2:04 pm
First, not trace. Use Extended Events. It puts far, far less of a load on the system. Plus, you're on SQL Server 2017. Every single piece of new functionality is monitored through Extended Events. Use that.
Also, you have a bunch of tuning opportunities throughout. For example, this: AND TERR.GEO_KEY LIKE '%MSP%'. No statistics use or index use because of this. Nothing but table scans. Guaranteed performance issues. I didn't unpack all the queries, but it looks like a lot of repetition and possibly unnecessary data movement (so many scans that end up with one or zero rows). A careful breakdown will likely identify more tuning opportunities.
One clarity issue, the queries are using the old school join syntax that went off the standard more than 30 years ago. What you have will work (as long as it's always inner joins), but it does make for less clear code and will cause issues as soon as you try to use outer joins. I'd strongly recommend changing that.
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
March 29, 2022 at 7:13 pm
If the MERGE statement is just updating or inserting then change it to be an update statement followed by an insert where not exists statement.
March 30, 2022 at 5:11 am
Hi Grant Sir,
Thanks for the pointers and sharing the link of capturing Waits. will check with them replace old join syntax's with ANSI joins.
Couple of questions:
3. Is there a good way to track down or monitor slow running queries within a stored procedure?
Thanks,
Sam
March 30, 2022 at 5:13 am
Hi Jonathan,
Thanks for the input. I would like to know if there is any implications/perf issues of using MERGE statement especially when dealing large sets of data?
March 30, 2022 at 11:54 am
Hi Jonathan,
Thanks for the input. I would like to know if there is any implications/perf issues of using MERGE statement especially when dealing large sets of data?
I've found replacing a MERGE that does update and insert with an update followed by an insert makes it run in about 60% of the time it takes to run with MERGE. This is on large datasets (100 million rows).
March 30, 2022 at 11:56 am
Hi Grant Sir,
Thanks for the pointers and sharing the link of capturing Waits. will check with them replace old join syntax's with ANSI joins.
Couple of questions:
- for conditions like , <col> LIKE '%MSP%', how can we re-write or how to improve performance for such queries?
- Wanted to give a try with OPTION(RECOMPILE). will it help?
3. Is there a good way to track down or monitor slow running queries within a stored procedure?
Thanks,
Sam
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
March 30, 2022 at 11:58 am
vsamantha35 wrote:Hi Jonathan, Thanks for the input. I would like to know if there is any implications/perf issues of using MERGE statement especially when dealing large sets of data?
I've found replacing a MERGE that does update and insert with an update followed by an insert makes it run in about 60% of the time it takes to run with MERGE. This is on large datasets (100 million rows).
Absolute agreement by the way. MERGE is a compromise. Like many compromises, it's not as good as the things it's replacing.
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
March 30, 2022 at 3:27 pm
Doing only a quick glance at the execution plans, this looks like it could be a parameter sniffing issue. You could go all surgical on this but I'm thinking that building a recompile into the proc would solve your issue, especially considering that a recompile would take a whole lot less time than what you're currently suffering. It it doesn't work (but I think it might), it's easy enough to remove from the proc.
As the saying goes, "One good test is worth a thousand expert opinions".
--Jeff Moden
Change is inevitable... Change for the better is not.
March 30, 2022 at 3:35 pm
Many thanks Grant sir for the detailed explanation.
Thanks Jeff and Jonathan for the feedback.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply