流畅的NHibernate N + 1问题与复杂的对象

时间:2016-09-01 05:01:02

标签: nhibernate fluent-nhibernate nhibernate-mapping

我遇到NHibernate查询数据库的问题太多次了。我刚刚意识到这可能与n + 1问题有关,但我无法弄清楚如何改变我的映射来解决问题。

正如您将看到的,我的尝试涉及指定不延迟加载其他对象,但它似乎没有做到这一点。

这是查询:

public IQueryable<Report> ReadAll(DateTime since)
{
    return m_session.QueryOver<Report>()
       .JoinQueryOver(r => r.Mail)
       .Where(m => m.Received >= since)
       .List()
       .AsQueryable();
}

提前感谢您的回复!如果您需要有关我的对象或映射的更多信息,请告诉我。

简化对象图(省略一些):

public class Report : EntityBase
{
    public virtual Product Product { get; set; }
    public virtual StackTrace StackTrace { get; set; }
    public virtual Mail Mail { get; set; }

    public virtual IList<ClientUser> ReadBy { get; set; }
}

-

public class Product : EntityBase
{
    public virtual string Name { get; set; }
    public virtual Version Version { get; set; }
    public virtual IList<Report> Reports { get; set; }
    public virtual IList<StackTrace> StackTraces { get; set; }
}

-

public class StackTrace : EntityBase
{
    public virtual IList<StackTraceEntry> Entries { get; set; }
    public virtual IList<Report> Reports { get; set; }
    public virtual Product Product { get; set; }
}

映射示例:

public class ReportMap : ClassMap<Report>
{
    public ReportMap()
    {
        Table("Report");

        References(x => x.User)
          .Column("EndUserId")
          .Not.LazyLoad();

        References(x => x.Product)
          .Column("ProductId")
          .Not.LazyLoad();

        References(x => x.StackTrace)
          .Column("StackTraceId")
          .Not.LazyLoad();

        HasManyToMany(x => x.ReadBy)
          .Cascade.SaveUpdate()
          .Table("ClientUserRead")
          .ParentKeyColumn("ReportId")
          .ChildKeyColumn("ClientUserId")
          .Not.LazyLoad().BatchSize(200);
    }
}

-

public class StackTraceMap : ClassMap<StackTrace>
{
    public StackTraceMap()
    {
        Table("StackTrace");

        References(x => x.Product)
          .Column("ProductId");

        HasMany(x => x.Entries)
          .KeyColumn("StackTraceId")
          .Not.LazyLoad()
          .Cascade
          .All().BatchSize(500);

        HasMany(x => x.Reports)
          .KeyColumn("StackTraceId")
          .Inverse().BatchSize(100);
    }
}

2 个答案:

答案 0 :(得分:6)

要走的路是使用batch fetching。在这里阅读更多相关信息:

How to Eager Load Associations without duplication in NHibernate?

在每个实体映射上应用 BatchSize (对于many-to-one关系 - 避免1 + N)

public ReportMap()
{
   Table(...)
   BatchSize(25);
   ...

并且每个集合(解决one-to-many 1 + N的问题)

HasMany(x => x.Reports)
      .KeyColumn("StackTraceId")
      .BatchSize(25)
      ...

答案 1 :(得分:4)

您可以在查询中指定提取路径。例如,此提取路径可以告诉查询急切地加入产品和主要对象,以及客户端集合上的虚构关联:

public IQueryable<Report> ReadAll(DateTime since)
{
    return m_session.QueryOver<Report>()
       .JoinQueryOver(r => r.Mail)
       .Where(m => m.Received >= since)
       .Fetch(x => x.Product).Eager
       .Fetch(x => x.Mail).Eager
       .Fetch(x => x.ReadBy.First().SomeProp).Eager
       .List()
       .AsQueryable();
}

如果您希望始终如此,请尝试使用.Fetch.Join()代替.Not.LazyLoad()进行关联。但我不建议这样做,因为它可能导致简单的查询变得巨大。批处理或子查询也可以提供帮助。

相关问题