I have a stored proc that's been running great for months and now suddenly is chosing a really crummy plan everytime even though when I run the query independently it runs instantly as it's using Parallelism and Merge Join operations and comes back in less than a second.
Here's a link to the proc execution plan: https://skydrive.live.com/redir?resid=3B34923A73A7291!3957
Here's a link to the query execution plan: https://skydrive.live.com/redir?resid=3B34923A73A7291!3958
Here's the actual query:
declare @StartDate datetime = '5/1/2013' declare @EndDate datetime = '6/1/2013'; set tran isolation level read uncommitted; with ProviderDealers as ( select distinct wm.[Date], wm.DealerCode from WebsiteMetric wm left join Dealer d on wm.DealerCode = d.DealerCode left join DistrictLookup dl on d.District = dl.District where wm.[Date] >= @StartDate and wm.[Date] < @EndDate and wm.WebsiteMetricTypeId = 2 --and (@Provider is null or wm.WebsiteMetricProviderId = @Provider) --and (@BusinessCenter is null or dl.BusinessCenter = @BusinessCenter) --and (@District is null or dl.District = @District) --and (@DealerCode is null or d.DealerCode = @DealerCode) ), ProviderWebsiteMetricTotals as ( select wm.[Date], [TotalVisits] = Sum(Visits), UniqueVisits = Sum(UniqueVisits), FormSubmissions = Sum(FormSubmissions), PhoneCallLeads = Sum(PhoneCallLeads), UniquePhoneCallLeads = Sum(UniquePhoneLeads), ConversionRate = Avg(ConversionRateByFormSubmission), TimeOnSite = Avg(TimeOnSite), NewInventoryPageViews = Sum(NewInventoryPageViews), UsedInventoryPageViews = Sum(UsedInventoryPageViews) from WebsiteMetric wm join ProviderDealers pd on wm.[Date] = pd.[Date] and wm.DealerCode = pd.DealerCode where wm.[Date] >= @StartDate and wm.[Date] < @EndDate and wm.WebsiteMetricTypeId = 1 group by wm.[Date] ), ProviderMetrics as ( select wm.[Date], wm.WebsiteMetricProviderId, [TotalVisits] = Sum(Visits), UniqueVisits = Sum(wm.UniqueVisits), FormSubmissions = Sum(wm.FormSubmissions), PhoneCallLeads = Sum(wm.PhoneCallLeads), UniquePhoneCallLeads = Sum(wm.UniquePhoneLeads), ConversionRate = Avg(wm.ConversionRateByFormSubmission), TimeOnSite = Avg(wm.TimeOnSite), NewInventoryPageViews = Sum(wm.NewInventoryPageViews), UsedInventoryPageViews = Sum(wm.UsedInventoryPageViews) from WebsiteMetric wm join Dealer d on wm.DealerCode = d.DealerCode join DistrictLookup dl on d.District = dl.District where wm.[Date] >= @StartDate and wm.[Date] < @EndDate and wm.WebsiteMetricTypeId = 2 --and (@Provider is null or wm.WebsiteMetricProviderId = @Provider) --and (@BusinessCenter is null or dl.BusinessCenter = @BusinessCenter) --and (@District is null or dl.District = @District) --and (@DealerCode is null or d.DealerCode = @DealerCode) group by wm.[Date], wm.WebsiteMetricProviderId ) select pm.*, ProviderTotalVisits = a.TotalVisits, ProviderUniqueVisits = a.UniqueVisits, ProviderFormSubmissions = a.FormSubmissions, ProviderPhoneCallLeads = a.PhoneCallLeads from ProviderMetrics pm outer apply ( select pwt.TotalVisits, pwt.UniqueVisits, pwt.FormSubmissions, pwt.PhoneCallLeads from ProviderWebsiteMetricTotals pwt where pm.[Date] = pwt.[Date] ) a
How can I get the proc plan to look like the query plan?
Thanks!
Blades Don't Need Reloading...