NHibernate 查询性能与大量的分离和连接

NHibernate query performance with a large number of disjunctions and conjunctions

我正在尝试使用析取和连词对来自 NHibernate 会话的数据应用过滤器。我已按如下方式实施:

var disjunction = new Disjunction();
foreach (var entry in filterCriteria.SelectedCriteria)
{
     var conjunction = Restrictions.Conjunction();
     conjunction.Add(Restrictions.Eq("SourceAccount", entry.SourceAccount));
     conjunction.Add(Restrictions.Eq("SourceItemId", entry.SourceItemId));
     conjunction.Add(Restrictions.Eq("SourceProgram", entry.SourceProgram));
     disjunction.Add(conjunction);
}
criteria.Add(disjunction);

问题是 SelectedCriteria 集合中有大量组合,导致 SQL 查询具有 lot 的 WHERE (SourceAccount = x1 , SourceItemId = x2, SourceProgram = x3) OR (SourceAccount = y1, SourceAccount = y2, SourceAccount = y3) OR ...等等等等。这意味着查询的性能很糟糕。

我的问题是如何更理智地将大量析取和连接应用于 NHibernate 查询?

正如评论中所指出的,这更像是一个 SQL 查询问题,而不是 NHibernate 问题。我通过改进完整查询的逻辑以最大限度地减少产生的分离和连接的数量来解决它。