简体   繁体   中英

Performance Issue with NHibernate Query

I am currently having a performance problem with the following query written in NHibernate. I am trying to transform the data I queried into DTO's. With this complex structure I cannot use QueryOver to transform the entities. On the other side Linq provider is so useful but it takes ~10 seconds to load and transform ~6000 entities with each 30 child items. It creates an SQL query with left outer join. Are there any other ways to write this query with a better approach?

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();

Here is my solution. if there is any other better way, I am completely open to it:

  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>();

And this is my custom result transformer to get the same hierarchy like my linq query

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;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM