From e5c92a1ee66c6af84e7cee637a2fa324a4d865e8 Mon Sep 17 00:00:00 2001 From: Lauryn Menard Date: Wed, 21 Feb 2024 21:12:29 +0100 Subject: [PATCH] remote-billing: Add index to RemoteRealmAuditLog for billing events. When profiling the database queries for the remote support view, getting the user counts for remote realms was identifed as an expensive query. Adds an index on RemoteRealmAuditLog to improve this relatively common query for remote billing information. --- ...log_add_synced_billing_event_type_index.py | 21 +++++++++++++++++++ zilencer/models.py | 5 +++++ 2 files changed, 26 insertions(+) create mode 100644 zilencer/migrations/0059_remoterealmauditlog_add_synced_billing_event_type_index.py diff --git a/zilencer/migrations/0059_remoterealmauditlog_add_synced_billing_event_type_index.py b/zilencer/migrations/0059_remoterealmauditlog_add_synced_billing_event_type_index.py new file mode 100644 index 0000000000..9cbeceb513 --- /dev/null +++ b/zilencer/migrations/0059_remoterealmauditlog_add_synced_billing_event_type_index.py @@ -0,0 +1,21 @@ +# Generated by Django 4.2.9 on 2024-02-20 12:04 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("zilencer", "0058_remoteinstallationcount_add_mobile_pushes_forwarded_index"), + ] + + operations = [ + migrations.AddIndex( + model_name="remoterealmauditlog", + index=models.Index( + condition=models.Q(("event_type__in", [101, 102, 103, 104, 105, 201, 202, 229])), + fields=["remote_realm_id", "id"], + name="zilencer_remoterealmauditlog_synced_billing_events", + ), + ), + ] diff --git a/zilencer/models.py b/zilencer/models.py index e0fb655efb..5fdb5591eb 100644 --- a/zilencer/models.py +++ b/zilencer/models.py @@ -333,6 +333,11 @@ class RemoteRealmAuditLog(AbstractRealmAuditLog): condition=Q(remote_realm__isnull=True), name="zilencer_remoterealmauditlog_server", ), + models.Index( + fields=["remote_realm_id", "id"], + condition=Q(event_type__in=AbstractRealmAuditLog.SYNCED_BILLING_EVENTS), + name="zilencer_remoterealmauditlog_synced_billing_events", + ), ]