Skip to content

Commit

Permalink
Add database indexes on relations to improve query performance (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
omar-selo authored Dec 4, 2024
1 parent 4f23b04 commit f4264d4
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
"""Add indexes on relations that don't have them
Revision ID: b3b376fb6353
Revises: 063e32aac8db
Create Date: 2024-12-03 13:02:41.364193+00:00
"""
from alembic import op

# revision identifiers, used by Alembic.
revision = "b3b376fb6353"
down_revision = "063e32aac8db"
branch_labels = None
depends_on = None


def upgrade() -> None:
op.create_index(
op.f("artefact_assignee_id_ix"),
"artefact",
["assignee_id"],
unique=False,
)
op.create_index(
op.f("artefact_stage_id_ix"),
"artefact",
["stage_id"],
unique=False,
)
op.create_index(
op.f("artefact_build_artefact_id_ix"),
"artefact_build",
["artefact_id"],
unique=False,
)
op.create_index(
op.f("artefact_build_environment_review_artefact_build_id_ix"),
"artefact_build_environment_review",
["artefact_build_id"],
unique=False,
)
op.create_index(
op.f("artefact_build_environment_review_environment_id_ix"),
"artefact_build_environment_review",
["environment_id"],
unique=False,
)
op.create_index(
op.f("stage_family_id_ix"),
"stage",
["family_id"],
unique=False,
)
op.create_index(
op.f("test_event_test_execution_id_ix"),
"test_event",
["test_execution_id"],
unique=False,
)
op.create_index(
op.f("test_execution_artefact_build_id_ix"),
"test_execution",
["artefact_build_id"],
unique=False,
)
op.create_index(
op.f("test_execution_environment_id_ix"),
"test_execution",
["environment_id"],
unique=False,
)


def downgrade() -> None:
op.drop_index(op.f("test_execution_environment_id_ix"), table_name="test_execution")
op.drop_index(
op.f("test_execution_artefact_build_id_ix"), table_name="test_execution"
)
op.drop_index(op.f("test_event_test_execution_id_ix"), table_name="test_event")
op.drop_index(op.f("stage_family_id_ix"), table_name="stage")
op.drop_index(
op.f("artefact_build_environment_review_environment_id_ix"),
table_name="artefact_build_environment_review",
)
op.drop_index(
op.f("artefact_build_environment_review_artefact_build_id_ix"),
table_name="artefact_build_environment_review",
)
op.drop_index(op.f("artefact_build_artefact_id_ix"), table_name="artefact_build")
op.drop_index(op.f("artefact_stage_id_ix"), table_name="artefact")
op.drop_index(op.f("artefact_assignee_id_ix"), table_name="artefact")
26 changes: 17 additions & 9 deletions backend/test_observer/data_access/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ class Stage(Base):
name: Mapped[str] = mapped_column(String(100), index=True)
position: Mapped[int] = mapped_column()
# Relationships
family_id: Mapped[int] = mapped_column(ForeignKey("family.id", ondelete="CASCADE"))
family_id: Mapped[int] = mapped_column(
ForeignKey("family.id", ondelete="CASCADE"), index=True
)
family: Mapped[Family] = relationship(back_populates="stages")
artefacts: Mapped[list["Artefact"]] = relationship(
back_populates="stage", cascade="all, delete"
Expand All @@ -153,12 +155,16 @@ class Artefact(Base):
series: Mapped[str] = mapped_column(default="")
repo: Mapped[str] = mapped_column(default="")
# Relationships
stage_id: Mapped[int] = mapped_column(ForeignKey("stage.id", ondelete="CASCADE"))
stage_id: Mapped[int] = mapped_column(
ForeignKey("stage.id", ondelete="CASCADE"), index=True
)
stage: Mapped[Stage] = relationship(back_populates="artefacts")
builds: Mapped[list["ArtefactBuild"]] = relationship(
back_populates="artefact", cascade="all, delete"
)
assignee_id: Mapped[int | None] = mapped_column(ForeignKey("app_user.id"))
assignee_id: Mapped[int | None] = mapped_column(
ForeignKey("app_user.id"), index=True
)
assignee: Mapped[User | None] = relationship(back_populates="assignments")
# Default fields
due_date: Mapped[date | None] = mapped_column(default=determine_due_date)
Expand Down Expand Up @@ -241,7 +247,7 @@ class ArtefactBuild(Base):
revision: Mapped[int | None]
# Relationships
artefact_id: Mapped[int] = mapped_column(
ForeignKey("artefact.id", ondelete="CASCADE")
ForeignKey("artefact.id", ondelete="CASCADE"), index=True
)
artefact: Mapped[Artefact] = relationship(
back_populates="builds", foreign_keys=[artefact_id]
Expand Down Expand Up @@ -330,12 +336,14 @@ class TestExecution(Base):
c3_link: Mapped[str | None] = mapped_column(String(200), nullable=True)
# Relationships
artefact_build_id: Mapped[int] = mapped_column(
ForeignKey("artefact_build.id", ondelete="CASCADE")
ForeignKey("artefact_build.id", ondelete="CASCADE"), index=True
)
artefact_build: Mapped["ArtefactBuild"] = relationship(
back_populates="test_executions"
)
environment_id: Mapped[int] = mapped_column(ForeignKey("environment.id"))
environment_id: Mapped[int] = mapped_column(
ForeignKey("environment.id"), index=True
)
environment: Mapped["Environment"] = relationship(back_populates="test_executions")
test_results: Mapped[list["TestResult"]] = relationship(
back_populates="test_execution", cascade="all, delete"
Expand Down Expand Up @@ -435,7 +443,7 @@ class TestEvent(Base):
timestamp: Mapped[datetime]
detail: Mapped[str]
test_execution_id: Mapped[int] = mapped_column(
ForeignKey("test_execution.id", ondelete="CASCADE")
ForeignKey("test_execution.id", ondelete="CASCADE"), index=True
)
test_execution: Mapped["TestExecution"] = relationship(back_populates="test_events")

Expand Down Expand Up @@ -508,12 +516,12 @@ class ArtefactBuildEnvironmentReview(Base):
review_comment: Mapped[str] = mapped_column(default="")

environment_id: Mapped[int] = mapped_column(
ForeignKey("environment.id", ondelete="CASCADE")
ForeignKey("environment.id", ondelete="CASCADE"), index=True
)
environment: Mapped["Environment"] = relationship()

artefact_build_id: Mapped[int] = mapped_column(
ForeignKey("artefact_build.id", ondelete="CASCADE")
ForeignKey("artefact_build.id", ondelete="CASCADE"), index=True
)
artefact_build: Mapped["ArtefactBuild"] = relationship(
back_populates="environment_reviews",
Expand Down

0 comments on commit f4264d4

Please sign in to comment.