【问题标题】:further optimizing NHibernate CreateMultiQuery usage for eager fetching进一步优化 NHibernate CreateMultiQuery 使用以进行急切获取
【发布时间】:2010-10-05 16:21:30
【问题描述】:

当我们想通过单次往返急切地加载实体及其子实体时,建议使用 CreateMultiQuery - 返回的列更少,因为我们没有 X*Y*Z*T 而是 X、X*Y、X*Z、X *T。这对我来说效果很好,但它并不是最优的,因为好像 X 是一个有很多列的表,我从数据库中提取了很多数据。示例:

const string query1 = "select f from Feature f where f.Id in (:fids)"; const string query2 = "select f from Feature f left join fetch f.SprintAssignment where f.Id in (:fids)"; const string query3 = "select f from Feature f left join fetch f.Tasks where f.Id in (:fids)"; const string query4 = "select f from Feature f left join fetch f.Tags where f.Id in (:fids)"; const string query5 = "select f from Feature f inner join fetch f.Project where f.Id in (:fids)";

现在,Feature 表相当大(大约 20 个字段,包括 FK 到其他表)。这意味着对于 query2-5,我得到了大量的数据复制(整个特征字段 + 连接表字段)。

显然,我们需要 FeatureId 以便 Identity Map 可以解析所有内容(整个 Feature 对象及其所有子实体),但为什么它会提取其余部分?

有没有办法对其进行优化,使其仅返回 FeatureId + 连接表列?

【问题讨论】:

    标签: nhibernate optimization


    【解决方案1】:

    不,没有

    【讨论】:

      【解决方案2】:

      您必须在查询上使用投影,并根据 id 投影查询连接表。这样你只能得到连接表的列。这里有一个在 vb.net 中使用 Criteria 的小样本

      ' create the first query, and restrict it to ids
      Dim dFilterTask As NHibernate.Criterion.DetachedCriteria = Nothing
      dFilterTask = NHibernate.Criterion.DetachedCriteria.For(GetType(Task))
      ' add your criterias here
      dFilterTask.Add(your criterias here)
      dFilterTask.SetProjection(NHibernate.Criterion.Projections.Id)
      
      ' second query, select where the task id is in your first query
      Dim dFilterTaskJoin As NHibernate.Criterion.DetachedCriteria = Nothing
      dFilterTaskJoin = NHibernate.Criterion.DetachedCriteria.For(GetType(TaskJoinedClass))
      dFilterTaskJoin.Add(NHibernate.Criterion.Subqueries.PropertyIn("TaskId", dFilterTask))
      

      【讨论】:

        猜你喜欢
        • 2011-06-24
        • 2010-12-26
        • 1970-01-01
        • 1970-01-01
        • 2012-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多