简体   繁体   English

DB 查询速度快,但 JPARepository 获取速度慢

[英]Query on DB is fast, but fetching by JPARepository is slow

I have following tables in DB:我在数据库中有以下表格:

Person , Parent , GrandParent Parent - Person is OneToMany relation ( Person has parentId ) GrandParent - Parent is ManyToMany relation ( grandparent_parent table) Person , Parent , GrandParent Parent - PersonOneToMany关系( PersonparentIdGrandParent - ParentManyToMany关系( grandparent_parent表)

I created in PersonRepository which extends JPARepository :我在扩展JPARepository PersonRepository中创建:

@Query("SELECT person.uuid FROM Person person JOIN person.parent parent JOIN parent.grandparents grandparent WHERE grandparent.uuid = ?1")
Set<String> findByGrandParentId(final String parentId);

For fetching all ids of Person which are under given GrandParent .用于获取在给定GrandParent下的Person所有 id。 This SQL is created:创建此 SQL:

SELECT
   person0_.uuid as col_0_0_ 
FROM
    person person0_ 
INNER JOIN
    parent parent1_ 
        on person0_.parentid=parent1_.uuid 
INNER JOIN
    grandparent_parent grandaparent_parent2_ 
        on parent1_.uuid=grandaparent_parent2_.parentid 
INNER JOIN
    grandparent parent3_ 
        on grandaparent_parent2_.grandparentid=parent3_.uuid 
WHERE
    parent3_.uuid='13906f55-441c-45bd-bef6-8beefa4119c4'

I logged how much time repository needs fetch data, and it took (average) ~400ms to fetch ~400 records.我登录库需要多少时间获取数据,并花了(平均)〜400ms的获取〜400分的记录。 Then I execute the same SQL query on DB and each time query took no more than 50ms .然后我在 DB 上执行相同的 SQL 查询,每次查询花费的时间不超过50ms

I know that this generated query is not optimized because we can only join two tables GRANDPARENT_PARENT and PERSON , but this is not the problem here, because such query is executed also below 50ms .我知道这个生成的查询没有优化,因为我们只能连接两个表GRANDPARENT_PARENTPERSON ,但这不是这里的问题,因为这样的查询也在50ms以下执行。

Why I have such differences between fetching by repository and fetching in db?为什么我在通过存储库获取和在数据库中获取之间有如此大的差异? Is it possible to fix that?有没有可能解决这个问题?

Multiple Possibilities:多种可能性:

1. The generated Query 1. 生成的查询

The generated query looks pretty fine to me.生成的查询对我来说看起来很不错。 It's exactly the same as the query within your @Query annotation.它与@Query注释中的查询@Query

2. SQL Result to Java Object conversion 2. SQL Result 到 Java Object 的转换

I don't know how big your tables are, but: Converting SQL Results to Java Objects takes some time.我不知道您的表有多大,但是:将 SQL 结果转换为 Java 对象需要一些时间。 For small tables this could increase query time by 0-5%.对于小表,这可能会增加 0-5% 的查询时间。

3. Lazy loading 3. 懒加载

You didn't show the code for your entities.您没有显示实体的代码。 If you have @OneToMany or @ManyToMany relations, JPA will by default use Lazy Loading.如果您有@OneToMany@ManyToMany关系,JPA 将默认使用延迟加载。 This can really slow down everything by magnitudes.这真的可以让一切都减慢幅度。

4. Latency 4. 延迟

If you execute the SQL Query on the same machine where the SQL DB is, but your Java Application communicates over network with the SQL DB, it can result in much slower queries, too.如果您在 SQL DB 所在的同一台机器上执行 SQL 查询,但您的 Java 应用程序通过网络与 SQL DB 通信,它也会导致查询速度慢得多。

(5. Wrong kind of DB. Seems like you build an object graph. Maybe have a look at Neo4j ;-) ) (5. 错误的数据库类型。好像你构建了一个对象图。也许看看 Neo4j ;-) )

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

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