Add better indexes

This commit is contained in:
R. Miles McCain 2020-04-14 10:16:07 -04:00
parent cfe3dac408
commit f4710170ee
No known key found for this signature in database
GPG Key ID: 91CB47BDDF2671A5
2 changed files with 100 additions and 9 deletions

View File

@ -0,0 +1,76 @@
# Generated by Django 3.0.5 on 2020-04-14 14:08
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('analytics', '0008_auto_20200412_0015'),
]
operations = [
migrations.AlterModelOptions(
name='hit',
options={'ordering': ['-start_time']},
),
migrations.AlterModelOptions(
name='session',
options={'ordering': ['-start_time']},
),
migrations.AlterField(
model_name='hit',
name='initial',
field=models.BooleanField(db_index=True, default=True),
),
migrations.AlterField(
model_name='hit',
name='location',
field=models.TextField(blank=True, db_index=True),
),
migrations.AlterField(
model_name='hit',
name='referrer',
field=models.TextField(blank=True, db_index=True),
),
migrations.AlterField(
model_name='hit',
name='start_time',
field=models.DateTimeField(auto_now_add=True, db_index=True),
),
migrations.AlterField(
model_name='session',
name='identifier',
field=models.TextField(blank=True, db_index=True),
),
migrations.AlterField(
model_name='session',
name='ip',
field=models.GenericIPAddressField(db_index=True),
),
migrations.AlterField(
model_name='session',
name='start_time',
field=models.DateTimeField(auto_now_add=True, db_index=True),
),
migrations.AddIndex(
model_name='hit',
index=models.Index(fields=['session', '-start_time'], name='analytics_h_session_b2667f_idx'),
),
migrations.AddIndex(
model_name='hit',
index=models.Index(fields=['session', 'location'], name='analytics_h_session_775f5a_idx'),
),
migrations.AddIndex(
model_name='hit',
index=models.Index(fields=['session', 'referrer'], name='analytics_h_session_98b8bf_idx'),
),
migrations.AddIndex(
model_name='session',
index=models.Index(fields=['service', '-start_time'], name='analytics_s_service_4b1137_idx'),
),
migrations.AddIndex(
model_name='session',
index=models.Index(fields=['service', 'identifier'], name='analytics_s_service_82ab21_idx'),
),
]

View File

@ -13,13 +13,13 @@ def _default_uuid():
class Session(models.Model): class Session(models.Model):
uuid = models.UUIDField(default=_default_uuid, primary_key=True) uuid = models.UUIDField(default=_default_uuid, primary_key=True)
service = models.ForeignKey(Service, on_delete=models.CASCADE) service = models.ForeignKey(Service, on_delete=models.CASCADE, db_index=True)
# Cross-session identification; optional, and provided by the service # Cross-session identification; optional, and provided by the service
identifier = models.TextField(blank=True) identifier = models.TextField(blank=True, db_index=True)
# Time # Time
start_time = models.DateTimeField(auto_now_add=True) start_time = models.DateTimeField(auto_now_add=True, db_index=True)
last_seen = models.DateTimeField(auto_now_add=True) last_seen = models.DateTimeField(auto_now_add=True)
# Core request information # Core request information
@ -38,7 +38,7 @@ class Session(models.Model):
default="OTHER", default="OTHER",
) )
os = models.TextField() os = models.TextField()
ip = models.GenericIPAddressField() ip = models.GenericIPAddressField(db_index=True)
# GeoIP data # GeoIP data
asn = models.TextField(blank=True) asn = models.TextField(blank=True)
@ -47,6 +47,13 @@ class Session(models.Model):
latitude = models.FloatField(null=True) latitude = models.FloatField(null=True)
time_zone = models.TextField(blank=True) time_zone = models.TextField(blank=True)
class Meta:
ordering = ["-start_time"]
indexes = [
models.Index(fields=["service", "-start_time"]),
models.Index(fields=["service", "identifier"]),
]
@property @property
def is_currently_active(self): def is_currently_active(self):
return timezone.now() - self.last_seen < timezone.timedelta(seconds=10) return timezone.now() - self.last_seen < timezone.timedelta(seconds=10)
@ -57,20 +64,28 @@ class Session(models.Model):
class Hit(models.Model): class Hit(models.Model):
session = models.ForeignKey(Session, on_delete=models.CASCADE) session = models.ForeignKey(Session, on_delete=models.CASCADE, db_index=True)
initial = models.BooleanField(default=True) initial = models.BooleanField(default=True, db_index=True)
# Base request information # Base request information
start_time = models.DateTimeField(auto_now_add=True) start_time = models.DateTimeField(auto_now_add=True, db_index=True)
last_seen = models.DateTimeField(auto_now_add=True) last_seen = models.DateTimeField(auto_now_add=True)
heartbeats = models.IntegerField(default=0) heartbeats = models.IntegerField(default=0)
tracker = models.TextField() # Tracking pixel or JS tracker = models.TextField() # Tracking pixel or JS
# Advanced page information # Advanced page information
location = models.TextField(blank=True) location = models.TextField(blank=True, db_index=True)
referrer = models.TextField(blank=True) referrer = models.TextField(blank=True, db_index=True)
load_time = models.FloatField(null=True) load_time = models.FloatField(null=True)
class Meta:
ordering = ["-start_time"]
indexes = [
models.Index(fields=["session", "-start_time"]),
models.Index(fields=["session", "location"]),
models.Index(fields=["session", "referrer"]),
]
@property @property
def duration(self): def duration(self):
return self.last_seen - self.start_time return self.last_seen - self.start_time