Skip to content

Commit

Permalink
Add index for username in uppercase
Browse files Browse the repository at this point in the history
This should speed-up case-insensitive queries
at least on postgreSQL.

Some benchnmarking:

 - with cache
    *  cold cache
        ```
        >>> import timeit
        >>> timeit.timeit("User.objects.get_anonymous_user()", number=100, setup="from inyoka.portal.user import User; from django.conf import settings")
        1.2246114060000082
        ```

    * warm cache (mind the additional 0)
        ```
        >>> import timeit
        >>> timeit.timeit("User.objects.get_anonymous_user()", number=1000, setup="from inyoka.portal.user import User; from django.conf import settings")
        0.29570037799999227
        ```

 - without index (before applying the migration of this commit)
        ```
        >>> import timeit
        >>> timeit.timeit("User.objects.get(username__iexact=settings.ANONYMOUS_USER_NAME)", number=1000, setup="from inyoka.portal.user import User; from django.conf import settings")
        51.55617682000002
        ```
 - with index

        ```
        >>> import timeit
        >>> timeit.timeit("User.objects.get(username__iexact=settings.ANONYMOUS_USER_NAME)", number=1000, setup="from inyoka.portal.user import User; from django.conf import settings")
        0.9243776870002876
        ```


This can be also seen in the EXPLAIN (mind the filter instead of get, otherwise an explain is not possible)

```
>>> print(User.objects.filter(username__iexact=settings.ANONYMOUS_USER_NAME).explain(verbose=True))
Bitmap Heap Scan on public.portal_user  (cost=18.18..2544.97 rows=743 width=447)
  Output: id, password, last_login, is_superuser, username, email, status, date_joined, banned_until, avatar, jabber, signature, location, gpgkey, website, launchpad, settings, forum_read_status, member_title, icon
  Recheck Cond: (upper((portal_user.username)::text) = 'ANONYMOUS'::text)
  ->  Bitmap Index Scan on upper_username_idx  (cost=0.00..17.99 rows=743 width=0)
        Index Cond: (upper((portal_user.username)::text) = 'ANONYMOUS'::text)
```
  • Loading branch information
chris34 committed Feb 12, 2024
1 parent 59d474d commit f62bcb3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
18 changes: 18 additions & 0 deletions inyoka/portal/migrations/0036_user_upper_username_idx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.23 on 2024-02-05 21:55

from django.db import migrations, models
import django.db.models.functions.text


class Migration(migrations.Migration):

dependencies = [
('portal', '0035_auto_20231127_0114'),
]

operations = [
migrations.AddIndex(
model_name='user',
index=models.Index(django.db.models.functions.text.Upper('username'), name='upper_username_idx'),
),
]
4 changes: 4 additions & 0 deletions inyoka/portal/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from django.core.cache import cache
from django.core.exceptions import ValidationError
from django.db import models, transaction
from django.db.models.functions import Upper
from django.dispatch import receiver
from django.utils.html import escape
from django.utils.translation import gettext as _
Expand Down Expand Up @@ -547,6 +548,9 @@ class Meta:
permissions = (
('subscribe_user', 'Can subscribe Users'),
)
indexes = [
models.Index(Upper('username'), name='upper_username_idx'),
]


class UserPage(models.Model):
Expand Down

0 comments on commit f62bcb3

Please sign in to comment.