Skip to content

Commit bf5f0e6

Browse files
authored
🗂 Add a couple of missing covering indexes (#501)
1 parent b913bc2 commit bf5f0e6

File tree

4 files changed

+679
-654
lines changed

4 files changed

+679
-654
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"""Add covering indexes
2+
3+
Revision ID: 056b595a6a00
4+
Revises: 156d8781d7b8
5+
Create Date: 2024-07-27 10:09:23.909935
6+
7+
"""
8+
9+
import sqlalchemy as sa
10+
11+
from alembic import op
12+
13+
# revision identifiers, used by Alembic.
14+
revision = "056b595a6a00"
15+
down_revision = "156d8781d7b8"
16+
branch_labels = None
17+
depends_on = None
18+
19+
20+
def upgrade():
21+
# ### commands auto generated by Alembic - please adjust! ###
22+
op.create_index(
23+
"ix_events_service",
24+
"events",
25+
["service_account_id"],
26+
unique=False,
27+
postgresql_where=sa.text("service_account_id IS NOT NULL"),
28+
)
29+
op.create_index(
30+
"ix_labelset_service_account_id",
31+
"labelsets",
32+
["labelled_by_sa_id"],
33+
unique=False,
34+
postgresql_where=sa.text("labelled_by_sa_id IS NOT NULL"),
35+
)
36+
op.create_index(
37+
"ix_labelset_user_id",
38+
"labelsets",
39+
["labelled_by_user_id"],
40+
unique=False,
41+
postgresql_where=sa.text("labelled_by_user_id IS NOT NULL"),
42+
)
43+
# ### end Alembic commands ###
44+
45+
46+
def downgrade():
47+
# ### commands auto generated by Alembic - please adjust! ###
48+
op.drop_index(
49+
"ix_labelset_user_id",
50+
table_name="labelsets",
51+
postgresql_where=sa.text("labelled_by_user_id IS NOT NULL"),
52+
)
53+
op.drop_index(
54+
"ix_labelset_service_account_id",
55+
table_name="labelsets",
56+
postgresql_where=sa.text("labelled_by_sa_id IS NOT NULL"),
57+
)
58+
op.drop_index(
59+
"ix_events_service",
60+
table_name="events",
61+
postgresql_where=sa.text("service_account_id IS NOT NULL"),
62+
)
63+
# ### end Alembic commands ###

app/models/event.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,6 @@ def description(self):
6565
"User", back_populates="events", foreign_keys=[user_id]
6666
)
6767

68-
# Partial indexes for school and user events
69-
70-
__table_args__ = (
71-
Index("ix_events_school", "school_id", postgresql_where=school_id.is_not(None)),
72-
Index("ix_events_user", "user_id", postgresql_where=user_id.is_not(None)),
73-
# Index("ix_events_info_work_id", "info", postgresql_where=info.has.is_not(None)),
74-
)
75-
7668
service_account_id: Mapped[Optional[uuid.UUID]] = mapped_column(
7769
ForeignKey("service_accounts.id", name="fk_event_service_account"),
7870
nullable=True,
@@ -81,6 +73,18 @@ def description(self):
8173
"ServiceAccount", foreign_keys=[service_account_id], back_populates="events"
8274
)
8375

76+
# Partial covering indexes for school, user, and service account foreign relations
77+
__table_args__ = (
78+
Index("ix_events_school", "school_id", postgresql_where=school_id.is_not(None)),
79+
Index("ix_events_user", "user_id", postgresql_where=user_id.is_not(None)),
80+
Index(
81+
"ix_events_service",
82+
"service_account_id",
83+
postgresql_where=service_account_id.is_not(None),
84+
),
85+
# Index("ix_events_info_work_id", "info", postgresql_where=info.has.is_not(None)),
86+
)
87+
8488
def __repr__(self):
8589
return f"<Event {self.title} - {self.description}>"
8690

app/models/labelset.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,20 @@ class LabelSet(Base):
133133
checked: Mapped[bool] = mapped_column(Boolean(), nullable=True)
134134
checked_at: Mapped[datetime] = mapped_column(DateTime, nullable=True)
135135

136+
# Partial covering indexes for labeller foreign relations
137+
__table_args__ = (
138+
Index(
139+
"ix_labelset_user_id",
140+
"labelled_by_user_id",
141+
postgresql_where=labelled_by_user_id.is_not(None),
142+
),
143+
Index(
144+
"ix_labelset_service_account_id",
145+
"labelled_by_sa_id",
146+
postgresql_where=labelled_by_sa_id.is_not(None),
147+
),
148+
)
149+
136150
def __repr__(self):
137151
return f"<LabelSet id={self.id} - '{self.work.title}' ages: {self.min_age}-{self.max_age} >"
138152

0 commit comments

Comments
 (0)