KEYCLOAK-19052: Optimised (split) the clearExpiredEvents query to reduce execution time

This commit is contained in:
Kashif Saadat 2021-10-29 17:01:26 +01:00 committed by Hynek Mlnařík
parent 7f734c9e68
commit d9bf511406

View file

@ -88,16 +88,17 @@ public class JpaEventStoreProvider implements EventStoreProvider {
if (KeycloakModelUtils.isRealmProviderJpa(session)) { if (KeycloakModelUtils.isRealmProviderJpa(session)) {
// Group realms by expiration times. This will be effective if different realms have same/similar event expiration times, which will probably be the case in most environments // Group realms by expiration times. This will be effective if different realms have same/similar event expiration times, which will probably be the case in most environments
List<Long> eventExpirations = em.createQuery("select distinct realm.eventsExpiration from RealmEntity realm").getResultList(); List<Long> eventExpirations = em.createQuery("select distinct realm.eventsExpiration from RealmEntity realm where realm.eventsExpiration > 0").getResultList();
for (Long expiration : eventExpirations) { for (Long expiration : eventExpirations) {
if (expiration > 0) { List<String> realmIds = em.createQuery("select realm.id from RealmEntity realm where realm.eventsExpiration = :expiration")
int currentNumDeleted = em.createQuery("delete from EventEntity where realmId in (select realm.id from RealmEntity realm where realm.eventsExpiration = :expiration) and time < :eventTime") .setParameter("expiration", expiration)
.setParameter("expiration", expiration) .getResultList();
.setParameter("eventTime", currentTimeMillis - (expiration * 1000)) int currentNumDeleted = em.createQuery("delete from EventEntity where realmId in :realmIds and time < :eventTime")
.executeUpdate(); .setParameter("realmIds", realmIds)
logger.tracef("Deleted %d events for the expiration %d", currentNumDeleted, expiration); .setParameter("eventTime", currentTimeMillis - (expiration * 1000))
numDeleted += currentNumDeleted; .executeUpdate();
} logger.tracef("Deleted %d events for the expiration %d", currentNumDeleted, expiration);
numDeleted += currentNumDeleted;
} }
logger.debugf("Cleared %d expired events in all realms", numDeleted); logger.debugf("Cleared %d expired events in all realms", numDeleted);
} else { } else {