Skip to content

Commit ecd0181

Browse files
authored
Add missing indexes for commonly used Foreign Keys (#10545)
* Add missing indexes for commonly used Foreign Keys These aren't automatically created, so for many of these large tables we are doing sequential scans when accessing the relation * lint
1 parent eb241f9 commit ecd0181

File tree

5 files changed

+127
-2
lines changed

5 files changed

+127
-2
lines changed

warehouse/accounts/models.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ class WebAuthn(db.Model):
188188
UUID(as_uuid=True),
189189
ForeignKey("users.id", deferrable=True, initially="DEFERRED"),
190190
nullable=False,
191+
index=True,
191192
)
192193
label = Column(String, nullable=False)
193194
credential_id = Column(String, unique=True, nullable=False)
@@ -202,6 +203,7 @@ class RecoveryCode(db.Model):
202203
UUID(as_uuid=True),
203204
ForeignKey("users.id", deferrable=True, initially="DEFERRED"),
204205
nullable=False,
206+
index=True,
205207
)
206208
code = Column(String(length=128), nullable=False)
207209
generated = Column(DateTime, nullable=False, server_default=sql.func.now())
@@ -214,6 +216,7 @@ class UserEvent(db.Model):
214216
UUID(as_uuid=True),
215217
ForeignKey("users.id", deferrable=True, initially="DEFERRED"),
216218
nullable=False,
219+
index=True,
217220
)
218221
tag = Column(String, nullable=False)
219222
time = Column(DateTime, nullable=False, server_default=sql.func.now())

warehouse/integrations/vulnerabilities/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"release_id",
2424
ForeignKey("releases.id", onupdate="CASCADE", ondelete="CASCADE"),
2525
nullable=False,
26+
index=True,
2627
),
2728
Column(
2829
"vulnerability_source",

warehouse/macaroons/models.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ class Macaroon(db.Model):
4242
# All of our Macaroons belong to a specific user, because a caveat-less
4343
# Macaroon should act the same as their password does, instead of as a
4444
# global permission to upload files.
45-
user_id = Column(UUID(as_uuid=True), ForeignKey("users.id"), nullable=False)
45+
user_id = Column(
46+
UUID(as_uuid=True), ForeignKey("users.id"), nullable=False, index=True
47+
)
4648

4749
# Store some information about the Macaroon to give users some mechanism
4850
# to differentiate between them.
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License");
2+
# you may not use this file except in compliance with the License.
3+
# You may obtain a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS,
9+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
# See the License for the specific language governing permissions and
11+
# limitations under the License.
12+
"""
13+
create missing FK indexes
14+
15+
Revision ID: 1b97443dea8a
16+
Revises: d18d443f89f0
17+
Create Date: 2022-01-03 20:52:20.615419
18+
"""
19+
20+
from alembic import op
21+
22+
revision = "1b97443dea8a"
23+
down_revision = "d18d443f89f0"
24+
25+
26+
def upgrade():
27+
# CREATE INDEX CONCURRENTLY cannot happen inside a transaction. We'll close
28+
# our transaction here and issue the statement.
29+
op.execute("COMMIT")
30+
31+
op.create_index(
32+
op.f("ix_macaroons_user_id"),
33+
"macaroons",
34+
["user_id"],
35+
unique=False,
36+
postgresql_concurrently=True,
37+
)
38+
op.create_index(
39+
op.f("ix_project_events_project_id"),
40+
"project_events",
41+
["project_id"],
42+
unique=False,
43+
postgresql_concurrently=True,
44+
)
45+
op.create_index(
46+
op.f("ix_release_vulnerabilities_release_id"),
47+
"release_vulnerabilities",
48+
["release_id"],
49+
unique=False,
50+
postgresql_concurrently=True,
51+
)
52+
op.create_index(
53+
op.f("ix_releases_description_id"),
54+
"releases",
55+
["description_id"],
56+
unique=False,
57+
postgresql_concurrently=True,
58+
)
59+
op.create_index(
60+
op.f("ix_role_invitations_project_id"),
61+
"role_invitations",
62+
["project_id"],
63+
unique=False,
64+
postgresql_concurrently=True,
65+
)
66+
op.create_index(
67+
op.f("ix_role_invitations_user_id"),
68+
"role_invitations",
69+
["user_id"],
70+
unique=False,
71+
postgresql_concurrently=True,
72+
)
73+
op.create_index(
74+
op.f("ix_user_events_user_id"),
75+
"user_events",
76+
["user_id"],
77+
unique=False,
78+
postgresql_concurrently=True,
79+
)
80+
op.create_index(
81+
op.f("ix_user_recovery_codes_user_id"),
82+
"user_recovery_codes",
83+
["user_id"],
84+
unique=False,
85+
postgresql_concurrently=True,
86+
)
87+
op.create_index(
88+
op.f("ix_user_security_keys_user_id"),
89+
"user_security_keys",
90+
["user_id"],
91+
unique=False,
92+
postgresql_concurrently=True,
93+
)
94+
95+
96+
def downgrade():
97+
# ### commands auto generated by Alembic - please adjust! ###
98+
op.drop_index(
99+
op.f("ix_user_security_keys_user_id"), table_name="user_security_keys"
100+
)
101+
op.drop_index(
102+
op.f("ix_user_recovery_codes_user_id"), table_name="user_recovery_codes"
103+
)
104+
op.drop_index(op.f("ix_user_events_user_id"), table_name="user_events")
105+
op.drop_index(op.f("ix_role_invitations_user_id"), table_name="role_invitations")
106+
op.drop_index(op.f("ix_role_invitations_project_id"), table_name="role_invitations")
107+
op.drop_index(op.f("ix_releases_description_id"), table_name="releases")
108+
op.drop_index(
109+
op.f("ix_release_vulnerabilities_release_id"),
110+
table_name="release_vulnerabilities",
111+
)
112+
op.drop_index(op.f("ix_project_events_project_id"), table_name="project_events")
113+
op.drop_index(op.f("ix_macaroons_user_id"), table_name="macaroons")
114+
# ### end Alembic commands ###

warehouse/packaging/models.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,14 @@ class RoleInvitation(db.Model):
105105
)
106106
token = Column(Text, nullable=False)
107107
user_id = Column(
108-
ForeignKey("users.id", onupdate="CASCADE", ondelete="CASCADE"), nullable=False
108+
ForeignKey("users.id", onupdate="CASCADE", ondelete="CASCADE"),
109+
nullable=False,
110+
index=True,
109111
)
110112
project_id = Column(
111113
ForeignKey("projects.id", onupdate="CASCADE", ondelete="CASCADE"),
112114
nullable=False,
115+
index=True,
113116
)
114117

115118
user = orm.relationship(User, lazy=False)
@@ -279,6 +282,7 @@ class ProjectEvent(db.Model):
279282
"projects.id", deferrable=True, initially="DEFERRED", ondelete="CASCADE"
280283
),
281284
nullable=False,
285+
index=True,
282286
)
283287
tag = Column(String, nullable=False)
284288
time = Column(DateTime, nullable=False, server_default=sql.func.now())
@@ -380,6 +384,7 @@ def __table_args__(cls): # noqa
380384
description_id = Column(
381385
ForeignKey("release_descriptions.id", onupdate="CASCADE", ondelete="CASCADE"),
382386
nullable=False,
387+
index=True,
383388
)
384389
description = orm.relationship(
385390
"Description",

0 commit comments

Comments
 (0)