Add indexes on fields on which we order and vice versa

Triggered by issue_event_list being more than 5s on "emu" (my 1,500,000 event
test-machine). Reason: sorting those events on non-indexed field. Switching
to a field-with-index solved it.

I then analysed (grepped) for "ordering" and "order_by" and set indexes
accordingly and more or less indiscriminately (i.e. even on tables that are
assumed to have relatively few rows, such as Project & Team).
This commit is contained in:
Klaas van Schelven
2025-02-04 21:19:24 +01:00
parent 24e204edcc
commit 86e8c4318b
9 changed files with 95 additions and 4 deletions

View File

@@ -360,7 +360,7 @@ def issue_event_stacktrace(request, issue, event_pk=None, digest_order=None, nav
def issue_event_404(request, issue, tab, this_view):
"""If the Event is 404, but the issue is not, we can still show the issue page; we show a message for the event"""
last_event = issue.event_set.order_by("timestamp").last() # the template needs this for the tabs, we pick the last
last_event = issue.event_set.order_by("digest_order").last() # the template needs this for the tabs
return render(request, "issues/event_404.html", {
"tab": tab,
"this_view": this_view,
@@ -471,7 +471,7 @@ def issue_history(request, issue):
if request.method == "POST":
return _handle_post(request, issue)
last_event = issue.event_set.order_by("timestamp").last() # the template needs this for the tabs, we pick the last
last_event = issue.event_set.order_by("digest_order").last() # the template needs this for the tabs
return render(request, "issues/history.html", {
"tab": "history",
"project": issue.project,
@@ -489,7 +489,7 @@ def issue_grouping(request, issue):
if request.method == "POST":
return _handle_post(request, issue)
last_event = issue.event_set.order_by("timestamp").last() # the template needs this for the tabs, we pick the last
last_event = issue.event_set.order_by("digest_order").last() # the template needs this for the tabs
return render(request, "issues/grouping.html", {
"tab": "grouping",
"project": issue.project,
@@ -515,7 +515,7 @@ def issue_event_list(request, issue):
page_number = request.GET.get("page")
page_obj = paginator.get_page(page_number)
last_event = issue.event_set.order_by("timestamp").last() # the template needs this for the tabs, we pick the last
last_event = issue.event_set.order_by("digest_order").last() # the template needs this for the tabs
return render(request, "issues/event_list.html", {
"tab": "event-list",
"project": issue.project,

View File

@@ -0,0 +1,17 @@
# Generated by Django 4.2.18 on 2025-02-04 20:17
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("projects", "0002_b_squashed_initial"),
]
operations = [
migrations.AddIndex(
model_name="project",
index=models.Index(fields=["name"], name="projects_pr_name_11d782_idx"),
),
]

View File

@@ -114,6 +114,11 @@ class Project(models.Model):
def is_discoverable(self):
return self.visibility <= ProjectVisibility.DISCOVERABLE
class Meta:
indexes = [
models.Index(fields=["name"]),
]
class ProjectMembership(models.Model):
project = models.ForeignKey(Project, on_delete=models.CASCADE)

View File

@@ -0,0 +1,19 @@
# Generated by Django 4.2.18 on 2025-02-04 20:17
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("releases", "0001_initial"),
]
operations = [
migrations.AddIndex(
model_name="release",
index=models.Index(
fields=["sort_epoch"], name="releases_re_sort_ep_5c07c8_idx"
),
),
]

View File

@@ -83,6 +83,10 @@ class Release(models.Model):
class Meta:
unique_together = ("project", "version")
indexes = [
models.Index(fields=["sort_epoch"]),
]
def get_short_version(self):
if self.version == "":
# the reason for this little hack is to have something show up in the UI for this case. I 'assume' (mother

View File

@@ -0,0 +1,19 @@
# Generated by Django 4.2.18 on 2025-02-04 20:14
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("snappea", "0003_task_created_at"),
]
operations = [
migrations.AddIndex(
model_name="task",
index=models.Index(
fields=["created_at"], name="snappea_tas_created_eb0824_idx"
),
),
]

View File

@@ -15,6 +15,11 @@ class Task(models.Model):
def __str__(self):
return self.task_name
class Meta:
indexes = [
models.Index(fields=['created_at']),
]
def wakeup_server():
wakeup_file = os.path.join(get_settings().WAKEUP_CALLS_DIR, thread_uuid)

View File

@@ -0,0 +1,17 @@
# Generated by Django 4.2.18 on 2025-02-04 20:17
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("teams", "0001_b_squashed_initial"),
]
operations = [
migrations.AddIndex(
model_name="team",
index=models.Index(fields=["name"], name="teams_team_name_43e047_idx"),
),
]

View File

@@ -32,6 +32,11 @@ class Team(models.Model):
def is_joinable(self):
return self.visibility <= TeamVisibility.JOINABLE
class Meta:
indexes = [
models.Index(fields=["name"]),
]
class TeamMembership(models.Model):
team = models.ForeignKey(Team, on_delete=models.CASCADE)