Skip to content

Performance: Inefficient Table Scan in create_profile #33

@windoze95

Description

@windoze95

Description

In app/api/auth.py, the create_profile endpoint checks if this is the first user by fetching all existing users from the database:

    result = await db.execute(select(User))
    existing = result.scalars().all()
    is_first_user = len(existing) == 0

Impact

As the userbase grows, this will fetch and instantiate ORM objects for every single user in the database just to check if the count is zero, consuming unnecessary memory and database I/O.

Suggested Fix

Replace this with a fast count query, or simply fetch a single user with a limit:

    result = await db.execute(select(User.id).limit(1))
    is_first_user = result.scalar_one_or_none() is None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions