【问题标题】:NHibernate query performance with a large number of disjunctions and conjunctions具有大量析取和连接的 NHibernate 查询性能
【发布时间】:2015-05-27 16:16:56
【问题描述】:

我正在尝试使用析取和连词对来自 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 问题。如果 NHibernate 不存在,您将如何调整查询?
  • 正如 Andrew 提到的,这更多地与如何优化查询有关,而不是与 NHibernate 相关,如果您有很多参数的非常大的查询,您可能应该使用 SP 或视图处理这种情况。

标签: c# sql hibernate nhibernate


【解决方案1】:

正如 cmets 中所指出的,这更像是一个 SQL 查询问题,而不是 NHibernate 问题。我通过改进完整查询的逻辑来解决这个问题,以尽量减少产生的析取和连词的数量。

【讨论】:

    猜你喜欢
    • 2021-08-19
    • 2011-12-29
    • 2013-07-28
    • 2018-01-13
    • 1970-01-01
    • 2011-12-16
    • 2015-08-28
    • 2018-09-29
    • 1970-01-01
    相关资源
    最近更新 更多