Skip to content

Commit

Permalink
audit-logs: Add index to RealmAuditLog for realm and event type.
Browse files Browse the repository at this point in the history
Adds an index on RealmAuditLog for the realm, event_type, and
event_time in order to improve database queries on these audit logs.

tabbott verified using EXPLAIN ANALYZE that this also considerably
speeds up queries that order by ID rather than event_time, but
event_time is how these should be ordered given the possibility of
backfills.
  • Loading branch information
laurynmm authored and timabbott committed Apr 19, 2024
1 parent 962ab13 commit 2e79e03
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
18 changes: 18 additions & 0 deletions zerver/migrations/0510_add_realmauditlog_realm_event_type_index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.12 on 2024-04-17 10:33

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("zerver", "0509_fix_emoji_metadata"),
]

operations = [
migrations.AddIndex(
model_name="realmauditlog",
index=models.Index(
fields=["realm", "event_type", "event_time"], name="zerver_realmauditlog_realm__event_type__event_time"
),
),
]
6 changes: 5 additions & 1 deletion zerver/models/realm_audit_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ class RealmAuditLog(AbstractRealmAuditLog):

class Meta:
indexes = [
models.Index(
name="zerver_realmauditlog_realm__event_type__event_time",
fields=["realm", "event_type", "event_time"],
),
models.Index(
name="zerver_realmauditlog_user_subscriptions_idx",
fields=["modified_user", "modified_stream"],
Expand All @@ -237,7 +241,7 @@ class Meta:
AbstractRealmAuditLog.SUBSCRIPTION_DEACTIVATED,
]
),
)
),
]

@override
Expand Down

0 comments on commit 2e79e03

Please sign in to comment.