We're designing an API Rest with the following technologies :
- spring-boot 1.5.7
- spring-data-jpa
- MySQL 5.5.59
It's a Java REST API designs with spring-boot and connected to a MySQL database via a mysql-connector. Most of the queries in repositories are written in the JPA Query way. To compile it we use maven and we deploy it in fatjar running in a tomcat sever.
The fact is several tables and resources within can be very large for one user. So when a GET request is made with high offset, we can deal with slow queries.
We tried several solutions like :
- set tomcat parameters for the mysql connection
- use hikari but no available parameters to kill slow queries
- deploy the rest api in a glassfish5 server but the spring data jpa queries throw an execption (could not extract result set), so it's a problem.
Moreover, a lot of people gonna say me "use pagination and check the offset in the request". The fact is that we're using that but the class PageRequest have a tricky behaviour. Indeed, the resource the API give you back with this class has the shape below :
{
"content": {},
"links": [
{
"href": "string",
"rel": "string",
"templated": true
}
],
"page": {
"number": 0,
"size": 0,
"totalElements": 0,
"totalPages": 0
}
}
As you can see you have an object page with a field totalElements. This is the problem if you have a lot of entries in a table for one user. Because spring data gonna do a "SELECT COUNT(*)" to have this totalElements information. Pagination is not the solution cause we arleady have thid.
So what we're asking you are the best practices and maybe the solution to handle and kill slow queries in a Restful API designs with spring-boot.
Thanks !