简体   繁体   English

JPA查询:通过大量可能的值进行选择的正确方法是什么?

[英]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: 首先想到的是:

  1. Cut the id list in parts of say 1000 ids. 将ID列表切成1000个ID的一部分。 A manageable size. 可管理的大小。
  2. Run an IN query for each part. 对每个零件运行一个IN查询。
  3. Aggregate the result into a list. 将结果汇总到列表中。

For big data sets, the performance can be improved dramatically by doing (2) in parallel. 对于大数据集,可以通过并行执行(2)来显着提高性能。

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

相关问题 JPA选择查询具有多个值 - JPA select query with multiple values 处理大文件的正确方法是什么? - What is the proper way to handle large files? JPA:迭代大型结果集的正确模式是什么? - JPA: what is the proper pattern for iterating over large result sets? 使用Spring Data JPA更新实体的正确方法是什么? - What is the proper way of updating entity using Spring Data JPA? 设置 JLabel 以显示图像的正确方法是什么? - What is the proper way to set a JLabel to show an image? JPA查询语法问题还是什么? JPA SELECT NEW语法问题? - JPA query syntax issue or what ?! JPA SELECT NEW syntax issue? 有没有办法在 JPA 查询中从数据库中获取 select 特定列 - Is there a way to select specific columns from a database in a JPA query 在JPA中选择具有子实体的许多实体的最佳优化方法是什么? - What is the best optimized way to select many entities with subentities in JPA? 使用ServerInterceptor设置MDC以登录grpc-java的正确方法是什么? - What is the proper way to set MDC for logging in grpc-java with ServerInterceptor? 将 Hibernate Query.list() 转换为 List 的“正确”方法是什么<type> ?</type> - What is the "proper" way to cast Hibernate Query.list() to List<Type>?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM