【问题标题】:QueryDSL generates cross joinQueryDSL 生成交叉连接
【发布时间】:2014-10-10 19:44:12
【问题描述】:

我希望为结果提供内容过滤。我的(为简洁而编辑)实体如下所示:

节点:

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Node {
    @Id
    @GeneratedValue
    public Integer id;
}

场景:

@Entity
public class Scene extends Node {
    @OneToMany(mappedBy = "scene", /*fetch = FetchType.EAGER, */cascade = CascadeType.ALL)
    public List<Source> sources;
}

来源:

@Entity
public class Source extends Node {
    @ManyToOne
    public Scene scene;

    @Enumerated(EnumType.ORDINAL)
    public SourceType type;
}

以下是我希望实现的过滤器示例。

给定一个 SourceTypes 的集合,我希望选择所有场景,以便该场景由这些类型中的每一个的源引用。我使用带有以下谓词的 QueryDSL 来实现这一点:

private Predicate filterBySourceType(Collection<SourceType> it) {
    BooleanBuilder bb = new BooleanBuilder();

    for (SourceType st : it)
        bb.and(QScene.scene.sources.any().type.eq(st));

    return bb.getValue();
}

将一系列这些谓词组合起来以提供一个整体查询。仅选择一个 SourceType 时,结果查询如下所示:

Hibernate: select count(scene0_.id) as col_0_0_ from Scene scene0_ inner join Node scene0_1_ on scene0_.id=scene0_1_.id where exists (select 1 from Source source1_ inner join Node source1_1_ on source1_.id=source1_1_.id where (source1_.id in (select sources2_.id from Source sources2_ inner join Node sources2_1_ on sources2_.id=sources2_1_.id where scene0_.id=sources2_.scene_id)) and source1_.type=?)

我相信上面发生的事情是交叉连接,因此(2k 个场景,1k 个源)查询需要几秒钟。

我尝试切换到具体的类多态性来消除节点连接,但没有明显的性能提升。

如何优化该谓词?

【问题讨论】:

    标签: hibernate jpa spring-data-jpa querydsl


    【解决方案1】:

    事实证明,您可以使用 JPASubQuery 来获取 CollectionExpression 并写出查询,类似于使用普通旧 SQL 编写它的方式。

            bb.and(QScene.scene.in(
                    new JPASubQuery()
                            .from(source)
                            .where(source.type.eq(st))
                            .list(source.scene)
            ));
    

    【讨论】:

    • 真的解决了问题吗??在您进行此编辑后,您还可以发布结果查询吗?
    猜你喜欢
    • 2013-08-13
    • 2013-02-25
    • 2021-11-08
    • 2013-05-11
    • 2015-10-28
    • 2020-03-12
    • 2013-06-08
    • 2013-05-06
    • 2014-11-19
    相关资源
    最近更新 更多