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.
This commit is contained in:
Alex Vandiver 2023-09-13 16:29:06 +00:00 committed by Tim Abbott
parent d228c502e9
commit 902498ec4f
2 changed files with 63 additions and 4 deletions

View File

@ -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",
),
),
]

View File

@ -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")