1

As you can see in this question JPA 2.0, Criteria API, Subqueries, In Expressions, there is a method for subqueries with Criteria API:

SELECT e 
FROM Employee e 
WHERE e IN (SELECT emp
          FROM Project p JOIN p.employees emp 
         WHERE p.name = :project)

But how about simply query like this:

SELECT e 
FROM Employee e 
WHERE e.project.name = :project

Which one is faster and why?

Community
  • 1
  • 1
user1079877
  • 9,008
  • 4
  • 43
  • 54
  • 1
    You'd have to measure, but I don't see how the second one could be slower that the first one, given that it's basically the same of the subquery of the first one. Why use something complex when something simpler does the same thing? – JB Nizet Feb 01 '15 at 07:36
  • It's because I think it's possible for Database to cache the result of the second part and for the second query, it's not possible or maybe it is also!!! Depend on the ORM behavior – user1079877 Feb 02 '15 at 05:43

0 Answers0