Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

The email index is not used byget_by_email() method #4

Open
Labels
bugSomething isn't working
@stephane

Description

@stephane

Describe the bug

An index is created on theemail field:
https://github.com/fastapi-users/fastapi-users-db-sqlalchemy/blob/main/fastapi_users_db_sqlalchemy/__init__.py#L61

but the index can't be used by theget_by_email() method because the filtering uses a function on the SQL field.

To Reproduce

explain analyzeSELECTauth_user.hashed_password,auth_user.is_active,auth_user.is_superuser,auth_user.is_verified,auth_user.id,auth_user.email,auth_user.first_name,auth_user.last_nameFROM auth_userWHERElower(auth_user.email)=lower('foo@bar.com');
Seq Scan on auth_user  (cost=0.00..10.90 rows=1 width=1245) (actual time=0.030..0.030 rows=0 loops=1)   Filter: (lower((email)::text) = 'foo@bar.com'::text)

Sequential scan is used instead of index scan.

Expected behavior

explain analyzeSELECTauth_user.hashed_password,auth_user.is_active,auth_user.is_superuser,auth_user.is_verified,auth_user.id,auth_user.email,auth_user.first_name,auth_user.last_nameFROM auth_userWHEREauth_user.email=lower('foo@bar.com');
Index Scan using ix_auth_user_email on auth_user  (cost=0.14..2.36 rows=1 width=1245) (actual time=0.044..0.045 rows=0 loops=1)   Index Cond: ((email)::text = 'foo@bar.com'::text)

There is several ways to fix the issue:

  1. use CIText extension but it's not standard in SQLAlchemy (https://pypi.org/project/sqlalchemy-citext/).
  2. create a functional index:CREATE INDEX ix_auth_user ON auth_user (lower(email) text_pattern_ops);
  3. store email in lower case (my favorite even though on rare occasions, an outdated server or program might not interpret the capitalization correctly).

I can provide a PR when we'll agree on a solution.

Configuration

  • FastAPI Users version : 0.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp