From a956868eaf3dd3cdcf9c6a2570df0bda57c295a1 Mon Sep 17 00:00:00 2001 From: smilerz Date: Sun, 30 May 2021 08:57:09 -0500 Subject: [PATCH] create indexes --- .../migrations/0122_build_full_text_index.py | 30 ++++++++++++++++++- cookbook/models.py | 17 +++++++++-- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/cookbook/migrations/0122_build_full_text_index.py b/cookbook/migrations/0122_build_full_text_index.py index 18c967471..7741e2f1d 100644 --- a/cookbook/migrations/0122_build_full_text_index.py +++ b/cookbook/migrations/0122_build_full_text_index.py @@ -6,7 +6,7 @@ from django.db import migrations from django_scopes import scopes_disabled from django.utils import translation from cookbook.managers import DICTIONARY -from cookbook.models import Recipe, Step +from cookbook.models import Recipe, Step, Index @@ -52,6 +52,34 @@ class Migration(migrations.Migration): model_name='step', index=GinIndex(fields=['search_vector'], name='cookbook_st_search__2ef7fa_gin'), ), + migrations.AddIndex( + model_name='cooklog', + index=Index(fields=['id', 'recipe', '-created_at', 'rating'], name='cookbook_co_id_37485a_idx'), + ), + migrations.AddIndex( + model_name='food', + index=Index(fields=['id', 'name'], name='cookbook_fo_id_22b733_idx'), + ), + migrations.AddIndex( + model_name='ingredient', + index=Index(fields=['id', 'food', 'unit'], name='cookbook_in_id_3368be_idx'), + ), + migrations.AddIndex( + model_name='keyword', + index=Index(fields=['id', 'name'], name='cookbook_ke_id_ebc03f_idx'), + ), + migrations.AddIndex( + model_name='recipe', + index=Index(fields=['id', 'name', 'description'], name='cookbook_re_id_e4c2d4_idx'), + ), + migrations.AddIndex( + model_name='recipebook', + index=Index(fields=['name', 'description'], name='cookbook_re_name_bbe446_idx'), + ), + migrations.AddIndex( + model_name='viewlog', + index=Index(fields=['recipe', '-created_at'], name='cookbook_vi_recipe__5cd178_idx'), + ), migrations.RunPython( set_default_search_vector ), diff --git a/cookbook/models.py b/cookbook/models.py index f7a81a3fa..06c34a8b1 100644 --- a/cookbook/models.py +++ b/cookbook/models.py @@ -10,6 +10,7 @@ from django.contrib.postgres.indexes import GinIndex from django.contrib.postgres.search import SearchVectorField from django.core.validators import MinLengthValidator from django.db import models +from django.db.models import Index from django.utils import timezone from django.utils.translation import gettext as _ from django_prometheus.models import ExportModelOperationsMixin @@ -271,6 +272,7 @@ class Keyword(ExportModelOperationsMixin('keyword'), models.Model, PermissionMod class Meta: unique_together = (('space', 'name'),) + indexes = (Index(fields=['id', 'name']), ) class Unit(ExportModelOperationsMixin('unit'), models.Model, PermissionModelMixin): @@ -302,6 +304,7 @@ class Food(ExportModelOperationsMixin('food'), models.Model, PermissionModelMixi class Meta: unique_together = (('space', 'name'),) + indexes = (Index(fields=['id', 'name']), ) class Ingredient(ExportModelOperationsMixin('ingredient'), models.Model, PermissionModelMixin): @@ -327,6 +330,7 @@ class Ingredient(ExportModelOperationsMixin('ingredient'), models.Model, Permiss class Meta: ordering = ['order', 'pk'] + indexes = (Index(fields=['id', 'food', 'unit']), ) class Step(ExportModelOperationsMixin('step'), models.Model, PermissionModelMixin): @@ -361,7 +365,7 @@ class Step(ExportModelOperationsMixin('step'), models.Model, PermissionModelMixi class Meta: ordering = ['order', 'pk'] - indexes = (GinIndex(fields=["search_vector"]),) + indexes = (GinIndex(fields=["search_vector"]), ) class NutritionInformation(models.Model, PermissionModelMixin): @@ -427,7 +431,7 @@ class Recipe(models.Model, PermissionModelMixin): return self.name class Meta(): - indexes = (GinIndex(fields=["name_search_vector", "desc_search_vector"]),) + indexes = (GinIndex(fields=["name_search_vector", "desc_search_vector"]), Index(fields=['id', 'name', 'description']), ) class Comment(ExportModelOperationsMixin('comment'), models.Model, PermissionModelMixin): @@ -477,6 +481,9 @@ class RecipeBook(ExportModelOperationsMixin('book'), models.Model, PermissionMod def __str__(self): return self.name + class Meta(): + indexes = (Index(fields=['name', 'description']), ) + class RecipeBookEntry(ExportModelOperationsMixin('book_entry'), models.Model, PermissionModelMixin): recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE) @@ -672,6 +679,9 @@ class CookLog(ExportModelOperationsMixin('cook_log'), models.Model, PermissionMo def __str__(self): return self.recipe.name + class Meta(): + indexes = (Index(fields=['id', 'recipe', '-created_at', 'rating']), ) + class ViewLog(ExportModelOperationsMixin('view_log'), models.Model, PermissionModelMixin): recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE) @@ -684,6 +694,9 @@ class ViewLog(ExportModelOperationsMixin('view_log'), models.Model, PermissionMo def __str__(self): return self.recipe.name + class Meta(): + indexes = (Index(fields=['recipe', '-created_at']), ) + class ImportLog(models.Model, PermissionModelMixin): type = models.CharField(max_length=32)