Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(core): Make execution queries faster #9817

Merged
merged 20 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions packages/cli/src/databases/dsl/Indices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,18 @@ abstract class IndexOperation extends LazyPromise<void> {
protected tablePrefix: string,
queryRunner: QueryRunner,
protected customIndexName?: string,
protected skipIfMissing = false,
ivov marked this conversation as resolved.
Show resolved Hide resolved
) {
super((resolve) => {
void this.execute(queryRunner).then(resolve);
super((resolve, reject) => {
void this.execute(queryRunner)
.then(resolve)
.catch((error) => {
if (this.skipIfMissing) {
resolve();
} else {
reject(error);
}
});
});
}
}
Expand Down
10 changes: 8 additions & 2 deletions packages/cli/src/databases/dsl/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,14 @@ export const createSchemaBuilder = (tablePrefix: string, queryRunner: QueryRunne
customIndexName?: string,
) => new CreateIndex(tableName, columnNames, isUnique, tablePrefix, queryRunner, customIndexName),

dropIndex: (tableName: string, columnNames: string[], customIndexName?: string) =>
new DropIndex(tableName, columnNames, tablePrefix, queryRunner, customIndexName),
dropIndex: (
tableName: string,
columnNames: string[],
{ customIndexName, skipIfMissing }: { customIndexName?: string; skipIfMissing?: boolean } = {
skipIfMissing: false,
},
) =>
new DropIndex(tableName, columnNames, tablePrefix, queryRunner, customIndexName, skipIfMissing),

addForeignKey: (
tableName: string,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import type { MigrationContext, ReversibleMigration } from '@/databases/types';

export class RefactorExecutionIndices1717498465932 implements ReversibleMigration {
async up({ schemaBuilder, isPostgres, isSqlite }: MigrationContext) {
await schemaBuilder.dropIndex(
'execution_entity',
['waitTill', 'id'], // covered by `waitTill, status` - `id` is unused
{
customIndexName: isPostgres
? 'IDX_85b981df7b444f905f8bf50747'
: 'IDX_b94b45ce2c73ce46c54f20b5f9',
skipIfMissing: true,
},
);

if (isSqlite) {
await schemaBuilder.dropIndex('execution_entity', ['stoppedAt'], {
ivov marked this conversation as resolved.
Show resolved Hide resolved
customIndexName: 'idx_execution_entity_stopped_at', // duplicate of `IDX_execution_entity_stoppedAt`
ivov marked this conversation as resolved.
Show resolved Hide resolved
skipIfMissing: true,
});
}

await schemaBuilder.createIndex(
'execution_entity',
['status', 'startedAt'], // for default query at `GET /executions`
);

await schemaBuilder.createIndex(
'execution_entity',
['workflowId', 'status', 'startedAt'], // for query with filters at `GET /executions`
);

await schemaBuilder.createIndex(
'execution_entity',
['waitTill', 'status'], // for waiting executions queries on interval
);
}

async down({ schemaBuilder }: MigrationContext) {
await schemaBuilder.dropIndex('execution_entity', ['status', 'startedAt']);
await schemaBuilder.dropIndex('execution_entity', ['workflowId', 'status', 'startedAt']);
await schemaBuilder.dropIndex('execution_entity', ['waitTill', 'status']);

await schemaBuilder.createIndex('execution_entity', ['waitTill', 'id']);
}
}
2 changes: 2 additions & 0 deletions packages/cli/src/databases/migrations/mysqldb/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 { RefactorExecutionIndices1717498465932 } from '../common/1717498465932-RefactorExecutionIndices';

export const mysqlMigrations: Migration[] = [
InitialMigration1588157391238,
Expand Down Expand Up @@ -119,4 +120,5 @@ export const mysqlMigrations: Migration[] = [
CreateProject1714133768519,
MakeExecutionStatusNonNullable1714133768521,
AddActivatedAtUserSetting1717498465931,
RefactorExecutionIndices1717498465932,
];
2 changes: 2 additions & 0 deletions packages/cli/src/databases/migrations/postgresdb/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 { RefactorExecutionIndices1717498465932 } from '../common/1717498465932-RefactorExecutionIndices';

export const postgresMigrations: Migration[] = [
InitialMigration1587669153312,
Expand Down Expand Up @@ -117,4 +118,5 @@ export const postgresMigrations: Migration[] = [
CreateProject1714133768519,
MakeExecutionStatusNonNullable1714133768521,
AddActivatedAtUserSetting1717498465931,
RefactorExecutionIndices1717498465932,
];
2 changes: 2 additions & 0 deletions packages/cli/src/databases/migrations/sqlite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 { RefactorExecutionIndices1717498465932 } from '../common/1717498465932-RefactorExecutionIndices';

const sqliteMigrations: Migration[] = [
InitialMigration1588102412422,
Expand Down Expand Up @@ -113,6 +114,7 @@ const sqliteMigrations: Migration[] = [
CreateProject1714133768519,
MakeExecutionStatusNonNullable1714133768521,
AddActivatedAtUserSetting1717498465931,
RefactorExecutionIndices1717498465932,
];

export { sqliteMigrations };
Original file line number Diff line number Diff line change
Expand Up @@ -735,8 +735,8 @@ export class ExecutionRepository extends Repository<ExecutionEntity> {
if (firstId) qb.andWhere('execution.id > :firstId', { firstId });
if (lastId) qb.andWhere('execution.id < :lastId', { lastId });

if (query.order?.stoppedAt === 'DESC') {
qb.orderBy({ 'execution.stoppedAt': 'DESC' });
if (query.order?.startedAt === 'DESC') {
qb.orderBy({ 'execution.startedAt': 'DESC' });
} else if (query.order?.top) {
qb.orderBy(`(CASE WHEN execution.status = '${query.order.top}' THEN 0 ELSE 1 END)`);
} else {
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/databases/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export interface MigrationContext {
tablePrefix: string;
dbType: DatabaseType;
isMysql: boolean;
isSqlite: boolean;
isPostgres: boolean;
dbName: string;
migrationName: string;
nodeTypes: INodeTypes;
Expand Down
4 changes: 4 additions & 0 deletions packages/cli/src/databases/utils/migrationHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ function parseJson<T>(data: string | T): T {

const dbType = config.getEnv('database.type');
const isMysql = ['mariadb', 'mysqldb'].includes(dbType);
const isSqlite = dbType === 'sqlite';
const isPostgres = dbType === 'postgresdb';
const dbName = config.getEnv(`database.${dbType === 'mariadb' ? 'mysqldb' : dbType}.database`);
const tablePrefix = config.getEnv('database.tablePrefix');

Expand All @@ -99,6 +101,8 @@ const createContext = (queryRunner: QueryRunner, migration: Migration): Migratio
tablePrefix,
dbType,
isMysql,
isSqlite,
isPostgres,
dbName,
migrationName: migration.name,
queryRunner,
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/executions/execution.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ export class ExecutionService {
/**
* Return:
*
* - the latest summaries of current and completed executions that satisfy a query,
* - the summaries of latest current and completed executions that satisfy a query,
* - the total count of all completed executions that satisfy the query, and
* - whether the total of completed executions is an estimate.
*
Expand All @@ -389,7 +389,7 @@ export class ExecutionService {
this.findRangeWithCount({
...query,
status: completedStatuses,
order: { stoppedAt: 'DESC' },
order: { startedAt: 'DESC' },
}),
]);

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/executions/execution.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export namespace ExecutionSummaries {
type OrderFields = {
order?: {
top?: ExecutionStatus;
stoppedAt?: 'DESC';
startedAt?: 'DESC';
};
};
}
Expand Down