Skip to content

Commit

Permalink
Use Block Range index instead of b-tree to handle range operations
Browse files Browse the repository at this point in the history
Signed-off-by: Kipchirchir Sigei <[email protected]>
  • Loading branch information
KipSigei committed Mar 13, 2023
1 parent 14879d8 commit 6624537
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 50 deletions.
44 changes: 44 additions & 0 deletions onadata/apps/logger/migrations/0005_add_brindex_date_fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Generated by Django 3.2.18 on 2023-03-13 15:34

import django.contrib.postgres.indexes
from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("logger", "0004_update_instance_geoms"),
]

operations = [
migrations.SeparateDatabaseAndState(
database_operations=[
migrations.RunSQL(
sql='CREATE INDEX "logger_inst_date_cr_079af3_brin" ON "logger_instance" USING brin ("date_created") WITH (autosummarize = on);',
reverse_sql='DROP INDEX "logger_inst_date_cr_079af3_brin";',
),
migrations.RunSQL(
sql='CREATE INDEX "logger_inst_date_mo_270e0d_brin" ON "logger_instance" USING brin ("date_modified") WITH (autosummarize = on);',
reverse_sql='DROP INDEX "logger_inst_date_mo_270e0d_brin";',
),
],
state_operations=[
migrations.AddIndex(
model_name="instance",
index=django.contrib.postgres.indexes.BrinIndex(
autosummarize=True,
fields=["date_created"],
name="logger_inst_date_cr_079af3_brin",
),
),
migrations.AddIndex(
model_name="instance",
index=django.contrib.postgres.indexes.BrinIndex(
autosummarize=True,
fields=["date_modified"],
name="logger_inst_date_mo_270e0d_brin",
),
),
],
)
]
48 changes: 0 additions & 48 deletions onadata/apps/logger/migrations/0005_auto_20230308_0252.py

This file was deleted.

9 changes: 7 additions & 2 deletions onadata/apps/logger/models/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from django.contrib.auth import get_user_model
from django.contrib.gis.db import models
from django.contrib.gis.geos import GeometryCollection, Point
from django.contrib.postgres.indexes import BrinIndex
from django.core.cache import cache
from django.db import connection, transaction
from django.db.models import Q
Expand Down Expand Up @@ -649,10 +650,10 @@ class Instance(models.Model, InstanceBaseClass):
survey_type = models.ForeignKey("logger.SurveyType", on_delete=models.PROTECT)

# shows when we first received this instance
date_created = models.DateTimeField(auto_now_add=True, db_index=True)
date_created = models.DateTimeField(auto_now_add=True)

# this will end up representing "date last parsed"
date_modified = models.DateTimeField(auto_now=True, db_index=True)
date_modified = models.DateTimeField(auto_now=True)

# this will end up representing "date instance was deleted"
deleted_at = models.DateTimeField(null=True, default=None)
Expand Down Expand Up @@ -694,6 +695,10 @@ class Instance(models.Model, InstanceBaseClass):
class Meta:
app_label = "logger"
unique_together = ("xform", "uuid")
indexes = [
BrinIndex(fields=['date_created'], autosummarize=True),
BrinIndex(fields=['date_modified'], autosummarize=True),
]

@classmethod
def set_deleted_at(cls, instance_id, deleted_at=timezone.now(), user=None):
Expand Down

0 comments on commit 6624537

Please sign in to comment.