我有一个带有 Customer 表的数据库。这些客户中的每一个都有一个安装表的外键,该表还有一个地址表的外键(为简单起见重命名表)。
在NHibernate中,我尝试像这样查询 Customer 表:
ISession session = tx.Session;
var customers = session.QueryOver<Customer>().Where(x => x.Country == country);
var installations = customers.JoinQueryOver(x => x.Installation, JoinType.LeftOuterJoin);
var addresses = installations.JoinQueryOver(x => x.Address, JoinType.LeftOuterJoin);
if (installationType != null)
{
installations.Where(x => x.Type == installationType);
}
return customers.TransformUsing(new DistinctRootEntityResultTransformer()).List<Customer>();
这导致类似于(由NHibernate Profiler捕获)的 SQL 查询:
SELECT *
FROM Customer this_
left outer join Installation installati1_
on this_.InstallationId = installati1_.Id
left outer join Address address2_
on installati1_.AddressId = address2_.Id
WHERE this_.CountryId = 4
and installati1_.TypeId = 1
当我在Microsoft SQL Server Management Studio中执行上述 SQL 查询时,它会在大约 5 秒内执行,但会返回 ~200.000 条记录。然而,在运行代码时检索List需要花费大量时间。我已经等了10分钟没有任何结果。调试日志表明,由于对象层次结构,构造和启动了许多对象。有没有办法解决这个性能问题?