I have this query that gets executed though Linq to Entities. First time the query runs it generates the execution plan which takes just under 2 minutes. After the plan is cached the query takes 1 or 2 seconds. The problem I have is that the plan keeps getting rebuild every few hours and I am not sure why that would be?
This is the linq query we are using, I know it looks crazy but for what we need this was our only option.
var data = from row in mgr.ServiceDesk_RequestEvent
.Include("ServiceDesk_Event")
.Include("ServiceDesk_Event.ServiceDesk_SLAEventRule")
.Include("ServiceDesk_Event.ServiceDesk_SLAEventRule.ServiceDesk_RuleSet")
.Include("ServiceDesk_Event.ServiceDesk_SLAEventRule.ServiceDesk_RuleSet.ServiceDesk_Rule")
.Include("ServiceDesk_Event.ServiceDesk_SLAEventRule.ServiceDesk_RuleSet.ServiceDesk_Rule.ServiceDesk_RuleOperator")
.Include("ServiceDesk_Event.ServiceDesk_SLAEventRule.ServiceDesk_RuleSet.ServiceDesk_Rule.ServiceDesk_RuleConstraintField")
.Include("ServiceDesk_Event.ServiceDesk_SLAEventRule.ServiceDesk_RuleSet.ServiceDesk_Rule.ServiceDesk_RuleConstraintValue")
.Include("ServiceDesk_Event.ServiceDesk_SLAEventRule.ServiceDesk_RuleSet.ServiceDesk_Action")
.Include("ServiceDesk_Request")
.Include("ServiceDesk_Request.People_User")
.Include("ServiceDesk_Request.ServiceDesk_RequestCategory")
.Include("ServiceDesk_Request.ServiceDesk_RequestCategory.ServiceDesk_SLA")
.Include("ServiceDesk_Request.ServiceDesk_RequestRole_Groups")
.Include("ServiceDesk_Request.ServiceDesk_RequestRole_Groups.Security_Role.Security_UserRoles")
.Include("ServiceDesk_Request.ServiceDesk_RequestRole_Groups.Security_Role.Security_UserRoles.Security_User")
.Include("ServiceDesk_Request.ServiceDesk_RequestPriority")
.Include("ServiceDesk_Request.Offices_User")
.Include("ServiceDesk_Request.ServiceDesk_RequestTechnicians")
.Include("ServiceDesk_Request.ServiceDesk_RequestTechnicians.People")
where row.Completed == false && row.Deleted == false
select row;
I don't want to paste the generated t-sql here since it quite large. If anyone has ideas please feel free to contribute.
Thank You.