简体   繁体   English

Hibernate 需要很长时间才能获取查询结果

[英]Hibernate takes a long time to get query result

Whenever I query a Table letting Spring Data/Hibernate build the query 'findByLabelAndUserId' on my code, it always seems to take forever.每当我查询一个表,让 Spring Data/Hibernate 在我的代码上构建查询 'findByLabelAndUserId' 时,它似乎总是需要永远。

I test the query on the sql server and it returns the result instantaneously.我在 sql server 上测试查询,它立即返回结果。

When I test the query on my project, it tooks more than 4 seconds to fetch 10 lines.当我在我的项目上测试查询时,获取 10 行需要 4 秒多的时间。

Here are the things I think to do : using @Query, but this is will take too long to change many methods.以下是我认为要做的事情:使用@Query,但更改许多方法需要很长时间。 I added an all-argument Constructor but nothing changed.我添加了一个全参数构造函数,但没有任何改变。 I changed the fetch method from eager to lazy but nothing changed我将 fetch 方法从急切改为懒惰,但没有任何改变

I tried to to setup this property in the jdbc url : sendStringParametersAsUnicode=false我试图在 jdbc url 中设置这个属性:sendStringParametersAsUnicode=false

It reduce time by 0.5 seconds but this is not enough.它将时间减少了 0.5 秒,但这还不够。

This is my code: LeaveController:这是我的代码:LeaveController:

@RequestMapping(value = "/leaves-management/leaves", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasAuthority('view_MY_LEAVE')")
public List<LeaveDto> getAllByUser(HttpServletRequest httpRequest) {
    String token = httpRequest.getHeader(tokenHeader);
    String username = jwtTokenUtil.getUsernameFromToken(token);
    User user = userService.findByUsername(username);
    List<Request> listRequests = requestService.findByLabelAndUserId(LabelRequest.LEAVE, user.getId());
    return listModelToListDtoWithStatus(listRequests);
}

the entity section:实体部分:

public class Request extends AbstractAuditEntity {
    
    @Column(nullable = false)
        @Enumerated(EnumType.STRING)
        private LabelRequest label;

        @Column(nullable = false)
        @Enumerated(EnumType.STRING)
        private Status status;

        @ManyToOne(fetch = FetchType.LAZY)
        private User user;
    }

What can I do to reduce time building query using spring data/hibernate.我可以做些什么来减少使用 spring 数据/休眠构建查询的时间。

Adding sendStringParametersAsUnicode=false to the jdbc url drastically improved the response time from ~2000ms to 200ms.将 sendStringParametersAsUnicode=false 添加到 jdbc url 将响应时间从 ~2000ms 大幅缩短到 200ms。 Cant believe this.不敢相信这个。 Thanks a bunch for this answer.非常感谢这个答案。

Try to add JPQL query directly inside userService Interface.尝试直接在 userService 接口中添加 JPQL 查询。 This way you will use this queries and not the one that spring will generate.这样,您将使用此查询而不是 spring 将生成的查询。

@Query("SELECT r FROM Request r WHERE r.label = label AND r.user = user")
Collection<Request> findByLabelAndUserId(@Param("label") Label label, @Param("user") User user);

If this does not work try directly native query again inside userService Interface.如果这不起作用,请在 userService 接口中再次尝试直接本机查询。

@Query("SELECT r FROM Table_Request r WHERE r.Column_label = label AND r.Column_user = user",  nativeQuery = true)
Collection<Request> findByLabelAndUserId(@Param("label") Label label, @Param("user") User user);

Nevertheless it is very strange why spring data make so inefficient query.然而,很奇怪为什么 spring 数据的查询效率如此低下。 Bare in mind if the query is so simple the problem can be somewhere else in your application.请记住,如果查询如此简单,问题可能出在应用程序的其他地方。

Good luck!祝你好运!

Sounds like a typical "left join fetch" issue.听起来像是典型的“左连接获取”问题。 Check what is the effective query that gets executed检查执行的有效查询是什么

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

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