From 902498ec4ff572ff289b875f62bb304df8cbf21e Mon Sep 17 00:00:00 2001 From: Alex Vandiver Date: Wed, 13 Sep 2023 16:29:06 +0000 Subject: [PATCH] zilencer: Update remoterealm indexes. There is no reason to have an index on just `realm_id` or `remote_id`, as those values mean nothing outside of the scope of a specific `server_id`. Remove those never-used single-column indexes from the two tables that have them. By contrast, the pair of `server_id` and `remote_id` is quite useful and specific -- it is a unique pair, and every POST of statistics from a remote host requires looking up the highest `remote_id` for a given `server_id`, which (without this index) is otherwise a quite large scan. Add a unique constraint, which (in PostgreSQL) is implemented as a unique index. --- .../0029_update_remoterealm_indexes.py | 45 +++++++++++++++++++ zilencer/models.py | 22 +++++++-- 2 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 zilencer/migrations/0029_update_remoterealm_indexes.py diff --git a/zilencer/migrations/0029_update_remoterealm_indexes.py b/zilencer/migrations/0029_update_remoterealm_indexes.py new file mode 100644 index 0000000000..ad510c7291 --- /dev/null +++ b/zilencer/migrations/0029_update_remoterealm_indexes.py @@ -0,0 +1,45 @@ +# Generated by Django 4.2.5 on 2023-09-13 05:02 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("zilencer", "0028_rename_extradatajson_remoteauditlog_extra_data"), + ] + + operations = [ + migrations.AlterField( + model_name="remoterealmauditlog", + name="realm_id", + field=models.IntegerField(), + ), + migrations.AlterField( + model_name="remoterealmauditlog", + name="remote_id", + field=models.IntegerField(), + ), + migrations.AlterField( + model_name="remoterealmcount", + name="realm_id", + field=models.IntegerField(), + ), + migrations.AlterField( + model_name="remoterealmcount", + name="remote_id", + field=models.IntegerField(), + ), + migrations.AddConstraint( + model_name="remoterealmauditlog", + constraint=models.UniqueConstraint( + fields=("server", "remote_id"), name="zilencer_remoterealmauditlog_server_remote" + ), + ), + migrations.AddIndex( + model_name="remoterealmauditlog", + index=models.Index( + fields=["server", "realm_id", "remote_id"], + name="zilencer_remoterealmauditlog_server_realm_remote", + ), + ), + ] diff --git a/zilencer/models.py b/zilencer/models.py index c646516429..af6682eb76 100644 --- a/zilencer/models.py +++ b/zilencer/models.py @@ -100,13 +100,27 @@ class RemoteRealmAuditLog(AbstractRealmAuditLog): """ server = models.ForeignKey(RemoteZulipServer, on_delete=models.CASCADE) - realm_id = models.IntegerField(db_index=True) + realm_id = models.IntegerField() # The remote_id field lets us deduplicate data from the remote server - remote_id = models.IntegerField(db_index=True) + remote_id = models.IntegerField() def __str__(self) -> str: return f"{self.server!r} {self.event_type} {self.event_time} {self.id}" + class Meta: + constraints = [ + models.UniqueConstraint( + fields=["server", "remote_id"], + name="zilencer_remoterealmauditlog_server_remote", + ), + ] + indexes = [ + models.Index( + fields=["server", "realm_id", "remote_id"], + name="zilencer_remoterealmauditlog_server_realm_remote", + ), + ] + class RemoteInstallationCount(BaseCount): server = models.ForeignKey(RemoteZulipServer, on_delete=models.CASCADE) @@ -129,9 +143,9 @@ class RemoteInstallationCount(BaseCount): # We can't subclass RealmCount because we only have a realm_id here, not a foreign key. class RemoteRealmCount(BaseCount): server = models.ForeignKey(RemoteZulipServer, on_delete=models.CASCADE) - realm_id = models.IntegerField(db_index=True) + realm_id = models.IntegerField() # The remote_id field lets us deduplicate data from the remote server - remote_id = models.IntegerField(db_index=True) + remote_id = models.IntegerField() class Meta: unique_together = ("server", "realm_id", "property", "subgroup", "end_time")