From 9d115ce8dc26af47c33d762c6cf0f4b4256d63a8 Mon Sep 17 00:00:00 2001 From: Tristan Slater <1631008+trslater@users.noreply.github.com> Date: Wed, 28 Feb 2024 16:58:50 -0800 Subject: [PATCH] Remove redundant join from pub NOI sub search view This should speed it up substantially. --- ...lic-notice-of-intent-search-view.entity.ts | 1 - ...b_noi_search_view_remove_redundant_join.ts | 51 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 services/apps/alcs/src/providers/typeorm/migrations/1709167790743-pub_noi_search_view_remove_redundant_join.ts diff --git a/services/apps/alcs/src/portal/public/search/notice-of-intent/public-notice-of-intent-search-view.entity.ts b/services/apps/alcs/src/portal/public/search/notice-of-intent/public-notice-of-intent-search-view.entity.ts index bb20e03786..a21c214987 100644 --- a/services/apps/alcs/src/portal/public/search/notice-of-intent/public-notice-of-intent-search-view.entity.ts +++ b/services/apps/alcs/src/portal/public/search/notice-of-intent/public-notice-of-intent-search-view.entity.ts @@ -68,7 +68,6 @@ import { LinkedStatusType } from '../public-search.dto'; .leftJoin( (qb) => qb - .from(NoticeOfIntentDecision, 'decision_date') .select('decisiondate', 'date') .addSelect('outcome', 'outcome') .addSelect('dest_rank', 'dest_rank') diff --git a/services/apps/alcs/src/providers/typeorm/migrations/1709167790743-pub_noi_search_view_remove_redundant_join.ts b/services/apps/alcs/src/providers/typeorm/migrations/1709167790743-pub_noi_search_view_remove_redundant_join.ts new file mode 100644 index 0000000000..2d25624abd --- /dev/null +++ b/services/apps/alcs/src/providers/typeorm/migrations/1709167790743-pub_noi_search_view_remove_redundant_join.ts @@ -0,0 +1,51 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +export class PubNoiSearchViewRemoveRedundantJoin1709167790743 + implements MigrationInterface +{ + name = 'PubNoiSearchViewRemoveRedundantJoin1709167790743'; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `DELETE FROM "alcs"."typeorm_metadata" WHERE "type" = $1 AND "name" = $2 AND "schema" = $3`, + ['VIEW', 'public_notice_of_intent_submission_search_view', 'alcs'], + ); + await queryRunner.query( + `DROP VIEW "alcs"."public_notice_of_intent_submission_search_view"`, + ); + await queryRunner.query( + `CREATE VIEW "alcs"."public_notice_of_intent_submission_search_view" AS SELECT "noi_sub"."uuid" AS "uuid", "noi_sub"."applicant" AS "applicant", "noi"."uuid" AS "notice_of_intent_uuid", "noticeOfIntentType"."audit_deleted_date_at" AS "noticeOfIntentType_audit_deleted_date_at", "noticeOfIntentType"."audit_created_at" AS "noticeOfIntentType_audit_created_at", "noticeOfIntentType"."audit_updated_at" AS "noticeOfIntentType_audit_updated_at", "noticeOfIntentType"."audit_created_by" AS "noticeOfIntentType_audit_created_by", "noticeOfIntentType"."audit_updated_by" AS "noticeOfIntentType_audit_updated_by", "noticeOfIntentType"."label" AS "noticeOfIntentType_label", "noticeOfIntentType"."code" AS "noticeOfIntentType_code", "noticeOfIntentType"."description" AS "noticeOfIntentType_description", "noticeOfIntentType"."short_label" AS "noticeOfIntentType_short_label", "noticeOfIntentType"."background_color" AS "noticeOfIntentType_background_color", "noticeOfIntentType"."text_color" AS "noticeOfIntentType_text_color", "noticeOfIntentType"."html_description" AS "noticeOfIntentType_html_description", "noticeOfIntentType"."portal_label" AS "noticeOfIntentType_portal_label", "noticeOfIntentType"."alc_fee_amount" AS "noticeOfIntentType_alc_fee_amount", "noticeOfIntentType"."government_fee_amount" AS "noticeOfIntentType_government_fee_amount", "localGovernment"."name" AS "local_government_name", "noi_sub"."file_number" AS "file_number", "noi_sub"."local_government_uuid" AS "local_government_uuid", "noi_sub"."type_code" AS "notice_of_intent_type_code", "noi"."date_submitted_to_alc" AS "date_submitted_to_alc", "noi"."decision_date" AS "decision_date", decision_date.outcome AS "outcome", decision_date.dest_rank AS "dest_rank", "noi"."region_code" AS "notice_of_intent_region_code", GREATEST(status_link.effective_date, decision_date.date) AS "last_update", alcs.get_current_status_for_notice_of_intent_submission_by_uuid("noi_sub"."uuid") AS "status" FROM "alcs"."notice_of_intent_submission" "noi_sub" INNER JOIN "alcs"."notice_of_intent" "noi" ON "noi"."file_number" = "noi_sub"."file_number" AND "noi"."hide_from_portal" = FALSE AND "noi"."audit_deleted_date_at" IS NULL INNER JOIN "alcs"."notice_of_intent_type" "noticeOfIntentType" ON "noi_sub"."type_code" = "noticeOfIntentType"."code" AND "noticeOfIntentType"."audit_deleted_date_at" IS NULL LEFT JOIN "alcs"."local_government" "localGovernment" ON "noi_sub"."local_government_uuid" = "localGovernment"."uuid" AND "localGovernment"."audit_deleted_date_at" IS NULL LEFT JOIN (SELECT MAX("effective_date") AS "effective_date", submission_uuid AS "submission_uuid" FROM "alcs"."notice_of_intent_submission_to_submission_status" "status_link" GROUP BY submission_uuid) "status_link" ON status_link."submission_uuid" = "noi_sub"."uuid" LEFT JOIN (SELECT DISTINCT ON (notice_of_intentuuid) decisiondate AS "date", outcome AS "outcome", dest_rank AS "dest_rank", notice_of_intentuuid AS "notice_of_intent_uuid" FROM (SELECT outcome_code AS "outcome", date AS "decisiondate", notice_of_intent_uuid AS "notice_of_intentuuid", RANK() OVER (PARTITION BY notice_of_intent_uuid ORDER BY date DESC, audit_created_at DESC) AS "dest_rank" FROM "alcs"."notice_of_intent_decision" "decision" WHERE "decision"."audit_deleted_date_at" IS NULL) "decisions" WHERE dest_rank = 1) "decision_date" ON decision_date."notice_of_intent_uuid" = "noi"."uuid" WHERE ( "noi_sub"."is_draft" = FALSE AND ("noi"."date_received_all_items" IS NOT NULL AND "noi"."date_received_all_items" <= NOW()) AND alcs.get_current_status_for_notice_of_intent_submission_by_uuid("noi_sub"."uuid")->>'status_type_code' != 'CANC' ) AND ( "noi_sub"."audit_deleted_date_at" IS NULL )`, + ); + await queryRunner.query( + `INSERT INTO "alcs"."typeorm_metadata"("database", "schema", "table", "type", "name", "value") VALUES (DEFAULT, $1, DEFAULT, $2, $3, $4)`, + [ + 'alcs', + 'VIEW', + 'public_notice_of_intent_submission_search_view', + 'SELECT "noi_sub"."uuid" AS "uuid", "noi_sub"."applicant" AS "applicant", "noi"."uuid" AS "notice_of_intent_uuid", "noticeOfIntentType"."audit_deleted_date_at" AS "noticeOfIntentType_audit_deleted_date_at", "noticeOfIntentType"."audit_created_at" AS "noticeOfIntentType_audit_created_at", "noticeOfIntentType"."audit_updated_at" AS "noticeOfIntentType_audit_updated_at", "noticeOfIntentType"."audit_created_by" AS "noticeOfIntentType_audit_created_by", "noticeOfIntentType"."audit_updated_by" AS "noticeOfIntentType_audit_updated_by", "noticeOfIntentType"."label" AS "noticeOfIntentType_label", "noticeOfIntentType"."code" AS "noticeOfIntentType_code", "noticeOfIntentType"."description" AS "noticeOfIntentType_description", "noticeOfIntentType"."short_label" AS "noticeOfIntentType_short_label", "noticeOfIntentType"."background_color" AS "noticeOfIntentType_background_color", "noticeOfIntentType"."text_color" AS "noticeOfIntentType_text_color", "noticeOfIntentType"."html_description" AS "noticeOfIntentType_html_description", "noticeOfIntentType"."portal_label" AS "noticeOfIntentType_portal_label", "noticeOfIntentType"."alc_fee_amount" AS "noticeOfIntentType_alc_fee_amount", "noticeOfIntentType"."government_fee_amount" AS "noticeOfIntentType_government_fee_amount", "localGovernment"."name" AS "local_government_name", "noi_sub"."file_number" AS "file_number", "noi_sub"."local_government_uuid" AS "local_government_uuid", "noi_sub"."type_code" AS "notice_of_intent_type_code", "noi"."date_submitted_to_alc" AS "date_submitted_to_alc", "noi"."decision_date" AS "decision_date", decision_date.outcome AS "outcome", decision_date.dest_rank AS "dest_rank", "noi"."region_code" AS "notice_of_intent_region_code", GREATEST(status_link.effective_date, decision_date.date) AS "last_update", alcs.get_current_status_for_notice_of_intent_submission_by_uuid("noi_sub"."uuid") AS "status" FROM "alcs"."notice_of_intent_submission" "noi_sub" INNER JOIN "alcs"."notice_of_intent" "noi" ON "noi"."file_number" = "noi_sub"."file_number" AND "noi"."hide_from_portal" = FALSE AND "noi"."audit_deleted_date_at" IS NULL INNER JOIN "alcs"."notice_of_intent_type" "noticeOfIntentType" ON "noi_sub"."type_code" = "noticeOfIntentType"."code" AND "noticeOfIntentType"."audit_deleted_date_at" IS NULL LEFT JOIN "alcs"."local_government" "localGovernment" ON "noi_sub"."local_government_uuid" = "localGovernment"."uuid" AND "localGovernment"."audit_deleted_date_at" IS NULL LEFT JOIN (SELECT MAX("effective_date") AS "effective_date", submission_uuid AS "submission_uuid" FROM "alcs"."notice_of_intent_submission_to_submission_status" "status_link" GROUP BY submission_uuid) "status_link" ON status_link."submission_uuid" = "noi_sub"."uuid" LEFT JOIN (SELECT DISTINCT ON (notice_of_intentuuid) decisiondate AS "date", outcome AS "outcome", dest_rank AS "dest_rank", notice_of_intentuuid AS "notice_of_intent_uuid" FROM (SELECT outcome_code AS "outcome", date AS "decisiondate", notice_of_intent_uuid AS "notice_of_intentuuid", RANK() OVER (PARTITION BY notice_of_intent_uuid ORDER BY date DESC, audit_created_at DESC) AS "dest_rank" FROM "alcs"."notice_of_intent_decision" "decision" WHERE "decision"."audit_deleted_date_at" IS NULL) "decisions" WHERE dest_rank = 1) "decision_date" ON decision_date."notice_of_intent_uuid" = "noi"."uuid" WHERE ( "noi_sub"."is_draft" = FALSE AND ("noi"."date_received_all_items" IS NOT NULL AND "noi"."date_received_all_items" <= NOW()) AND alcs.get_current_status_for_notice_of_intent_submission_by_uuid("noi_sub"."uuid")->>\'status_type_code\' != \'CANC\' ) AND ( "noi_sub"."audit_deleted_date_at" IS NULL )', + ], + ); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `DELETE FROM "alcs"."typeorm_metadata" WHERE "type" = $1 AND "name" = $2 AND "schema" = $3`, + ['VIEW', 'public_notice_of_intent_submission_search_view', 'alcs'], + ); + await queryRunner.query( + `DROP VIEW "alcs"."public_notice_of_intent_submission_search_view"`, + ); + await queryRunner.query( + `CREATE VIEW "alcs"."public_notice_of_intent_submission_search_view" AS SELECT "noi_sub"."uuid" AS "uuid", "noi_sub"."applicant" AS "applicant", "noi"."uuid" AS "notice_of_intent_uuid", "noticeOfIntentType"."audit_deleted_date_at" AS "noticeOfIntentType_audit_deleted_date_at", "noticeOfIntentType"."audit_created_at" AS "noticeOfIntentType_audit_created_at", "noticeOfIntentType"."audit_updated_at" AS "noticeOfIntentType_audit_updated_at", "noticeOfIntentType"."audit_created_by" AS "noticeOfIntentType_audit_created_by", "noticeOfIntentType"."audit_updated_by" AS "noticeOfIntentType_audit_updated_by", "noticeOfIntentType"."label" AS "noticeOfIntentType_label", "noticeOfIntentType"."code" AS "noticeOfIntentType_code", "noticeOfIntentType"."description" AS "noticeOfIntentType_description", "noticeOfIntentType"."short_label" AS "noticeOfIntentType_short_label", "noticeOfIntentType"."background_color" AS "noticeOfIntentType_background_color", "noticeOfIntentType"."text_color" AS "noticeOfIntentType_text_color", "noticeOfIntentType"."html_description" AS "noticeOfIntentType_html_description", "noticeOfIntentType"."portal_label" AS "noticeOfIntentType_portal_label", "noticeOfIntentType"."alc_fee_amount" AS "noticeOfIntentType_alc_fee_amount", "noticeOfIntentType"."government_fee_amount" AS "noticeOfIntentType_government_fee_amount", "localGovernment"."name" AS "local_government_name", "noi_sub"."file_number" AS "file_number", "noi_sub"."local_government_uuid" AS "local_government_uuid", "noi_sub"."type_code" AS "notice_of_intent_type_code", "noi"."date_submitted_to_alc" AS "date_submitted_to_alc", "noi"."decision_date" AS "decision_date", decision_date.outcome AS "outcome", decision_date.dest_rank AS "dest_rank", "noi"."region_code" AS "notice_of_intent_region_code", GREATEST(status_link.effective_date, decision_date.date) AS "last_update", alcs.get_current_status_for_notice_of_intent_submission_by_uuid("noi_sub"."uuid") AS "status" FROM "alcs"."notice_of_intent_submission" "noi_sub" INNER JOIN "alcs"."notice_of_intent" "noi" ON "noi"."file_number" = "noi_sub"."file_number" AND "noi"."hide_from_portal" = FALSE AND "noi"."audit_deleted_date_at" IS NULL INNER JOIN "alcs"."notice_of_intent_type" "noticeOfIntentType" ON "noi_sub"."type_code" = "noticeOfIntentType"."code" AND "noticeOfIntentType"."audit_deleted_date_at" IS NULL LEFT JOIN "alcs"."local_government" "localGovernment" ON "noi_sub"."local_government_uuid" = "localGovernment"."uuid" AND "localGovernment"."audit_deleted_date_at" IS NULL LEFT JOIN (SELECT MAX("effective_date") AS "effective_date", submission_uuid AS "submission_uuid" FROM "alcs"."notice_of_intent_submission_to_submission_status" "status_link" GROUP BY submission_uuid) "status_link" ON status_link."submission_uuid" = "noi_sub"."uuid" LEFT JOIN (SELECT DISTINCT ON (notice_of_intentuuid) decisiondate AS "date", outcome AS "outcome", dest_rank AS "dest_rank", notice_of_intentuuid AS "notice_of_intent_uuid" FROM "alcs"."notice_of_intent_decision" "decision_date", (SELECT outcome_code AS "outcome", date AS "decisiondate", notice_of_intent_uuid AS "notice_of_intentuuid", RANK() OVER (PARTITION BY notice_of_intent_uuid ORDER BY date DESC, audit_created_at DESC) AS "dest_rank" FROM "alcs"."notice_of_intent_decision" "decision" WHERE "decision"."audit_deleted_date_at" IS NULL) "decisions" WHERE dest_rank = 1) "decision_date" ON decision_date."notice_of_intent_uuid" = "noi"."uuid" WHERE ( "noi_sub"."is_draft" = FALSE AND ("noi"."date_received_all_items" IS NOT NULL AND "noi"."date_received_all_items" <= NOW()) AND alcs.get_current_status_for_notice_of_intent_submission_by_uuid("noi_sub"."uuid")->>'status_type_code' != 'CANC' ) AND ( "noi_sub"."audit_deleted_date_at" IS NULL )`, + ); + await queryRunner.query( + `INSERT INTO "alcs"."typeorm_metadata"("database", "schema", "table", "type", "name", "value") VALUES (DEFAULT, $1, DEFAULT, $2, $3, $4)`, + [ + 'alcs', + 'VIEW', + 'public_notice_of_intent_submission_search_view', + 'SELECT "noi_sub"."uuid" AS "uuid", "noi_sub"."applicant" AS "applicant", "noi"."uuid" AS "notice_of_intent_uuid", "noticeOfIntentType"."audit_deleted_date_at" AS "noticeOfIntentType_audit_deleted_date_at", "noticeOfIntentType"."audit_created_at" AS "noticeOfIntentType_audit_created_at", "noticeOfIntentType"."audit_updated_at" AS "noticeOfIntentType_audit_updated_at", "noticeOfIntentType"."audit_created_by" AS "noticeOfIntentType_audit_created_by", "noticeOfIntentType"."audit_updated_by" AS "noticeOfIntentType_audit_updated_by", "noticeOfIntentType"."label" AS "noticeOfIntentType_label", "noticeOfIntentType"."code" AS "noticeOfIntentType_code", "noticeOfIntentType"."description" AS "noticeOfIntentType_description", "noticeOfIntentType"."short_label" AS "noticeOfIntentType_short_label", "noticeOfIntentType"."background_color" AS "noticeOfIntentType_background_color", "noticeOfIntentType"."text_color" AS "noticeOfIntentType_text_color", "noticeOfIntentType"."html_description" AS "noticeOfIntentType_html_description", "noticeOfIntentType"."portal_label" AS "noticeOfIntentType_portal_label", "noticeOfIntentType"."alc_fee_amount" AS "noticeOfIntentType_alc_fee_amount", "noticeOfIntentType"."government_fee_amount" AS "noticeOfIntentType_government_fee_amount", "localGovernment"."name" AS "local_government_name", "noi_sub"."file_number" AS "file_number", "noi_sub"."local_government_uuid" AS "local_government_uuid", "noi_sub"."type_code" AS "notice_of_intent_type_code", "noi"."date_submitted_to_alc" AS "date_submitted_to_alc", "noi"."decision_date" AS "decision_date", decision_date.outcome AS "outcome", decision_date.dest_rank AS "dest_rank", "noi"."region_code" AS "notice_of_intent_region_code", GREATEST(status_link.effective_date, decision_date.date) AS "last_update", alcs.get_current_status_for_notice_of_intent_submission_by_uuid("noi_sub"."uuid") AS "status" FROM "alcs"."notice_of_intent_submission" "noi_sub" INNER JOIN "alcs"."notice_of_intent" "noi" ON "noi"."file_number" = "noi_sub"."file_number" AND "noi"."hide_from_portal" = FALSE AND "noi"."audit_deleted_date_at" IS NULL INNER JOIN "alcs"."notice_of_intent_type" "noticeOfIntentType" ON "noi_sub"."type_code" = "noticeOfIntentType"."code" AND "noticeOfIntentType"."audit_deleted_date_at" IS NULL LEFT JOIN "alcs"."local_government" "localGovernment" ON "noi_sub"."local_government_uuid" = "localGovernment"."uuid" AND "localGovernment"."audit_deleted_date_at" IS NULL LEFT JOIN (SELECT MAX("effective_date") AS "effective_date", submission_uuid AS "submission_uuid" FROM "alcs"."notice_of_intent_submission_to_submission_status" "status_link" GROUP BY submission_uuid) "status_link" ON status_link."submission_uuid" = "noi_sub"."uuid" LEFT JOIN (SELECT DISTINCT ON (notice_of_intentuuid) decisiondate AS "date", outcome AS "outcome", dest_rank AS "dest_rank", notice_of_intentuuid AS "notice_of_intent_uuid" FROM "alcs"."notice_of_intent_decision" "decision_date", (SELECT outcome_code AS "outcome", date AS "decisiondate", notice_of_intent_uuid AS "notice_of_intentuuid", RANK() OVER (PARTITION BY notice_of_intent_uuid ORDER BY date DESC, audit_created_at DESC) AS "dest_rank" FROM "alcs"."notice_of_intent_decision" "decision" WHERE "decision"."audit_deleted_date_at" IS NULL) "decisions" WHERE dest_rank = 1) "decision_date" ON decision_date."notice_of_intent_uuid" = "noi"."uuid" WHERE ( "noi_sub"."is_draft" = FALSE AND ("noi"."date_received_all_items" IS NOT NULL AND "noi"."date_received_all_items" <= NOW()) AND alcs.get_current_status_for_notice_of_intent_submission_by_uuid("noi_sub"."uuid")->>\'status_type_code\' != \'CANC\' ) AND ( "noi_sub"."audit_deleted_date_at" IS NULL )', + ], + ); + } +}