对于在Hibernate中遍历一对一关系的查询是否会对性能产生影响?

时间:2018-02-09 08:05:47

标签: java mysql sql hibernate query-performance

考虑到我有一个像这样定义的实体A

@Entity
public class A {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

    @Version
    @Column(name = "version")
    private Integer version;

    @OneToOne
    private B b;

    // getters & setters
}

和B,其定义如下:

@Entity
public class B {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

    @Version
    @Column(name = "version")
    private Integer version;

    private String x;

    // getters & setters
}

我们还假设x被编入索引。我的问题是,是否有一个普通的"明显的"查询来自A的x之间的性能差异如下:

TypedQuery<A> query = entityManager.createQuery("SELECT a FROM A a where a.b.x = :x", A.class);
query.setParameter("x", "myX");
A a = query.getSingleResult();

直接从B中查询x

TypedQuery<Person> q = entityManager.createQuery("select b from B b where b.x = :x", B.class);
q.setParameter("x", "myX");
B b = query.getSingleResult();  

如果重要的是我们正在使用MySQL 5.6。

0 个答案:

没有答案