Skip to content

Commit

Permalink
Try adding an index to speed up things
Browse files Browse the repository at this point in the history
  • Loading branch information
tonial committed Jan 10, 2025
1 parent d9ec948 commit 261fa63
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
19 changes: 19 additions & 0 deletions itou/users/migrations/0021_user_full_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 5.1.4 on 2025-01-10 14:18

import django.contrib.postgres.search
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('users', '0020_make_unnaccent_immutable'),
]

operations = [
migrations.AddField(
model_name='user',
name='full_name',
field=models.GeneratedField(db_persist=True, expression=django.contrib.postgres.search.SearchVector('first_name', 'last_name', config='simple'), output_field=django.contrib.postgres.search.SearchVectorField(), verbose_name='nom complet utilisé pour rechercher un utilisateur'),
),
]
20 changes: 20 additions & 0 deletions itou/users/migrations/0022_user_full_name_search_idx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 5.1.4 on 2025-01-10 14:19

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


class Migration(migrations.Migration):

dependencies = [
('auth', '0012_alter_user_first_name_max_length'),
('cities', '0001_initial'),
('users', '0021_user_full_name'),
]

operations = [
migrations.AddIndex(
model_name='user',
index=django.contrib.postgres.indexes.GinIndex(models.F('full_name'), name='full_name_search_idx'),
),
]
15 changes: 12 additions & 3 deletions itou/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
from citext import CIEmailField
from django.conf import settings
from django.contrib.auth.models import AbstractUser, UserManager
from django.contrib.postgres.indexes import OpClass
from django.contrib.postgres.indexes import GinIndex, OpClass
from django.contrib.postgres.lookups import Unaccent
from django.contrib.postgres.search import SearchVector, SearchVectorField
from django.core.exceptions import ValidationError
from django.core.serializers.json import DjangoJSONEncoder
from django.core.validators import MaxLengthValidator, MinLengthValidator, RegexValidator
from django.db import models
from django.db.models import Count, Q
from django.db.models.functions import Upper
from django.db.models.functions import Concat, Upper
from django.urls import reverse
from django.utils import timezone
from django.utils.crypto import salted_hmac
Expand Down Expand Up @@ -170,6 +172,12 @@ class User(AbstractUser, AddressMixin):
default="",
choices=Title.choices,
)
full_name = models.GeneratedField(
expression=SearchVector("first_name", "last_name", config="simple"),
output_field=SearchVectorField(),
verbose_name="nom complet utilisé pour rechercher un utilisateur",
db_persist=True,
)

email = CIEmailField(
"adresse e-mail",
Expand Down Expand Up @@ -231,7 +239,8 @@ class Meta(AbstractUser.Meta):
models.Index(
OpClass(Upper("email"), name="text_pattern_ops"),
name="users_user_email_upper",
)
),
GinIndex("full_name", name="full_name_search_idx"),
]
constraints = [
models.CheckConstraint(
Expand Down

0 comments on commit 261fa63

Please sign in to comment.