【发布时间】:2014-09-11 21:05:11
【问题描述】:
我目前在使用 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();
【问题讨论】:
-
您确定要获取
Values吗?在发布的代码中看起来不是这样。 -
是的,它正在热切地工作。 HasMany(x => x.Values) .KeyColumn("Entity") .Cascade.AllDeleteOrphan() .Fetch.Select() .BatchSize(1000);
-
另外,当我在 ".Select(..." 之前添加 Fetch(x=>x.Values) 时,大约需要 20 秒。
-
一个建议是将您的
Values类型从List更改为IEnumerable并删除ToList()调用。这将加快您的转换速度。 -
谢谢,我会试试的。是否可以在 NH 的 QueryOver 或 Criteria API 中编写相同的查询?
标签: performance linq nhibernate dto