perf(core): Make execution queries faster (#9817)

This commit is contained in:
Iván Ovejero
2024-08-22 13:27:45 +02:00
committed by GitHub
parent 3428f28a73
commit dc7dc995d5
11 changed files with 167 additions and 12 deletions
@@ -0,0 +1,118 @@
import type { MigrationContext, ReversibleMigration } from '@/databases/types';
/**
* Add new indices:
*
* - `workflowId, startedAt` for `ExecutionRepository.findManyByRangeQuery` (default query) and for `ExecutionRepository.findManyByRangeQuery` (filter query)
* - `waitTill, status, deletedAt` for `ExecutionRepository.getWaitingExecutions`
* - `stoppedAt, status, deletedAt` for `ExecutionRepository.softDeletePrunableExecutions`
*
* Remove unused indices in sqlite:
*
* - `stoppedAt` (duplicate with different casing)
* - `waitTill`
* - `status, workflowId`
*
* Remove unused indices in MySQL:
*
* - `status`
*
* Remove unused indices in all DBs:
*
* - `waitTill, id`
* - `workflowId, id`
*
* Remove incomplete index in all DBs:
*
* - `stopped_at` (replaced with composite index)
*
* Keep index as is:
*
* - `deletedAt` for query at `ExecutionRepository.hardDeleteSoftDeletedExecutions`
*/
export class RefactorExecutionIndices1723796243146 implements ReversibleMigration {
async up({ schemaBuilder, isPostgres, isSqlite, isMysql, runQuery, escape }: MigrationContext) {
if (isSqlite || isPostgres) {
const executionEntity = escape.tableName('execution_entity');
const workflowId = escape.columnName('workflowId');
const startedAt = escape.columnName('startedAt');
const waitTill = escape.columnName('waitTill');
const status = escape.columnName('status');
const deletedAt = escape.columnName('deletedAt');
const stoppedAt = escape.columnName('stoppedAt');
await runQuery(`
CREATE INDEX idx_execution_entity_workflow_id_started_at
ON ${executionEntity} (${workflowId}, ${startedAt})
WHERE ${startedAt} IS NOT NULL AND ${deletedAt} IS NULL;
`);
await runQuery(`
CREATE INDEX idx_execution_entity_wait_till_status_deleted_at
ON ${executionEntity} (${waitTill}, ${status}, ${deletedAt})
WHERE ${waitTill} IS NOT NULL AND ${deletedAt} IS NULL;
`);
await runQuery(`
CREATE INDEX idx_execution_entity_stopped_at_status_deleted_at
ON ${executionEntity} (${stoppedAt}, ${status}, ${deletedAt})
WHERE ${stoppedAt} IS NOT NULL AND ${deletedAt} IS NULL;
`);
} else if (isMysql) {
await schemaBuilder.createIndex('execution_entity', ['workflowId', 'startedAt']);
await schemaBuilder.createIndex('execution_entity', ['waitTill', 'status', 'deletedAt']);
await schemaBuilder.createIndex('execution_entity', ['stoppedAt', 'status', 'deletedAt']);
}
if (isSqlite) {
await schemaBuilder.dropIndex('execution_entity', ['waitTill'], {
customIndexName: 'idx_execution_entity_wait_till',
skipIfMissing: true,
});
await schemaBuilder.dropIndex('execution_entity', ['status', 'workflowId'], {
customIndexName: 'IDX_8b6f3f9ae234f137d707b98f3bf43584',
skipIfMissing: true,
});
}
if (isMysql) {
await schemaBuilder.dropIndex('execution_entity', ['status'], {
customIndexName: 'IDX_8b6f3f9ae234f137d707b98f3bf43584',
skipIfMissing: true,
});
}
// all DBs
await schemaBuilder.dropIndex(
'execution_entity',
['stoppedAt'],
isSqlite ? { customIndexName: 'idx_execution_entity_stopped_at', skipIfMissing: true } : {},
);
await schemaBuilder.dropIndex('execution_entity', ['waitTill', 'id'], {
customIndexName: isPostgres
? 'IDX_85b981df7b444f905f8bf50747'
: 'IDX_b94b45ce2c73ce46c54f20b5f9',
skipIfMissing: true,
});
await schemaBuilder.dropIndex('execution_entity', ['workflowId', 'id'], {
customIndexName:
isPostgres || isMysql
? 'idx_execution_entity_workflow_id_id'
: 'IDX_81fc04c8a17de15835713505e4',
skipIfMissing: true,
});
}
async down({ schemaBuilder }: MigrationContext) {
await schemaBuilder.dropIndex('execution_entity', ['workflowId', 'startedAt']);
await schemaBuilder.dropIndex('execution_entity', ['waitTill', 'status']);
await schemaBuilder.dropIndex('execution_entity', ['stoppedAt', 'deletedAt', 'status']);
await schemaBuilder.createIndex('execution_entity', ['waitTill', 'id']);
await schemaBuilder.createIndex('execution_entity', ['stoppedAt']);
await schemaBuilder.createIndex('execution_entity', ['workflowId', 'id']);
}
}
@@ -58,6 +58,7 @@ import { MoveSshKeysToDatabase1711390882123 } from '../common/1711390882123-Move
import { RemoveNodesAccess1712044305787 } from '../common/1712044305787-RemoveNodesAccess';
import { MakeExecutionStatusNonNullable1714133768521 } from '../common/1714133768521-MakeExecutionStatusNonNullable';
import { AddActivatedAtUserSetting1717498465931 } from './1717498465931-AddActivatedAtUserSetting';
import { RefactorExecutionIndices1723796243146 } from '../common/1723796243146-RefactorExecutionIndices';
import { AddConstraintToExecutionMetadata1720101653148 } from '../common/1720101653148-AddConstraintToExecutionMetadata';
import { CreateInvalidAuthTokenTable1723627610222 } from '../common/1723627610222-CreateInvalidAuthTokenTable';
@@ -123,4 +124,5 @@ export const mysqlMigrations: Migration[] = [
AddActivatedAtUserSetting1717498465931,
AddConstraintToExecutionMetadata1720101653148,
CreateInvalidAuthTokenTable1723627610222,
RefactorExecutionIndices1723796243146,
];
@@ -57,6 +57,7 @@ import { MoveSshKeysToDatabase1711390882123 } from '../common/1711390882123-Move
import { RemoveNodesAccess1712044305787 } from '../common/1712044305787-RemoveNodesAccess';
import { MakeExecutionStatusNonNullable1714133768521 } from '../common/1714133768521-MakeExecutionStatusNonNullable';
import { AddActivatedAtUserSetting1717498465931 } from './1717498465931-AddActivatedAtUserSetting';
import { RefactorExecutionIndices1723796243146 } from '../common/1723796243146-RefactorExecutionIndices';
import { AddConstraintToExecutionMetadata1720101653148 } from '../common/1720101653148-AddConstraintToExecutionMetadata';
import { FixExecutionMetadataSequence1721377157740 } from './1721377157740-FixExecutionMetadataSequence';
import { CreateInvalidAuthTokenTable1723627610222 } from '../common/1723627610222-CreateInvalidAuthTokenTable';
@@ -123,4 +124,5 @@ export const postgresMigrations: Migration[] = [
AddConstraintToExecutionMetadata1720101653148,
FixExecutionMetadataSequence1721377157740,
CreateInvalidAuthTokenTable1723627610222,
RefactorExecutionIndices1723796243146,
];
@@ -55,6 +55,7 @@ import { MoveSshKeysToDatabase1711390882123 } from '../common/1711390882123-Move
import { RemoveNodesAccess1712044305787 } from '../common/1712044305787-RemoveNodesAccess';
import { MakeExecutionStatusNonNullable1714133768521 } from '../common/1714133768521-MakeExecutionStatusNonNullable';
import { AddActivatedAtUserSetting1717498465931 } from './1717498465931-AddActivatedAtUserSetting';
import { RefactorExecutionIndices1723796243146 } from '../common/1723796243146-RefactorExecutionIndices';
import { AddConstraintToExecutionMetadata1720101653148 } from '../common/1720101653148-AddConstraintToExecutionMetadata';
import { CreateInvalidAuthTokenTable1723627610222 } from '../common/1723627610222-CreateInvalidAuthTokenTable';
@@ -117,6 +118,7 @@ const sqliteMigrations: Migration[] = [
AddActivatedAtUserSetting1717498465931,
AddConstraintToExecutionMetadata1720101653148,
CreateInvalidAuthTokenTable1723627610222,
RefactorExecutionIndices1723796243146,
];
export { sqliteMigrations };