Add missing index on viewField.viewFieldGroupId (#19253)
## Summary - Adds an index on `viewField.viewFieldGroupId` to fix a ~28s `DELETE FROM core.viewFieldGroup WHERE workspaceId = $1` query observed in production - The slow query is triggered during workspace hard deletion (`deleteWorkspaceSyncableMetadataEntities` in `workspace.service.ts`) - Root cause: the FK constraint `viewField.viewFieldGroupId → viewFieldGroup.id` with `ON DELETE SET NULL` forces PostgreSQL to sequentially scan the entire `viewField` table for every deleted `viewFieldGroup` row, because no index exists on the referencing column - Uses `CREATE INDEX CONCURRENTLY` in the migration to avoid locking the table on production
This commit is contained in:
+19
@@ -0,0 +1,19 @@
|
||||
import { type MigrationInterface, type QueryRunner } from 'typeorm';
|
||||
|
||||
export class AddViewFieldGroupIdIndexOnViewField1775129420309
|
||||
implements MigrationInterface
|
||||
{
|
||||
name = 'AddViewFieldGroupIdIndexOnViewField1775129420309';
|
||||
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(
|
||||
`CREATE INDEX IF NOT EXISTS "IDX_VIEW_FIELD_VIEW_FIELD_GROUP_ID" ON "core"."viewField" ("viewFieldGroupId")`,
|
||||
);
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(
|
||||
`DROP INDEX IF EXISTS "core"."IDX_VIEW_FIELD_VIEW_FIELD_GROUP_ID"`,
|
||||
);
|
||||
}
|
||||
}
|
||||
+1
@@ -32,6 +32,7 @@ export type ViewFieldOverrides = {
|
||||
@Index('IDX_VIEW_FIELD_WORKSPACE_ID_VIEW_ID', ['workspaceId', 'viewId'])
|
||||
@Index('IDX_VIEW_FIELD_VIEW_ID', ['viewId'])
|
||||
@Index('IDX_VIEW_FIELD_FIELD_METADATA_ID', ['fieldMetadataId'])
|
||||
@Index('IDX_VIEW_FIELD_VIEW_FIELD_GROUP_ID', ['viewFieldGroupId'])
|
||||
@Index(
|
||||
'IDX_VIEW_FIELD_FIELD_METADATA_ID_VIEW_ID_UNIQUE',
|
||||
['fieldMetadataId', 'viewId'],
|
||||
|
||||
Reference in New Issue
Block a user