我想做一个子查询,然后将其结果内部连接以生成一个查询。我想这样做,因为我已经测试了一个内部连接查询,与直接的子查询相比,它在MySql上的性能似乎要高得多。
下面是一个非常基本的例子,说明了我要复制的sql类型。
桌子
项目
项目ID
姓名
项目关系
项目ID
关系
我要创建的示例Sql
请给出名为“bob”的项的关系计数:

select ir.itemId, count(ir.relationId)
  from ItemRelations ir
    inner join (select itemId from Items where name = 'bob') sq
    on ir.itemId = sq.itemId
  group by ir.itemId

碱型纤维素栎
var bobItems = QueryOver.Of<Item>(() => itemAlias)
  .Where(() => itemAlias.Name == "bob")
  .Select(Projections.Id());

var bobRelationCount = session.QueryOver<ItemRelation>(() => itemRelationAlias)
   .Inner.Join(/* Somehow join the detached criteria here on the itemId */)
   .SelectList(
      list =>
        list.SelectGroup(() => itemRelationAlias.ItemId)
          .WithAlias(() => itemRelationCountAlias.ItemId)
        .SelectCount(() => itemRelationAlias.ItemRelationId)
          .WithAlias(() => itemRelationCountAlias.Count))
   .TransformUsing(Transformers.AliasToBean<ItemRelationCount>())
   .List<ItemRelationCount>();

我知道可以将其重构为一个查询,但是上面只是一个简单的例子。我无法更改分离的QueryOver,因为它被传递到我的代码位,并在系统的其他部分使用。
有人知道是否可以在分离的条件上进行内部联接吗?

最佳答案

MySql 5.6.5解决了与查询结构相关的性能问题。
请看这里:http://bugs.mysql.com/bug.php?id=42259
我不再需要更改NHibernate查询的输出格式。:)

关于mysql - NHibernate INNER JOIN在子查询上,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12515579/

10-11 02:52
查看更多