From 309d7957c90857972f836c02c99a2448ba0dcef3 Mon Sep 17 00:00:00 2001 From: KevSanchez Date: Thu, 3 Nov 2022 18:50:52 +0100 Subject: [PATCH] perf(Impact View): Concurrent Refresh on impact view Makes the Impact Materialized View able to be refreshed concurrently by adding an unique index --- ...567675-ImpactMaterializedViewUniqueIndex.ts | 18 ++++++++++++++++++ .../indicator-record.entity.ts | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 api/src/migrations/1667497567675-ImpactMaterializedViewUniqueIndex.ts diff --git a/api/src/migrations/1667497567675-ImpactMaterializedViewUniqueIndex.ts b/api/src/migrations/1667497567675-ImpactMaterializedViewUniqueIndex.ts new file mode 100644 index 0000000000..ecc79a7d1b --- /dev/null +++ b/api/src/migrations/1667497567675-ImpactMaterializedViewUniqueIndex.ts @@ -0,0 +1,18 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +export class ImpactMaterializedViewUniqueIndex1667497567675 + implements MigrationInterface +{ + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(` + DROP INDEX IF EXISTS idx_georegion_h3data_indexed; + CREATE UNIQUE INDEX idx_georegion_h3data_indexed ON impact_materialized_view ("geoRegionId", "h3DataId", "h3index") INCLUDE ("value"); + `); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(` + DROP INDEX IF EXISTS idx_georegion_h3data_indexed; + `); + } +} diff --git a/api/src/modules/indicator-records/indicator-record.entity.ts b/api/src/modules/indicator-records/indicator-record.entity.ts index 46f96da14b..15d140821c 100644 --- a/api/src/modules/indicator-records/indicator-record.entity.ts +++ b/api/src/modules/indicator-records/indicator-record.entity.ts @@ -108,7 +108,7 @@ export class IndicatorRecord extends TimestampedBaseEntity { static updateImpactView(): Promise { return getManager().query( - `REFRESH MATERIALIZED VIEW ${IMPACT_VIEW_NAME} WITH DATA`, + `REFRESH MATERIALIZED VIEW CONCURRENTLY ${IMPACT_VIEW_NAME} WITH DATA`, ); } }