Skip to content

Commit

Permalink
Index Academics join table columns that back common queries (#497)
Browse files Browse the repository at this point in the history
  • Loading branch information
KrisJordan authored Jun 8, 2024
1 parent ce41dd1 commit 1960d11
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 8 deletions.
18 changes: 17 additions & 1 deletion backend/entities/academics/section_entity.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Definition of SQLAlchemy table-backed object mapping entity for Course Sections."""

from typing import Self
from sqlalchemy import Integer, String, ForeignKey
from sqlalchemy import Integer, String, ForeignKey, Index
from sqlalchemy.orm import Mapped, mapped_column, relationship
from ..entity_base import EntityBase
from ..section_application_table import section_application_table
Expand All @@ -19,6 +19,22 @@ class SectionEntity(EntityBase):
# Name for the course section table in the PostgreSQL database
__tablename__ = "academics__section"

# Add indexes to the database for fast, common lookup queries
__table_args__ = (
Index(
"ix_academics__section__by_term",
"term_id",
"course_id",
unique=False,
),
Index(
"ix_academics__section__by_course",
"course_id",
"term_id",
unique=False,
),
)

# Section properties (columns in the database table)

# Unique ID for the section
Expand Down
26 changes: 19 additions & 7 deletions backend/entities/academics/section_member_entity.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""Definition of SQLAlchemy table-backed object mapping entity for the user - section association table."""

from typing import Self
from sqlalchemy import ForeignKey, Integer
from sqlalchemy import ForeignKey, Integer, Index
from sqlalchemy import Enum as SQLAlchemyEnum
from sqlalchemy.orm import Mapped, mapped_column, relationship

from backend.entities.office_hours import user_created_tickets_table
from backend.models.academics.section_member_details import SectionMemberDetails
from ..office_hours import user_created_tickets_table
from ...models.academics.section_member_details import SectionMemberDetails

from ...models.roster_role import RosterRole
from ...models.academics.section_member import SectionMember, SectionMemberDraft
Expand Down Expand Up @@ -37,6 +37,22 @@ class SectionMemberEntity(EntityBase):
# Name for the user section table in the PostgreSQL database
__tablename__ = "academics__user_section"

# Add indexes to the database for fast, common lookup queries
__table_args__ = (
Index(
"ix_academics__user_section__by_user",
"user_id",
"section_id",
unique=True,
),
Index(
"ix_academics__user_section__by_section",
"section_id",
"member_role",
unique=False,
),
)

# User Section properties (columns in the database table)

# Unique ID for a user's membership in an academic section
Expand Down Expand Up @@ -65,10 +81,6 @@ class SectionMemberEntity(EntityBase):
back_populates="caller", cascade="all, delete"
)

application_id: Mapped[int] = mapped_column(
ForeignKey("application.id"), nullable=True
)

def to_flat_model(self) -> SectionMember:
"""
Converts a `SectionEntity` object into a `SectionMember` model object
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
"""Adds Indexes to Academics for common query patterns.
Revision ID: 87dd1109e7b2
Revises: eb379629af4f
Create Date: 2024-06-08 07:17:36.113064
"""

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "87dd1109e7b2"
down_revision = "eb379629af4f"
branch_labels = None
depends_on = None


def upgrade() -> None:
# Add indices to the academics__section and academics__user_section tables for common query patterns.
op.create_index(
"ix_academics__section__by_course",
"academics__section",
["course_id", "term_id"],
unique=False,
)
op.create_index(
"ix_academics__section__by_term",
"academics__section",
["term_id", "course_id"],
unique=False,
)

op.create_index(
"ix_academics__user_section__by_section",
"academics__user_section",
["section_id", "member_role"],
unique=False,
)
op.create_index(
"ix_academics__user_section__by_user",
"academics__user_section",
["user_id", "section_id"],
unique=True,
)

# Remove the foreign key to a TA application from UserSection. It will go somewhere else eventually.
op.drop_constraint(
"academics__user_section_application_id_fkey",
"academics__user_section",
type_="foreignkey",
)
op.drop_column("academics__user_section", "application_id")


def downgrade() -> None:
op.add_column(
"academics__user_section",
sa.Column("application_id", sa.INTEGER(), autoincrement=False, nullable=True),
)
op.create_foreign_key(
"academics__user_section_application_id_fkey",
"academics__user_section",
"application",
["application_id"],
["id"],
)
op.drop_index(
"ix_academics__user_section__by_user", table_name="academics__user_section"
)
op.drop_index(
"ix_academics__user_section__by_section", table_name="academics__user_section"
)
op.drop_index("ix_academics__section__by_term", table_name="academics__section")
op.drop_index("ix_academics__section__by_course", table_name="academics__section")

0 comments on commit 1960d11

Please sign in to comment.