perf: add occurred_at indexes to connections
Time-window queries (dashboard stats, recent connections, JSON API, purge) had no usable index and full-scanned the connections table on every load, so the dashboard slowed down as data accumulated. - Add idx_occurred (occurred_at) for counts, MAX, ORDER BY ... LIMIT, and the purge delete - Add idx_src_ip_occ (src_ip, occurred_at) so frequent_ips() runs as a covering index scan - Drop the now-redundant standalone idx_src_ip Migrations in setup.php are idempotent (information_schema checks) and portable across MySQL 8 and MariaDB.
This commit is contained in:
1 parent
05e9fbe76e
commit
849360e61c
3 files changed
+41
-3
No files matched your search
@@ -41,6 +41,43 @@ if ($row > 0) {
|
||||
echo "Dropped legacy uq_event unique key.\n";
|
||||
}
|
||||
|
||||
// Migration: add occurred_at index if upgrading from an older schema.
|
||||
// Time-window queries (dashboard, JSON API, purge) full-scanned the table
|
||||
// without it, so page loads slowed down as the table grew.
|
||||
$row = $pdo->query(
|
||||
"SELECT COUNT(*) FROM information_schema.STATISTICS
|
||||
WHERE TABLE_SCHEMA = DATABASE()
|
||||
AND TABLE_NAME = 'connections'
|
||||
AND INDEX_NAME = 'idx_occurred'"
|
||||
)->fetchColumn();
|
||||
if ($row == 0) {
|
||||
$pdo->exec("ALTER TABLE connections ADD KEY idx_occurred (occurred_at)");
|
||||
echo "Added idx_occurred index.\n";
|
||||
}
|
||||
|
||||
// Migration: replace the standalone src_ip index with (src_ip, occurred_at)
|
||||
// so frequent_ips() resolves as a covering index scan.
|
||||
$row = $pdo->query(
|
||||
"SELECT COUNT(*) FROM information_schema.STATISTICS
|
||||
WHERE TABLE_SCHEMA = DATABASE()
|
||||
AND TABLE_NAME = 'connections'
|
||||
AND INDEX_NAME = 'idx_src_ip_occ'"
|
||||
)->fetchColumn();
|
||||
if ($row == 0) {
|
||||
$pdo->exec("ALTER TABLE connections ADD KEY idx_src_ip_occ (src_ip, occurred_at)");
|
||||
echo "Added idx_src_ip_occ index.\n";
|
||||
}
|
||||
$row = $pdo->query(
|
||||
"SELECT COUNT(*) FROM information_schema.STATISTICS
|
||||
WHERE TABLE_SCHEMA = DATABASE()
|
||||
AND TABLE_NAME = 'connections'
|
||||
AND INDEX_NAME = 'idx_src_ip'"
|
||||
)->fetchColumn();
|
||||
if ($row > 0) {
|
||||
$pdo->exec("ALTER TABLE connections DROP INDEX idx_src_ip");
|
||||
echo "Dropped redundant idx_src_ip index.\n";
|
||||
}
|
||||
|
||||
// Migration: create settings table and seed default retention if upgrading
|
||||
$pdo->exec(
|
||||
"CREATE TABLE IF NOT EXISTS settings (
|
||||
|
||||
Reference in new issue
Block a user