forked from mozilla/pontoon
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize filters by introducing Resource.order (mozilla#3130)
String filters can be slow, especially because ordering entities by Resource.path is slow. It turns out that: - Adding DB index to Resource.path doesn't bring performance gains. - Sorting entities by any other Resource.field is fast. The reason for slowness is probably that Resource.path can be a pretty long text field. So we're now introducing Resource.order, which to represent the alphabetic order of resources in the project. We reorder resources when new resources are added to the project.
- Loading branch information
Showing
6 changed files
with
100 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated by Django 3.2.24 on 2024-03-08 14:27 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("base", "0053_alter_translation_index_together"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="resource", | ||
name="order", | ||
field=models.PositiveIntegerField(default=0), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Generated by Django 3.2.24 on 2024-03-08 14:29 | ||
|
||
from django.db import migrations | ||
|
||
|
||
def populate_resource_order(apps, schema_editor): | ||
Project = apps.get_model("base", "Project") | ||
Resource = apps.get_model("base", "Resource") | ||
projects = Project.objects.all() | ||
|
||
ordered_resources = [] | ||
|
||
for p in projects.prefetch_related("resources"): | ||
resources = p.resources.all().order_by("path") | ||
for idx, r in enumerate(resources): | ||
r.order = idx | ||
ordered_resources.append(r) | ||
|
||
Resource.objects.bulk_update(ordered_resources, ["order"]) | ||
|
||
|
||
def reset_resource_order(apps, schema_editor): | ||
Resource = apps.get_model("base", "Resource") | ||
Resource.objects.update(order=0) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("base", "0054_resource_order"), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython( | ||
code=populate_resource_order, | ||
reverse_code=reset_resource_order, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters