Skip to content

Commit

Permalink
Merge pull request #1216 from LibraryOfCongress/more-database-optimiz…
Browse files Browse the repository at this point in the history
…ations

WIP: Add more combined indexes for popular queries
  • Loading branch information
rstorey authored Mar 24, 2020
2 parents c630c93 + 9702366 commit cb0b7c4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
43 changes: 43 additions & 0 deletions concordia/migrations/0048_auto_20200324_1820.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Generated by Django 2.2.11 on 2020-03-24 22:20

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("concordia", "0047_auto_20200324_1103"),
]

operations = [
migrations.RemoveIndex(model_name="asset", name="concordia_a_id_0c37bf_idx",),
migrations.AlterField(
model_name="item",
name="published",
field=models.BooleanField(blank=True, default=False),
),
migrations.AlterField(
model_name="project",
name="published",
field=models.BooleanField(blank=True, default=False),
),
migrations.AddIndex(
model_name="asset",
index=models.Index(
fields=["id", "item", "published", "transcription_status"],
name="concordia_a_id_137ca8_idx",
),
),
migrations.AddIndex(
model_name="item",
index=models.Index(
fields=["project", "published"], name="concordia_i_project_d8caf0_idx"
),
),
migrations.AddIndex(
model_name="project",
index=models.Index(
fields=["id", "campaign", "published"], name="concordia_p_id_17c9c9_idx"
),
),
]
8 changes: 5 additions & 3 deletions concordia/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class Project(MetricsModelMixin("project"), models.Model):

campaign = models.ForeignKey(Campaign, on_delete=models.CASCADE)

published = models.BooleanField(default=False, blank=True, db_index=True)
published = models.BooleanField(default=False, blank=True)

title = models.CharField(max_length=80)
slug = models.SlugField(max_length=80, allow_unicode=True)
Expand All @@ -174,6 +174,7 @@ class Project(MetricsModelMixin("project"), models.Model):
class Meta:
unique_together = (("slug", "campaign"),)
ordering = ["title"]
indexes = [models.Index(fields=["id", "campaign", "published"])]

def __str__(self):
return self.title
Expand All @@ -190,7 +191,7 @@ class Item(MetricsModelMixin("item"), models.Model):

project = models.ForeignKey(Project, on_delete=models.CASCADE)

published = models.BooleanField(default=False, blank=True, db_index=True)
published = models.BooleanField(default=False, blank=True)

title = models.CharField(max_length=600)
item_url = models.URLField(max_length=255)
Expand All @@ -208,6 +209,7 @@ class Item(MetricsModelMixin("item"), models.Model):

class Meta:
unique_together = (("item_id", "project"),)
indexes = [models.Index(fields=["project", "published"])]

def __str__(self):
return f"{self.item_id}: {self.title}"
Expand Down Expand Up @@ -276,7 +278,7 @@ class Asset(MetricsModelMixin("asset"), models.Model):
class Meta:
unique_together = (("slug", "item"),)
indexes = [
models.Index(fields=["id", "published", "transcription_status"]),
models.Index(fields=["id", "item", "published", "transcription_status"]),
models.Index(fields=["published", "transcription_status"]),
]

Expand Down
8 changes: 6 additions & 2 deletions concordia/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1511,7 +1511,9 @@ def filter_and_order_transcribable_assets(
| Q(transcription_status=TranscriptionStatus.IN_PROGRESS)
)

potential_assets = potential_assets.filter(assettranscriptionreservation=None)
potential_assets = potential_assets.exclude(
pk__in=Subquery(AssetTranscriptionReservation.objects.values("asset_id"))
)
potential_assets = potential_assets.select_related("item", "item__project")

# We'll favor assets which are in the same item or project as the original:
Expand Down Expand Up @@ -1544,7 +1546,9 @@ def filter_and_order_reviewable_assets(
transcription_status=TranscriptionStatus.SUBMITTED
)
potential_assets = potential_assets.exclude(transcription__user=user_pk)
potential_assets = potential_assets.filter(assettranscriptionreservation=None)
potential_assets = potential_assets.exclude(
pk__in=Subquery(AssetTranscriptionReservation.objects.values("asset_id"))
)
potential_assets = potential_assets.select_related("item", "item__project")

# We'll favor assets which are in the same item or project as the original:
Expand Down

0 comments on commit cb0b7c4

Please sign in to comment.