fix: Optimize heartbeat indexes containing important on sqlite using SQLite partial indexes (#6511)

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: louislam <1336778+louislam@users.noreply.github.com>
Co-authored-by: CommanderStorm <26258709+CommanderStorm@users.noreply.github.com>
This commit is contained in:
Copilot
2025-12-23 18:00:09 +00:00
committed by GitHub
parent d23ff8c486
commit af5fd5488d
2 changed files with 170 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
exports.up = async function (knex) {
const isSQLite = knex.client.dialect === "sqlite3";
if (isSQLite) {
// For SQLite: Use partial indexes with WHERE important = 1
// Drop existing indexes using IF EXISTS
await knex.raw("DROP INDEX IF EXISTS monitor_important_time_index");
await knex.raw("DROP INDEX IF EXISTS heartbeat_important_index");
// Create partial indexes with predicate
await knex.schema.alterTable("heartbeat", function (table) {
table.index([ "monitor_id", "time" ], "monitor_important_time_index", {
predicate: knex.whereRaw("important = 1")
});
table.index([ "important" ], "heartbeat_important_index", {
predicate: knex.whereRaw("important = 1")
});
});
}
// For MariaDB/MySQL: No changes (partial indexes not supported)
};
exports.down = async function (knex) {
const isSQLite = knex.client.dialect === "sqlite3";
if (isSQLite) {
// Restore original indexes
await knex.raw("DROP INDEX IF EXISTS monitor_important_time_index");
await knex.raw("DROP INDEX IF EXISTS heartbeat_important_index");
await knex.schema.alterTable("heartbeat", function (table) {
table.index([ "monitor_id", "important", "time" ], "monitor_important_time_index");
table.index([ "important" ]);
});
}
// For MariaDB/MySQL: No changes
};