[英]Does JPA load entities in JPQL, as it does when using entities in code?
I have a two tables: Person
and House
, the mapping is one to one. 我有两个表:
Person
和House
,映射是一对一的。
Now I have to assign the address
of Person
and House
(which can be different) to the same address. 现在,我必须将“
Person
和“ House
的address
(可以不同)分配给相同的地址。
There are more than 5000 records. 有超过5000条记录。 Which will be faster?
哪个会更快? Using Code to update the entities one by one, eg
使用代码一一更新实体,例如
for (id : Ids) {
Person person = PersonDAO.find(id);
person.setAddress ("abc");
}
and then doing same with House
; 然后对
House
做同样的事情;
Or should I use JPQL to update both in two different queries, eg 还是我应该使用JPQL在两个不同的查询中同时更新两者,例如
UPDATE Person p SET p.Address = "abc" WHERE ID IN(.....ID QUERY)
My question is what will be faster? 我的问题是什么会更快? Will the update using JPQL have the same performance, same as that in code?
使用JPQL进行的更新是否具有与代码中相同的性能? Or should I use native query to NOT load the entities, as I only want performance.
还是我应该使用本机查询来不加载实体,因为我只想要性能。
Using the query will be faster (and much more memory efficient), as the query provider will translate the JPQL query to native SQL. 使用查询将更快(并且内存效率更高),因为查询提供程序会将JPQL查询转换为本机SQL。 Also, if you use entities directly, the number of queries made against the database will be siginificantly higher (one select and update for each and every row).
同样,如果直接使用实体,则对数据库的查询数量将大大增加(每行一次选择并更新)。
The native query will be faster, as it doesn't have to translate anything. 本机查询将更快,因为它无需翻译任何内容。
If you want it to be even faster, you can use a PreparedStatement . 如果您希望它更快,可以使用PreparedStatement 。 With the .addBatch() method you add the query to the batch, and with the executeBatch() method you will execute the full batch, minimizing the amount of times being switched between user and kernel mode.
使用.addBatch()方法,您可以将查询添加到批处理中,而使用executeBatch()方法,您将执行完整的批处理,从而最大程度地减少了在用户和内核模式之间切换的时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.