diff --git a/common/src/main/java/me/lucko/luckperms/common/messaging/sql/AbstractSqlMessenger.java b/common/src/main/java/me/lucko/luckperms/common/messaging/sql/AbstractSqlMessenger.java index c9488373d..0f1bd6ef1 100644 --- a/common/src/main/java/me/lucko/luckperms/common/messaging/sql/AbstractSqlMessenger.java +++ b/common/src/main/java/me/lucko/luckperms/common/messaging/sql/AbstractSqlMessenger.java @@ -59,7 +59,7 @@ public abstract class AbstractSqlMessenger implements Messenger { public void init() throws SQLException { try (Connection c = getConnection()) { // init table - String createStatement = "CREATE TABLE IF NOT EXISTS `" + getTableName() + "` (`id` INT AUTO_INCREMENT NOT NULL, `time` TIMESTAMP NOT NULL, `msg` TEXT NOT NULL, PRIMARY KEY (`id`)) DEFAULT CHARSET = utf8mb4"; + String createStatement = "CREATE TABLE IF NOT EXISTS `" + getTableName() + "` (`id` INT AUTO_INCREMENT NOT NULL, `time` TIMESTAMP NOT NULL, `msg` TEXT NOT NULL, PRIMARY KEY (`id`), KEY (`time`)) DEFAULT CHARSET = utf8mb4"; try (Statement s = c.createStatement()) { try { s.execute(createStatement); @@ -73,6 +73,18 @@ public abstract class AbstractSqlMessenger implements Messenger { } } + // add index for time column if it doesn't already exist + try (PreparedStatement ps = c.prepareStatement("SELECT 1 FROM INFORMATION_SCHEMA.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ? AND COLUMN_NAME = 'time' LIMIT 1")) { + ps.setString(1, getTableName()); + try (ResultSet rs = ps.executeQuery()) { + if (!rs.next()) { + try (Statement s = c.createStatement()) { + s.execute("CREATE INDEX `time` ON `" + getTableName() + "` (`time`)"); + } + } + } + } + // pull last id try (PreparedStatement ps = c.prepareStatement("SELECT MAX(`id`) as `latest` FROM `" + getTableName() + "`")) { try (ResultSet rs = ps.executeQuery()) { @@ -112,7 +124,7 @@ public abstract class AbstractSqlMessenger implements Messenger { } try (Connection c = getConnection()) { - try (PreparedStatement ps = c.prepareStatement("SELECT `id`, `msg` FROM `" + getTableName() + "` WHERE `id` > ? AND (NOW() - `time` < 30)")) { + try (PreparedStatement ps = c.prepareStatement("SELECT `id`, `msg` FROM `" + getTableName() + "` WHERE `id` > ? AND `time` > (NOW() - INTERVAL 30 SECOND)")) { ps.setLong(1, this.lastId); try (ResultSet rs = ps.executeQuery()) { while (rs.next()) { @@ -139,7 +151,7 @@ public abstract class AbstractSqlMessenger implements Messenger { } try (Connection c = getConnection()) { - try (PreparedStatement ps = c.prepareStatement("DELETE FROM `" + getTableName() + "` WHERE (NOW() - `time` > 60)")) { + try (PreparedStatement ps = c.prepareStatement("DELETE FROM `" + getTableName() + "` WHERE `time` < (NOW() - INTERVAL 60 SECOND)")) { ps.execute(); } } catch (SQLException e) { diff --git a/settings.gradle b/settings.gradle index d0a3a06c7..c4e6bb3ca 100644 --- a/settings.gradle +++ b/settings.gradle @@ -13,7 +13,7 @@ pluginManagement { } plugins { - id("org.gradle.toolchains.foojay-resolver-convention") version("0.8.0") + id("org.gradle.toolchains.foojay-resolver-convention") version("1.0.0") } rootProject.name = 'luckperms'