-
Notifications
You must be signed in to change notification settings - Fork 75
Logging slow queries
SlowQueryListener
triggers callback when query execution exceeds specified threshold time.
For example, when 1 min is set to the threshold in a slow query logging listener and a query takes 5 min to run, it will log the query when 1 min has passed while the query is still running.
This is expected behavior; However, elapsed time in ExecutionInfo
is not populated because it is still executing the query.
I was asked how to log queries that have passed threshold with elapsed time populated. This behavior can achieved by customizing existing logging query listeners.
Here is sample implementation with SLF4JQueryLoggingListener
:
long thresholdInMills = ...
SLF4JQueryLoggingListener listener = new SLF4JQueryLoggingListener() {
@Override
public void afterQuery(ExecutionInfo execInfo, List<QueryInfo> queryInfoList) {
// call query logging logic only when it took more than threshold
if (thresholdInMills <= execInfo.getElapsedTime()) {
super.afterQuery(execInfo, queryInfoList);
}
}
};
listener.setLogLevel(SLF4JLogLevel.WARN);
This implementation will log queries that took longer than specified threshold AFTER query execution has finished. So that, query execution time is populated.