removed node sort on trees and added better indexes

This commit is contained in:
smilerz 2021-09-15 16:28:07 -05:00
parent 90dc445c45
commit aa6b8be647
3 changed files with 137 additions and 9 deletions

View File

@ -0,0 +1,106 @@
# Generated by Django 3.2.7 on 2021-09-15 21:27
import django.contrib.postgres.indexes
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('cookbook', '0152_automation'),
]
operations = [
migrations.RemoveIndex(
model_name='cooklog',
name='cookbook_co_id_93d841_idx',
),
migrations.RemoveIndex(
model_name='food',
name='cookbook_fo_id_22b733_idx',
),
migrations.RemoveIndex(
model_name='ingredient',
name='cookbook_in_id_3368be_idx',
),
migrations.RemoveIndex(
model_name='recipe',
name='cookbook_re_name_se_bdf3ca_gin',
),
migrations.RemoveIndex(
model_name='recipe',
name='cookbook_re_id_e4c2d4_idx',
),
migrations.RemoveIndex(
model_name='recipebook',
name='cookbook_re_name_bbe446_idx',
),
migrations.AddIndex(
model_name='cooklog',
index=models.Index(fields=['id'], name='cookbook_co_id_553a6d_idx'),
),
migrations.AddIndex(
model_name='cooklog',
index=models.Index(fields=['recipe'], name='cookbook_co_recipe__8ec719_idx'),
),
migrations.AddIndex(
model_name='cooklog',
index=models.Index(fields=['-created_at'], name='cookbook_co_created_f6e244_idx'),
),
migrations.AddIndex(
model_name='cooklog',
index=models.Index(fields=['rating'], name='cookbook_co_rating_aa7662_idx'),
),
migrations.AddIndex(
model_name='cooklog',
index=models.Index(fields=['created_by'], name='cookbook_co_created_7ea086_idx'),
),
migrations.AddIndex(
model_name='cooklog',
index=models.Index(fields=['created_by', 'rating'], name='cookbook_co_created_f5ccd7_idx'),
),
migrations.AddIndex(
model_name='food',
index=models.Index(fields=['id'], name='cookbook_fo_id_3c379b_idx'),
),
migrations.AddIndex(
model_name='food',
index=models.Index(fields=['name'], name='cookbook_fo_name_c848b6_idx'),
),
migrations.AddIndex(
model_name='ingredient',
index=models.Index(fields=['id'], name='cookbook_in_id_2c1f57_idx'),
),
migrations.AddIndex(
model_name='recipe',
index=django.contrib.postgres.indexes.GinIndex(fields=['name_search_vector'], name='cookbook_re_name_se_5dbbd5_gin'),
),
migrations.AddIndex(
model_name='recipe',
index=django.contrib.postgres.indexes.GinIndex(fields=['desc_search_vector'], name='cookbook_re_desc_se_fdee30_gin'),
),
migrations.AddIndex(
model_name='recipe',
index=models.Index(fields=['id'], name='cookbook_re_id_b2bdcf_idx'),
),
migrations.AddIndex(
model_name='recipe',
index=models.Index(fields=['name'], name='cookbook_re_name_b8a027_idx'),
),
migrations.AddIndex(
model_name='recipebook',
index=models.Index(fields=['name'], name='cookbook_re_name_94cc63_idx'),
),
migrations.AddIndex(
model_name='viewlog',
index=models.Index(fields=['recipe'], name='cookbook_vi_recipe__ce995d_idx'),
),
migrations.AddIndex(
model_name='viewlog',
index=models.Index(fields=['-created_at'], name='cookbook_vi_created_bd2b5f_idx'),
),
migrations.AddIndex(
model_name='viewlog',
index=models.Index(fields=['created_by'], name='cookbook_vi_created_f9385c_idx'),
),
]

View File

@ -336,7 +336,7 @@ class SyncLog(models.Model, PermissionModelMixin):
class Keyword(ExportModelOperationsMixin('keyword'), TreeModel, PermissionModelMixin): class Keyword(ExportModelOperationsMixin('keyword'), TreeModel, PermissionModelMixin):
# TODO add find and fix problem functions # TODO add find and fix problem functions
node_order_by = ['name'] # node_order_by = ['name']
name = models.CharField(max_length=64) name = models.CharField(max_length=64)
icon = models.CharField(max_length=16, blank=True, null=True) icon = models.CharField(max_length=16, blank=True, null=True)
description = models.TextField(default="", blank=True) description = models.TextField(default="", blank=True)
@ -371,7 +371,7 @@ class Unit(ExportModelOperationsMixin('unit'), models.Model, PermissionModelMixi
class Food(ExportModelOperationsMixin('food'), TreeModel, PermissionModelMixin): class Food(ExportModelOperationsMixin('food'), TreeModel, PermissionModelMixin):
# TODO add find and fix problem functions # TODO add find and fix problem functions
node_order_by = ['name'] # node_order_by = ['name']
name = models.CharField(max_length=128, validators=[MinLengthValidator(1)]) name = models.CharField(max_length=128, validators=[MinLengthValidator(1)])
recipe = models.ForeignKey('Recipe', null=True, blank=True, on_delete=models.SET_NULL) recipe = models.ForeignKey('Recipe', null=True, blank=True, on_delete=models.SET_NULL)
supermarket_category = models.ForeignKey(SupermarketCategory, null=True, blank=True, on_delete=models.SET_NULL) supermarket_category = models.ForeignKey(SupermarketCategory, null=True, blank=True, on_delete=models.SET_NULL)
@ -394,7 +394,10 @@ class Food(ExportModelOperationsMixin('food'), TreeModel, PermissionModelMixin):
constraints = [ constraints = [
models.UniqueConstraint(fields=['space', 'name'], name='f_unique_name_per_space') models.UniqueConstraint(fields=['space', 'name'], name='f_unique_name_per_space')
] ]
indexes = (Index(fields=['id', 'name']),) indexes = (
Index(fields=['id']),
Index(fields=['name']),
)
class Ingredient(ExportModelOperationsMixin('ingredient'), models.Model, PermissionModelMixin): class Ingredient(ExportModelOperationsMixin('ingredient'), models.Model, PermissionModelMixin):
@ -415,7 +418,9 @@ class Ingredient(ExportModelOperationsMixin('ingredient'), models.Model, Permiss
class Meta: class Meta:
ordering = ['order', 'pk'] ordering = ['order', 'pk']
indexes = (Index(fields=['id', 'food', 'unit']),) indexes = (
Index(fields=['id']),
)
class Step(ExportModelOperationsMixin('step'), models.Model, PermissionModelMixin): class Step(ExportModelOperationsMixin('step'), models.Model, PermissionModelMixin):
@ -504,7 +509,12 @@ class Recipe(ExportModelOperationsMixin('recipe'), models.Model, PermissionModel
return self.name return self.name
class Meta(): class Meta():
indexes = (GinIndex(fields=["name_search_vector", "desc_search_vector"]), Index(fields=['id', 'name', 'description']),) indexes = (
GinIndex(fields=["name_search_vector"]),
GinIndex(fields=["desc_search_vector"]),
Index(fields=['id']),
Index(fields=['name']),
)
class Comment(ExportModelOperationsMixin('comment'), models.Model, PermissionModelMixin): class Comment(ExportModelOperationsMixin('comment'), models.Model, PermissionModelMixin):
@ -555,7 +565,7 @@ class RecipeBook(ExportModelOperationsMixin('book'), models.Model, PermissionMod
return self.name return self.name
class Meta(): class Meta():
indexes = (Index(fields=['name', 'description']),) indexes = (Index(fields=['name']),)
class RecipeBookEntry(ExportModelOperationsMixin('book_entry'), models.Model, PermissionModelMixin): class RecipeBookEntry(ExportModelOperationsMixin('book_entry'), models.Model, PermissionModelMixin):
@ -756,7 +766,14 @@ class CookLog(ExportModelOperationsMixin('cook_log'), models.Model, PermissionMo
return self.recipe.name return self.recipe.name
class Meta(): class Meta():
indexes = (Index(fields=['id', 'recipe', '-created_at', 'rating', 'created_by']),) indexes = (
Index(fields=['id']),
Index(fields=['recipe']),
Index(fields=['-created_at']),
Index(fields=['rating']),
Index(fields=['created_by']),
Index(fields=['created_by', 'rating']),
)
class ViewLog(ExportModelOperationsMixin('view_log'), models.Model, PermissionModelMixin): class ViewLog(ExportModelOperationsMixin('view_log'), models.Model, PermissionModelMixin):
@ -771,7 +788,12 @@ class ViewLog(ExportModelOperationsMixin('view_log'), models.Model, PermissionMo
return self.recipe.name return self.recipe.name
class Meta(): class Meta():
indexes = (Index(fields=['recipe', '-created_at', 'created_by']),) indexes = (
Index(fields=['recipe']),
Index(fields=[ '-created_at']),
Index(fields=['created_by']),
Index(fields=['recipe', '-created_at', 'created_by']),
)
class ImportLog(models.Model, PermissionModelMixin): class ImportLog(models.Model, PermissionModelMixin):

View File

@ -226,7 +226,7 @@ class TreeMixin(MergeMixin, FuzzyFilterMixin):
self.queryset = self.model.objects.none() self.queryset = self.model.objects.none()
else: else:
return super().get_queryset() return super().get_queryset()
return self.queryset.filter(space=self.request.space) return self.queryset.filter(space=self.request.space).order_by('name')
@decorators.action(detail=True, url_path='move/(?P<parent>[^/.]+)', methods=['PUT'], ) @decorators.action(detail=True, url_path='move/(?P<parent>[^/.]+)', methods=['PUT'], )
@decorators.renderer_classes((TemplateHTMLRenderer, JSONRenderer)) @decorators.renderer_classes((TemplateHTMLRenderer, JSONRenderer))