简体   繁体   中英

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.

I test the query on the sql server and it returns the result instantaneously.

When I test the query on my project, it tooks more than 4 seconds to fetch 10 lines.

Here are the things I think to do : using @Query, but this is will take too long to change many methods. I added an all-argument Constructor but nothing changed. I changed the fetch method from eager to lazy but nothing changed

I tried to to setup this property in the jdbc url : sendStringParametersAsUnicode=false

It reduce time by 0.5 seconds but this is not enough.

This is my code: 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.

Adding sendStringParametersAsUnicode=false to the jdbc url drastically improved the response time from ~2000ms to 200ms. Cant believe this. Thanks a bunch for this answer.

Try to add JPQL query directly inside userService Interface. This way you will use this queries and not the one that spring will generate.

@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.

@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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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