Skip to content

Commit

Permalink
fix: add an index to FMR.date_modified
Browse files Browse the repository at this point in the history
`/oai-pmh/?verb=Identify` reports the "earliest datestamp" based on the
FormattedMetadataRecord with the earliest `date_modified` -- but that
query can take > 30 seconds, because there is no index.

add an index.
  • Loading branch information
aaxelb committed May 21, 2021
1 parent 61177eb commit d4d87fa
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
34 changes: 34 additions & 0 deletions share/migrations/0059_fmr_date_modified_index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):
atomic = False # CREATE INDEX CONCURRENTLY cannot be run in a txn

dependencies = [
('share', '0001_squashed_0058_big_rend'),
]

operations = [
migrations.SeparateDatabaseAndState(
state_operations=[
# tell Django we're adding an index
migrations.AddIndex(
model_name='formattedmetadatarecord',
index=models.Index(fields=['date_modified'], name=['fmr_date_modified_index']),
),
],
database_operations=[
# add the index without locking
# (if we were using Django 3, would use
# django.contrib.postgres.operations.AddIndexConcurrently)
migrations.RunSQL([
'CREATE INDEX CONCURRENTLY "fmr_date_modified_index" ON "share_formattedmetadatarecord" ("date_modified");',
], [
'DROP INDEX IF EXISTS "fmr_date_modified_index";'
])
]
)
]
3 changes: 3 additions & 0 deletions share/models/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ class JSONAPIMeta(BaseJSONAPIMeta):

class Meta:
unique_together = ('suid', 'record_format')
indexes = [
models.Index(fields=['date_modified'], name=['fmr_date_modified_index'])
]

def __repr__(self):
return f'<{self.__class__.__name__}({self.id}, {self.suid_id}, {self.record_format})>'

0 comments on commit d4d87fa

Please sign in to comment.