-
Notifications
You must be signed in to change notification settings - Fork 164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Spring Data JPA利用@EntityGraph解决N+1的SQL查询问题: @OnetoOne @OneToMany #36
Comments
解决办法是把
我们再执行上面的测试用例,而打印的SQL就会变成一个SQL,如下:
从而解决了问题。 |
zhangzhenhuajack
changed the title
Spring Data JPA利用@EntityGraph解决N+1的SQL查询问题
Spring Data JPA利用@EntityGraph解决N+1的SQL查询问题: @OnetoOne @OneToMany
Sep 17, 2021
不过建议对JPA不熟悉的,建议实体上都不要用关联关系。用mysql的思路解决问题即可; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
出现N+1的SQL的场景,我们有如下四个实体,核心内容如下:
那么我们发生的问题如下:
当我们把@entitygraph注释掉之后,执行如下测试用例的时候就会发生N+1的sql问题
N+1的sql现象如下:
问题总结:
也就是当我们查询2条tpuser的时候就会产生6条SQL,而最后的third_party_tpusers 只生成了一条sql是因为我们配置了
spring.jpa.properties.hibernate.default_batch_fetch_size=50
但是fetch_size解决不了 @OnetoOne的N+1的SQL问题。The text was updated successfully, but these errors were encountered: