[英]JPA query: what is a proper way to select by a large set of possible values?
What I have is a large (like hundreds of thousands) list of IDs. 我所拥有的是一大堆(如数十万个)ID。 I want to get some values from the objects with this IDs with a JPA query. 我想通过JPA查询从具有此ID的对象中获取一些值。
Option 1 - using IN clause 选项1-使用IN子句
List<Long> ids = /* ... */
entityManager.createQuery(
"SELECT v.lat, v.lon FROM Vehicle v WHERE v.id IN (:ids)",
Long[].class)
.setParameter("ids", ids)
.getResultList();
I'm afraid there might be a limit on IN
clause contents. 恐怕IN
子句的内容可能有限制。
Option 2 - query each ID separately 选项2-分别查询每个ID
List<Long> ids = /* ... */
for (Long id : ids) {
entityManager.createQuery(
"SELECT v.lat, v.lon FROM Vehicle v WHERE v.id = :id",
Long[].class)
.setParameter("id", id)
.getSingleResult();
}
That looks too slow as seems to be doing ids.size()
requests. 看起来好像在执行ids.size()
请求那样,它看起来太慢了。
What is a proper way to do this type of thing? 做这种事情的正确方法是什么?
Neither of your methods have much merit. 您的两种方法都没有什么优点。 As mentioned in Grzegorz's comment, all databases have a limit on how many list items you can use. 正如Grzegorz的评论中提到的那样,所有数据库都对可以使用的列表项有限制。 Also, there are limits on how many characters your query can contain. 此外,您的查询可以包含多少个字符也有限制。 So lists are out. 因此清单不存在。
Hundreds of thousands of individual queries seems (remember the posting guidelines Dan, be courteous) unwise. 成千上万的单个查询似乎是不明智的(请记住发布准则Dan,要有礼貌)。
If this were my problem, I'd look at how that list of id's got generated in the first place. 如果这是我的问题,那么我将首先看看如何生成ID列表。 It was probably a database query. 这可能是数据库查询。 If so, I would make a new version of that query to get the lats and lons. 如果是这样,我将对该查询进行新版本的获取纬度和经度。 It might be a table join or it might be a subquery. 它可能是表联接,也可能是子查询。 Without knowing your database structure, I can't give you any detailed suggestions. 不知道您的数据库结构,我无法给您任何详细的建议。
First thing that comes to mind: 首先想到的是:
For big data sets, the performance can be improved dramatically by doing (2) in parallel. 对于大数据集,可以通过并行执行(2)来显着提高性能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.