I have a regular easy SQL query to SingleStore db
SELECT ses.created_on, ses.update_on, ses.job_id, ses.ip
FROM session ses
WHERE ses.created_on != ses.updated_on
AND ses.updated_on >= DATE_SUB(NOW(), INTERVAL :threshold SECOND)
AND ses.created_on <= :endDate
AND ses.updated_on >= :startDate
The time for a direct request is 1.6 seconds, but when I try to use it with @Query and native=true, it takes 30 seconds+. It has about 1.5 million elements. Is it okay? How can I optimize it?
I have tried something like that
@Query(value = """SELECT ses.created_on, ses.update_on, ses.job_id, ses.ip
FROM session ses
WHERE ses.created_on != ses.updated_on
AND ses.updated_on >= DATE_SUB(NOW(), INTERVAL :threshold SECOND)
AND ses.created_on <= :endDate
AND ses.updated_on >= :startDate", nativeQuery = true)
Set<Tuple> query (@Param("threshold") Long threshold, @Param("startDate") Instant startDate, @Param("endDate") Instant endDate);
default Set<Data> dataquery(Instant startDate, Instant endDate, Long threshold){
query(threshHold, statDate, endDate)
.stream()
.map(tupl -> new Data(tupl.get("created_on", Timestamp.class).toInstant(),
tupl.get("updated_on", Timestamp.class).toInstant(),
tupl.get("job_id", Long.class),
tupl.get("ip", String.class)
.collect(Collectors.toSet());
}
I have tried using a Projection object instead of a Tuple, but it is still too slow. The problem is just inside the “query” method, not because of the stream inside the “data query.” Only the “query” method executes for about 30 seconds.
Now I’m trying to use just regular Connection. PreparedStatement and ResultSet but still 20-25sec
4