简体   繁体   English

JPA性能:实体或实体ID作为查询参数?

[英]JPA Performance: Entity or Entity Id as query parameter?

Let's say there are two entity types: Book (with primary key bookId ) and Author (with primary key authorId ). 假设有两种实体类型: Book (带有主键bookId )和Author (带有主键authorId )。 An author writes 0...n books. 一位作者写了0 ... n本书。 So Author is referenced in Book using @ManyToOne . 所以, Author在引用Book使用@ManyToOne

If we want to retrieve all books written by a certain author, we could do two things in JPQL (eg NamedQuery): 如果我们想要检索某个作者编写的所有书籍,我们可以在JPQL中做两件事(例如NamedQuery):

  1. SELECT b FROM Book b WHERE b.author = :author (entity as parameter) SELECT b FROM Book b WHERE b.author = :author (entity as parameter)
  2. SELECT b FROM Book b WHERE b.author.authorId = :authorId (primary key ID of entity as parameter) SELECT b FROM Book b WHERE b.author.authorId = :authorId (实体的主键ID作为参数)

In the first option we have to watch out that the passed object is really of type Author and it already has a primary key. 在第一个选项中,我们必须注意传递的对象实际上是Author类型,并且它已经有一个主键。

But: Is there a difference regarding performance in the two options mentioned above? 但是:在上面提到的两个选项中,性能是否存在差异? I guess (2) is less expensive, but (1) might be easier to optimize for the JPA provider. 我猜(2)更便宜,但(1)可能更容易针对JPA提供商进行优化。 Is there a performance test or literature available on this question? 是否有关于此问题的性能测试或文献?

We can assume that the Author instance (whose books we want to search for) has already been loaded (eg to view his/her profile in the response), so it was no additional code writing effort to either pass the whole object or just it's ID to the book search method. 我们可以假设Author实例(我们想要搜索的书籍)已经被加载(例如,在响应中查看他/她的个人资料),因此没有额外的代码编写工作来传递整个对象或只是它的书籍搜索方法的ID。 But is there a difference in execution speed? 但是执行速度有差异吗? Maybe depending on the JPA provider used? 可能取决于使用的JPA提供商?

I have tested both queries and the resulting sql was the same. 我测试了两个查询,结果sql是一样的。

Answering to the second part of your quesion - there is a lot of relevant literature, but I can particularly recommend Pro JPA 2, 2nd Edition by Mike Keith , Merrick Schincariol . 回答你问题的第二部分 - 有很多相关的文献,但我特别推荐Pro JPA 2, 2nd Edition by Mike Keith , Merrick Schincariol

If you want test queries manually, you can watch SQL-queries resulting from JPA-queries. 如果您需要手动执行测试查询,则可以查看JPA查询产生的SQL查询。 One approach would be to get know your JPA provider and see what options he delivers for such purpose (eg Hibernate provides option hibernate.show_sql ). 一种方法是了解您的JPA提供程序并查看他为此目的提供的选项(例如,Hibernate提供了选项hibernate.show_sql )。 The second option (in my opinion - much better) is to use tool such as log4jdbc which is an additional layer (simplifying) between your JDBC driver and Persistence Provider and can log all queries sent to database along with their parameters. 第二个选项(在我看来 - 更好)是使用诸如log4jdbc工具,它是JDBC驱动程序和持久性提供程序之间的附加层(简化),并且可以记录发送到数据库的所有查询及其参数。 It can also measure time spend on each query, so I think it is ideal tool for performance testing. 它还可以测量每个查询的时间花费,因此我认为它是性能测试的理想工具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM