I was on call again last week and Sunday night I used a SQL Profile to fix a performance problem. We rolled out some updates Saturday night and the modified query ran fine in some cases, but others ran for hours. When I got on there were 60 sessions running the bad plan and the load on the system was very high. It took me a while to identify the good plan, but then I found it, put in a SQL Profile, killed the 60 bad sessions, and the problem jobs ran in 2 minutes each. A similar situation came up Monday morning after I went off on call and a coworker took over and he ended up applying another SQL Profile on a similar query.
I spent the past couple of hours doing my typical SQL tuning exercise to see if I could figure out why Sunday’s query sometimes chose the bad plan.
The typical scenario includes these elements:
- Partitioned table with some near empty partitions and a lot of full partitions
- Bind variables used to determine partition choice
In our case we have certain tables that partition data by a code number. Certain numbers were used in the past or for other reasons are not active now. My guess is that the bad plans that we see come from bind variable values that point to the empty partitions. The bad plan works fine with empty partitions but then the optimizer uses it on full partitions and the query spins for hours.
I started to research this further to see if I could come up with a better fix than putting in SQL Profiles but did not get very far. I thought I would just pass this post along as is.
P.S. I originally wrote this June 4th, but decided to just publish as is today.
Have used similar approach many times to get the plan(s) that work well but the good and bad of SQL Profiles is they don’t promise a specific plan. I have at times put in a SQL Profile and then let it cook for a bit and find the plan(s) that work well and put them into a SQL_PLAN_BASELINE to keep the plan from straying off the good path.
Thank you for your comment Rich. I like hearing how other people are handling the same type of situation.