From af3d879fe74a393d53edc09c2c8f1e2573166259 Mon Sep 17 00:00:00 2001 From: Niek Candaele Date: Sun, 13 Oct 2024 19:34:30 +0200 Subject: [PATCH 1/2] fix: add indexes to speed up module-related queries --- ...20241013173159-add-indexen-for-moduleId.ts | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 packages/lib-db/src/migrations/sql/20241013173159-add-indexen-for-moduleId.ts diff --git a/packages/lib-db/src/migrations/sql/20241013173159-add-indexen-for-moduleId.ts b/packages/lib-db/src/migrations/sql/20241013173159-add-indexen-for-moduleId.ts new file mode 100644 index 0000000000..920ea62845 --- /dev/null +++ b/packages/lib-db/src/migrations/sql/20241013173159-add-indexen-for-moduleId.ts @@ -0,0 +1,37 @@ +import { Knex } from 'knex'; + +export async function up(knex: Knex): Promise { + await knex.schema.alterTable('functions', (table) => { + table.index('moduleId'); + }); + await knex.schema.alterTable('commands', (table) => { + table.index('moduleId'); + }); + await knex.schema.alterTable('hooks', (table) => { + table.index('moduleId'); + }); + await knex.schema.alterTable('permission', (table) => { + table.index('moduleId'); + }); + await knex.schema.alterTable('cronJobs', (table) => { + table.index('moduleId'); + }); +} + +export async function down(knex: Knex): Promise { + await knex.schema.alterTable('functions', (table) => { + table.dropIndex('moduleId'); + }); + await knex.schema.alterTable('commands', (table) => { + table.dropIndex('moduleId'); + }); + await knex.schema.alterTable('hooks', (table) => { + table.dropIndex('moduleId'); + }); + await knex.schema.alterTable('permission', (table) => { + table.dropIndex('moduleId'); + }); + await knex.schema.alterTable('cronJobs', (table) => { + table.dropIndex('moduleId'); + }); +} From fc7df660efded69405a94f50cad6b6d85d783298 Mon Sep 17 00:00:00 2001 From: Niek Candaele Date: Sun, 13 Oct 2024 20:25:27 +0200 Subject: [PATCH 2/2] fix: some more performance indexes --- ...20241013173159-add-indexen-for-moduleId.ts | 48 +++++++++++++------ 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/packages/lib-db/src/migrations/sql/20241013173159-add-indexen-for-moduleId.ts b/packages/lib-db/src/migrations/sql/20241013173159-add-indexen-for-moduleId.ts index 920ea62845..33bc003162 100644 --- a/packages/lib-db/src/migrations/sql/20241013173159-add-indexen-for-moduleId.ts +++ b/packages/lib-db/src/migrations/sql/20241013173159-add-indexen-for-moduleId.ts @@ -1,37 +1,57 @@ import { Knex } from 'knex'; +// This follows some research done with https://github.com/ankane/pghero + export async function up(knex: Knex): Promise { - await knex.schema.alterTable('functions', (table) => { - table.index('moduleId'); + // Remove duplicate indexes + await knex.schema.alterTable('events', (table) => { + table.dropIndex('domain', 'events_domain_index'); + }); + await knex.schema.alterTable('items', (table) => { + table.dropIndex('code', 'items_code_index'); + }); + await knex.schema.alterTable('players', (table) => { + table.dropIndex('domain', 'players_domain_index'); }); + + // Add indexes await knex.schema.alterTable('commands', (table) => { table.index('moduleId'); }); - await knex.schema.alterTable('hooks', (table) => { - table.index('moduleId'); + await knex.schema.alterTable('events', (table) => { + table.index(['domain', 'createdAt']); }); - await knex.schema.alterTable('permission', (table) => { + await knex.schema.alterTable('functions', (table) => { table.index('moduleId'); }); - await knex.schema.alterTable('cronJobs', (table) => { - table.index('moduleId'); + await knex.schema.alterTable('playerOnGameServer', (table) => { + table.index(['gameServerId', 'domain']); }); } export async function down(knex: Knex): Promise { - await knex.schema.alterTable('functions', (table) => { - table.dropIndex('moduleId'); + // Re-add removed indexes (if needed) + await knex.schema.alterTable('events', (table) => { + table.index('domain', 'events_domain_index'); + }); + await knex.schema.alterTable('items', (table) => { + table.index('code', 'items_code_index'); + }); + await knex.schema.alterTable('players', (table) => { + table.index('domain', 'players_domain_index'); }); + + // Drop added indexes await knex.schema.alterTable('commands', (table) => { table.dropIndex('moduleId'); }); - await knex.schema.alterTable('hooks', (table) => { - table.dropIndex('moduleId'); + await knex.schema.alterTable('events', (table) => { + table.dropIndex(['domain', 'createdAt']); }); - await knex.schema.alterTable('permission', (table) => { + await knex.schema.alterTable('functions', (table) => { table.dropIndex('moduleId'); }); - await knex.schema.alterTable('cronJobs', (table) => { - table.dropIndex('moduleId'); + await knex.schema.alterTable('playerOnGameServer', (table) => { + table.dropIndex(['gameServerId', 'domain']); }); }