我在 nhibernate 中有这个查询
var fdata = (from p in _session.Query<WfTask>()
join d in _session.Query<WfTaskDetail>() on p.WfTaskDetail.Id equals d.Id
orderby p.ActionDate descending //(order)
where
(_session.Query<WfTask>().Any(x => x.Actor.Id == personid && x.Action != null
&& x.Action.Id == actionDic[statusId] && p.Id != x.Id && p.WfTaskDetail.Id==x.WfTaskDetail.Id && p.StepNo>=x.StepNo-1) || (p.Actor.Id == personid && p.Status == statusId && p.StepNo==1))
&& p.Owner.Id == personid && (actorId == new Guid() || p.Actor.Id == actorId)
select new DataList
{
WfTask = p,
Name = userService.GetFullName(p.Actor.Id),
PositionName = org.GetOrganizatinOfPerson(p.Actor.Id).Name,// p.PositionName
//Statusid = new Guid(),//grp.Where(x => x.WfTask.StepNo == maxStepNo && x.WfTask.DocId == p.WfTask.DocId).Select(x => x.WfTask.Status).FirstOrDefault(),
Actionid = actionDic[statusId]
});
total = fdata.Count();
var dataa = pageNumber > 0 ? fdata.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList() : fdata.Take(pageSize).ToList();
问题是执行查询的时间非常长。我怎么解决这个问题?
模型和映射:
public class WfTask
{
public virtual Guid Id { get; set; }
public virtual Person Owner { get; set; }
public virtual Person Actor { get; set; }
public virtual Guid DocId { get; set; }
public virtual int StepNo { get; set; }
public virtual Guid WfInstanceId { get; set; }
public virtual Guid OwnerGroupId { get; set; }
public virtual Guid Status { get; set; }
public virtual Guid WfActionId { get; set; }
public virtual Guid WfStateId { get; set; }
public virtual Guid OwnerId { get; set; }
public virtual Guid ActorId { get; set; }
public virtual Guid WfTaskDetailId { get; set; }
public virtual string Comment { get; set; }
public virtual DateTime ActionDate { get; set; }
public virtual WfAction Action { get; set; }
public virtual WfState WfState { get; set; }
public virtual WfTaskDetail WfTaskDetail { get; set; }
public virtual bool ViewState { get; set; }
public virtual bool Deleted { get; set; }
}
public WfTaskMap()
{
Table("WfTask");
Id(m => m.Id).Column("Id");
Map(m => m.WfInstanceId);
Map(m => m.OwnerGroupId);
Map(m => m.Status);
Map(m => m.DocId);
Map(m => m.StepNo);
Map(m => m.Comment).Length(500);
Map(m => m.ActionDate);
References(x => x.Action).Column("WfActionId").Cascade.All();
References(x => x.WfState).Column("WfStateId").Cascade.All();
References(x => x.WfTaskDetail).Column("WfTaskDetailId").Cascade.All();
References(x => x.Owner).Column("OwnerId").ForeignKey("FK_Person_WfTask_OwnerId").Cascade.All();
References(x => x.Actor).Column("ActorId").ForeignKey("FK_Person_WfTask_ActorId").Cascade.All();
Map(m => m.ViewState);
Map(m => m.Deleted);
}
public class WfTaskDetail:IModel
{
public virtual Guid Id { get; set; }
public virtual Guid DocId { get; set; }
public virtual string Description { get; set; }
public virtual string Subject { get; set; }
public virtual string Type { get; set; }
public virtual Guid OrgVersionId { get; set; }
public virtual Guid WfVersionId { get; set; }
public virtual Guid DocTypeId { get; set; }
public virtual DateTime CreatedDate { get; set; }
public virtual WfVersion WfVersion { get; set; }
public virtual WfDocType DocType { get; set; }
}
public WfTaskDetailMap()
{
Table("WfTaskDetail");
Id(m => m.Id).Column("Id");
Map(m => m.DocId);
Map(m => m.Description).Length(1000);
Map(m => m.Subject).Length(1000);
Map(m => m.Type).Length(1000);
Map(m => m.OrgVersionId);
Map(m => m.CreatedDate);
References(x => x.DocType).Column("DocTypeId").Cascade.All();
References(x => x.WfVersion).Column("WfVersionId").Cascade.All();
}
我在 sql server 中运行这个查询,它执行得很快我还想知道是否有办法在 nhibernate 条件中编写查询。