Problem with hibernation pagination
I have a problem with Hibernate Pagination, and to some extent this is explained in
Mysql pagination optimization
Using Hibernate Scrolling Results to Slowly Read 90 Million Records
Hibernate - HQL pagination
Pagination and Sorting Issues
Hibernate row pagination
More details
HQL query from application:
Query q = session.createQuery("from RequestDao r order by r.id desc"); q.setFirstResult(0); q.setMaxResults(50);
Query returns 3 million records, and for pagination we set only 50 of these records, the pagination page is very slow, because with each update we call a query that receives 3 million records, and of them we set only 50 records.
My main question is:
Does HQL always work and get into the database, or does it go and clicks on a session or memory to search for data, and if every time it accesses the database and receives results, then this is very good from the point of view of performance, which would be better solutions to improve it?
Using HQL in sleep mode is the way we can query the database and first get only 50 records, and then get other records as required by the user. This problem really clogs the application, and so what is the best way to solve this problem?
HQL query generated in logs
from com.delta.dao.RequestDao r order by r.id desc
Hibernate generated request
select getrequest0_.ID as ID24_, getrequest0_.TIME as START3_24_, getrequest0_.STAT as STATUS24_, getrequest0_.SUM as SUMMARY24_, getrequest0_.OUTNAME as OUTPUT7_24_, getrequest0_.INPNAME as INPUT8_24_, getrequest0_.REQUEST_DATE as requestT9_24_, getrequest0_.PARENT_ID as PARENT10_24_, getrequest0_.INTER_TYPE as INTERPO60_24_, getrequest0_.OPEN_INT as OPEN61_24_, getrequest0_.SOURCE_TYPE as SOURCE62_24_, getrequest0_.TARGET_TYPE as TARGET20_24_, getrequest0_.SOURCE as SOURCE14_24_, getrequest0_.COPY_DATA as COPY16_24_, getrequest0_.CURVE as GENERATE63_24_, getrequest0_.TITLE as TITLE24_, getrequest0_.TIME_ID as TIMESERIES12_24_, getrequest0_.TASK_NAME as TASK51_24_ from REQUEST getrequest0_ where getrequest0_.KIND='csv' order by getrequest0_.ID desc
Here Explain the plan for the request:
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
| 1 | SIMPLE | getrequest0_ | ref | TR_KIND_ID | TR_KIND_ID | 6 | const | 1703018 | 100.00 | Using where |
Additional information: query execution time with and without order conditions for 50 recording restrictions
If I run a query with order , then the query takes 0.0012s with the setting LIMIT 50 and without order , the same query takes 0.0032s with the same LIMIT 50 .
Just as we can find if:
in a, is it true that we get the result from the database and store it in memory or where, if not, and at this time we have 3 million results in the result set, and then in b and c we set the offset value and limit so on, on the page we would see only 50 results, so now, where 3 million records are left, and on the second call of this query, we again go and hit the database and get 3 million records and put them into memory, and then back to c we set 50 records and continue.
I donโt understand this problem, and I would really appreciate it if someone could give a clear and detailed explanation of how this works and what would be the best solution for this problem.
Update
As it turns out, the problem with am is not related to the display of records on the page, but I have a filter on this page, and for each request I get all the outgoing values โโfrom the database again, and there are some funky things that happen in this causing an increase in time page loading.
I make several nested hibernation queries in the database and get the results back, what would be the best solution for this problem?