From 683fe331e4a6d5a9d85f0092237b44b9a5ded79e Mon Sep 17 00:00:00 2001 From: arpitnath Date: Mon, 21 Aug 2023 12:50:42 +0530 Subject: [PATCH] Added indexes to the components table on columns name, type, and page_id to optimize query performance. --- .../1691006952074-CreateComponentTable.ts | 16 ++++++++++++++-- server/src/entities/component.entity.ts | 3 ++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/server/migrations/1691006952074-CreateComponentTable.ts b/server/migrations/1691006952074-CreateComponentTable.ts index 2016b55eb2..2e23406680 100644 --- a/server/migrations/1691006952074-CreateComponentTable.ts +++ b/server/migrations/1691006952074-CreateComponentTable.ts @@ -1,4 +1,4 @@ -import { MigrationInterface, QueryRunner, Table, TableForeignKey } from 'typeorm'; +import { MigrationInterface, QueryRunner, Table, TableForeignKey, TableIndex } from 'typeorm'; export class CreateComponentTable1691006952074 implements MigrationInterface { public async up(queryRunner: QueryRunner): Promise { @@ -46,7 +46,6 @@ export class CreateComponentTable1691006952074 implements MigrationInterface { }) ); - // Add foreign key to relate Component with Page await queryRunner.createForeignKey( 'components', new TableForeignKey({ @@ -56,9 +55,22 @@ export class CreateComponentTable1691006952074 implements MigrationInterface { onDelete: 'CASCADE', }) ); + + await queryRunner.createIndex('components', new TableIndex({ columnNames: ['name'] })); + await queryRunner.createIndex('components', new TableIndex({ columnNames: ['type'] })); + await queryRunner.createIndex('components', new TableIndex({ columnNames: ['page_id'] })); } public async down(queryRunner: QueryRunner): Promise { + // Drop indexes + await queryRunner.dropIndex('components', 'IDX_COMPONENT_NAME'); + await queryRunner.dropIndex('components', 'IDX_COMPONENT_TYPE'); + await queryRunner.dropIndex('components', 'IDX_COMPONENT_PAGE'); + + // Drop foreign key + await queryRunner.dropForeignKey('components', 'FK_COMPONENT_PAGE'); + + // Drop table await queryRunner.dropTable('components'); } } diff --git a/server/src/entities/component.entity.ts b/server/src/entities/component.entity.ts index b32d4fae80..2c5127cd72 100644 --- a/server/src/entities/component.entity.ts +++ b/server/src/entities/component.entity.ts @@ -1,8 +1,9 @@ -import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, OneToMany, JoinColumn } from 'typeorm'; +import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, OneToMany, JoinColumn, Index } from 'typeorm'; import { Page } from './page.entity'; import { Layout } from './layout.entity'; @Entity({ name: 'components' }) +@Index('idx_component_page_id', ['pageId']) export class Component { @PrimaryGeneratedColumn('uuid') id: string;