Skip to content

Commit 2c1be32

Browse files
authored
fix(groupowners): Add index on type and json commit id. (#89892)
We have an extremely slow query here: https://github.com/getsentry/sentry/blob/1f6701a4e13a45eb1aedae31e5a2cb41d80892f8/src/sentry/integrations/github/tasks/pr_comment.py#L79-L98 Both `type` and the json `commit` id column are not indexed. Adding an index here to manage this query. This produces an index like ``` CREATE INDEX CONCURRENTLY "groupowner_type_json_commitid" ON "sentry_groupowner" ("type", (((("context")::jsonb ->> 'commitId'))::bigint)); ``` Which *should* fix the problem, but we'll see in prod
1 parent 3ad9fa8 commit 2c1be32

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

migrations_lockfile.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ remote_subscriptions: 0003_drop_remote_subscription
2121

2222
replays: 0004_index_together
2323

24-
sentry: 0865_file_offsets
24+
sentry: 0866_grouptype_index
2525

2626
social_auth: 0002_default_auto_field
2727

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Generated by Django 5.1.7 on 2025-04-17 18:42
2+
3+
import django.db.models.fields.json
4+
import django.db.models.functions.comparison
5+
from django.db import migrations, models
6+
7+
from sentry.new_migrations.migrations import CheckedMigration
8+
9+
10+
class Migration(CheckedMigration):
11+
# This flag is used to mark that a migration shouldn't be automatically run in production.
12+
# This should only be used for operations where it's safe to run the migration after your
13+
# code has deployed. So this should not be used for most operations that alter the schema
14+
# of a table.
15+
# Here are some things that make sense to mark as post deployment:
16+
# - Large data migrations. Typically we want these to be run manually so that they can be
17+
# monitored and not block the deploy for a long period of time while they run.
18+
# - Adding indexes to large tables. Since this can take a long time, we'd generally prefer to
19+
# run this outside deployments so that we don't block them. Note that while adding an index
20+
# is a schema change, it's completely safe to run the operation after the code has deployed.
21+
# Once deployed, run these manually via: https://develop.sentry.dev/database-migrations/#migration-deployment
22+
23+
is_post_deployment = True
24+
25+
dependencies = [
26+
("sentry", "0865_file_offsets"),
27+
]
28+
29+
operations = [
30+
migrations.AddIndex(
31+
model_name="groupowner",
32+
index=models.Index(
33+
models.F("type"),
34+
django.db.models.functions.comparison.Cast(
35+
django.db.models.fields.json.KeyTextTransform(
36+
"commitId",
37+
django.db.models.functions.comparison.Cast(
38+
models.F("context"), models.JSONField()
39+
),
40+
),
41+
models.BigIntegerField(),
42+
),
43+
name="groupowner_type_json_commitid",
44+
),
45+
),
46+
]

src/sentry/models/groupowner.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99

1010
from django.conf import settings
1111
from django.db import models
12+
from django.db.models import BigIntegerField, F
13+
from django.db.models import JSONField as DjangoJSONField
14+
from django.db.models.fields.json import KeyTextTransform
15+
from django.db.models.functions import Cast
1216
from django.utils import timezone
1317

1418
from sentry.backup.scopes import RelocationScope
@@ -77,6 +81,20 @@ class Meta:
7781
app_label = "sentry"
7882
db_table = "sentry_groupowner"
7983

84+
indexes = [
85+
models.Index(
86+
F("type"),
87+
Cast(
88+
KeyTextTransform(
89+
"commitId",
90+
Cast(F("context"), DjangoJSONField()),
91+
),
92+
BigIntegerField(),
93+
),
94+
name="groupowner_type_json_commitid",
95+
),
96+
]
97+
8098
def save(self, *args, **kwargs):
8199
keys = [k for k in (self.user_id, self.team_id) if k is not None]
82100
assert len(keys) != 2, "Must have team or user or neither, not both"

0 commit comments

Comments
 (0)