Skip to content

Commit bd56b0b

Browse files
authored
feat: add index to Provenance.file_id (#17677)
When used as a JOIN condition in `legacy.api.json.release` for the release's files, the `provenance` table is consulted for any records related to the `file_id`. Currently unindexed, this is a sequential scan of the table of ~164k rows, and ~87% of the total query cost. Add an index to what is effectively a primary key to prevent further slowdown as the provenance table grows with more projects publishing. Signed-off-by: Mike Fiedler <[email protected]>
1 parent 034bc3f commit bd56b0b

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

warehouse/attestations/models.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import pypi_attestations
2020

21-
from sqlalchemy import ForeignKey, orm
21+
from sqlalchemy import ForeignKey, Index, orm
2222
from sqlalchemy.dialects.postgresql import JSONB
2323
from sqlalchemy.orm import Mapped, mapped_column
2424

@@ -51,3 +51,5 @@ class Provenance(db.Model):
5151
@cached_property
5252
def as_model(self):
5353
return pypi_attestations.Provenance.model_validate(self.provenance)
54+
55+
__table_args__ = (Index("ix_provenance_file_id", file_id),)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
Add index to Provenance.file_id
14+
15+
Revision ID: 635b80625fc9
16+
Revises: 2f5dbc74c770
17+
Create Date: 2025-02-28 17:41:58.763011
18+
"""
19+
20+
from alembic import op
21+
22+
revision = "635b80625fc9"
23+
down_revision = "2f5dbc74c770"
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.get_bind().commit()
30+
with op.get_context().autocommit_block():
31+
op.create_index(
32+
"ix_provenance_file_id",
33+
"provenance",
34+
["file_id"],
35+
unique=False,
36+
postgresql_concurrently=True,
37+
)
38+
39+
40+
def downgrade():
41+
op.drop_index(
42+
"ix_provenance_file_id", table_name="provenance", postgresql_concurrently=True
43+
)

warehouse/migrations/versions/7f0c9f105f44_create_attestations_table.py

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# limitations under the License.
1212
"""
1313
create Attestations table
14+
1415
Revision ID: 7f0c9f105f44
1516
Revises: 26455e3712a2
1617
Create Date: 2024-07-25 15:49:01.993869

0 commit comments

Comments
 (0)