简体   繁体   English

在JPA中优化数百个选择查询

[英]Optimize hundreds select queries in JPA

I am using JPA and Facebook login for my app. 我正在为我的应用程序使用JPAFacebook登录名。 After the user has logged in the server checks if each user is in our database by Facebook id. 用户登录服务器后,通过Facebook ID检查每个用户是否在我们的数据库中。 I created a for loop which execute a select query each time. 我创建了一个for循环,每次执行一次select查询。 This approach is slow. 这种方法很慢。 Is there some better way to make it faster? 有什么更好的方法可以使其更快?

The code is 该代码是

for (User friend : friends) {
      User current = userManager.find(friend.facebookId);
If (null != current) { friend.setId(current.getId);}}

You could execute a single query with an IN instead. 您可以改为使用IN执行单个查询。 However with caching enabled, querying by id may be the fastest method if the friends are likely to be in the cache. 但是,启用缓存后,如果好友很可能位于缓存中,则按ID查询可能是最快的方法。

Select f from Friend f where f.id in :ids

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

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