简体   繁体   中英

First time SQL query is taking More time from Spring Boot application

I have to fetch some records and then display in output. In my Spring Boot Application, I am using JPA Specification for creating Criteria and then calling repo.findALL(Specification, Pageable ) , query generated by JPA is below:

Select * 
From "Table" 
Where "condition1" and "condition2" and condition"3"  
OrderBy column1 desc 
    offset 0 rows` fetch next 10 rows only

This query sometimes take more than 40 secs and rest of the time some 100ms. Although this issue is very rare (Once in 300-400 times) This table have around 40000 rows and one of the column is having JSON DATA

Is there any way to detect why this query taking much time randomly. When I manually triggered this query in DB then only once it took around 35+ secs and later on every time it is hardly taking 200ms. Any tool/approach to detect this rarely happening issue

First of all, for 40000 rows that amount of time for execution seems insane.

Do check your indexed columns.

Secondly, as per your question, the query takes long time when executing first time and shorter times while executed there after. Databases use query_cache in order to perform faster executions. When you execute a query for the first time, database has to go into execution plans for retrieving your result but when executed there after the execution plan is already there. Hence, it takes shorter interval to execute after first execution or ways to optimize your query.

You can check your query execution plan with

explain Select * from "Table" where "condition1" and "condition2" and condition"3"  orderBy column1 desc offset 0 rows

Explain is a keyword which shows the tentative query execution plan. Hope this helps.:)

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