Performance NHibernate查询的性能问题

Performance NHibernate查询的性能问题,performance,linq,nhibernate,dto,Performance,Linq,Nhibernate,Dto,我目前在使用NHibernate编写的以下查询中遇到性能问题。我正在尝试将查询的数据转换为DTO。对于这种复杂的结构,我不能使用QueryOver来转换实体。另一方面,Linq提供程序非常有用,但每30个子项加载和转换约6000个实体需要约10秒钟。它创建一个带有左外部联接的SQL查询。有没有其他更好的方法来编写这个查询 var Entities = session.Query<crmEntity>() .Where(x => x.EntityType.ID

我目前在使用NHibernate编写的以下查询中遇到性能问题。我正在尝试将查询的数据转换为DTO。对于这种复杂的结构,我不能使用QueryOver来转换实体。另一方面,Linq提供程序非常有用,但每30个子项加载和转换约6000个实体需要约10秒钟。它创建一个带有左外部联接的SQL查询。有没有其他更好的方法来编写这个查询

var Entities = session.Query<crmEntity>()
         .Where(x => x.EntityType.ID == EntityType)
         .Select(entity => new EntityDTO()
         {
             ID = entity.ID,
             EntityType = entity.EntityType.ID,
             InstanceID = entity.Instance.ID,
             Values = entity.Values.Select(
               value => new CustomFieldValueDTO()
             {
                 ID = value.ID,
                 FieldID = value.Field.ID,
                 Value = value.Value
             }).ToList<CustomFieldValueDTO>()
         }).ToList();
var Entities=session.Query()
.Where(x=>x.EntityType.ID==EntityType)
.Select(entity=>newentitydto()
{
ID=entity.ID,
EntityType=entity.EntityType.ID,
InstanceID=entity.Instance.ID,
Values=entity.Values.Select(
value=>new CustomFieldValueDTO()
{
ID=value.ID,
FieldID=value.Field.ID,
Value=Value.Value
})托利斯先生()
}).ToList();

这是我的解决方案。如果还有其他更好的方法,我完全愿意:

  session.CreateQuery(@"select vals.ID, 
                               vals.Field.ID,
                               vals.Value,
                               ent.ID 
               from crmEntity ent inner join ent.Values vals
               with vals.Value IS NOT NULL
               where ent.EntityType.ID=:eID and ent.Instance.ID=:instanceID order by ent.ID")
  .SetGuid("instanceID", InstanceID)
  .SetGuid("eID", EntityType)
  .SetResultTransformer(new EntityListTransformer()).Future<ReadOnlyEntityDTO>();
session.CreateQuery(@“选择vals.ID,
vals.Field.ID,
VAL.值,
耳鼻喉科
从crmEntity ent内部连接ent.VAL值
具有VAL。值不为空
其中ent.EntityType.ID=:eID和ent.Instance.ID=:instanceID order by ent.ID”)
.SetGuid(“instanceID”,instanceID)
.SetGuid(“eID”,EntityType)
.SetResultTransformer(新实体列表Transformer()).Future();
这是我的自定义结果转换器,用于获得与linq查询相同的层次结构

public class EntityListTransformer : IResultTransformer
    {
        private List<ReadOnlyEntityDTO> list;
        private ReadOnlyEntityDTO lastEntity;
        private Guid instanceID;

        public EntityListTransformer()
        {
            list = new List<ReadOnlyEntityDTO>();
            lastEntity = new ReadOnlyEntityDTO();
        }

        public System.Collections.IList TransformList(System.Collections.IList collection)
        {
            return list;
        }

        public object TransformTuple(object[] tuple, string[] aliases)
        {
            string ValueID = tuple[0].ToString();
            string FieldID = tuple[1].ToString();
            string Value = (string)tuple[2];
            string EntityID = tuple[3].ToString();


            if (lastEntity.ID != EntityID)
            {
                if (lastEntity.ID != null)
                {
                    list.Add(lastEntity);
                }


                lastEntity = new ReadOnlyEntityDTO()
                {
                    ID = EntityID
                };
            }

            lastEntity.Values.Add(new ReadOnlyCustomFieldValueDTO()
            {
                FieldID = FieldID,
                ID = ValueID,
                Value = Value
            });


            return tuple;
        }
    }
公共类EntityListTransformer:IResultTransformer
{
私人名单;
私有只读实体到最后一个实体;
私有Guid实例ID;
公共实体列表转换器()
{
列表=新列表();
lastEntity=new ReadOnlyEntityDTO();
}
public System.Collections.IList转换列表(System.Collections.IList collection)
{
退货清单;
}
公共对象转换元组(对象[]元组,字符串[]别名)
{
字符串ValueID=元组[0]。ToString();
string FieldID=元组[1]。ToString();
字符串值=(字符串)元组[2];
字符串EntityID=元组[3]。ToString();
if(lastEntity.ID!=EntityID)
{
if(lastEntity.ID!=null)
{
列表。添加(最后一个实体);
}
lastEntity=new ReadOnlyEntityDTO()
{
ID=实体ID
};
}
添加(新的ReadOnlyCustomFieldValueDTO())
{
FieldID=FieldID,
ID=ValueID,
价值=价值
});
返回元组;
}
}

您确定要急于获取
值吗?在发布的代码中看起来不是这样的。是的,它正在热切地工作。HasMany(x=>x.Values).KeyColumn(“实体”).Cascade.AllDeleteOrphan().Fetch.Select().BatchSize(1000);另外,当我在“.Select(…”之前添加Fetch(x=>x.Values)时,需要大约20秒。一个建议是将
Values
类型从
List
更改为
IEnumerable
,并删除
ToList()
调用。这将加快您的转换速度。谢谢,我会尝试。是否可以在NH的QueryOver或Criteria API中编写相同的查询?