Add index to time column in SQL Messenger (#4255)

This commit is contained in:
Yannick Schinko authored and GitHub committed 2026-05-11 21:17:18 +01:00
1 parent 8b3e990106
commit 090f218e12
2 files changed
+16 -4

No files matched your search

@@ -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) {
+1 -1
View File
@@ -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'