Skip to content

Commit

Permalink
Create index to find non empty titles for assigments
Browse files Browse the repository at this point in the history
A big time of the query time of get_assignments is spent finding
assignments with not null titles.

Create an index on that condition trying to speed it up.
  • Loading branch information
marcospri committed Aug 21, 2024
1 parent c6eddbf commit 48470a5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""Index on assignment.title is not null."""

from alembic import op
import sqlalchemy as sa

revision = "b39108c0cd35"
down_revision = "3d0c022c716c"


def upgrade() -> None:
# CONCURRENTLY can't be used inside a transaction. Finish the current one.
op.execute("COMMIT")

op.create_index(
"ix__assignment_title_is_not_null",
"assignment",
["title"],
unique=False,
postgresql_where=sa.text("title IS NOT NULL"),
postgresql_concurrently=True,
)


def downgrade() -> None:
op.execute("COMMIT")

op.drop_index(
"ix__assignment_title_is_not_null",
table_name="assignment",
postgresql_where=sa.text("title IS NOT NULL"),
postgresql_concurrently=True,
)
12 changes: 9 additions & 3 deletions lms/models/assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ class Assignment(CreatedUpdatedMixin, Base):
"""

__tablename__ = "assignment"
__table_args__ = (
sa.UniqueConstraint("resource_link_id", "tool_consumer_instance_guid"),
)

id: Mapped[int] = mapped_column(autoincrement=True, primary_key=True)

Expand Down Expand Up @@ -91,6 +88,15 @@ class Assignment(CreatedUpdatedMixin, Base):

course: Mapped[Course | None] = relationship(Course)

__table_args__ = (
sa.UniqueConstraint("resource_link_id", "tool_consumer_instance_guid"),
sa.Index(
"ix__assignment_title_is_not_null",
"title",
postgresql_where=title.is_not(None),
),
)

def get_canvas_mapped_file_id(self, file_id):
return self.extra.get("canvas_file_mappings", {}).get(file_id, file_id)

Expand Down

0 comments on commit 48470a5

Please sign in to comment.