From da4a97c25909c0ae6810429d90f145418dddc69f Mon Sep 17 00:00:00 2001 From: ClawOSS Date: Mon, 16 Mar 2026 04:26:31 +0800 Subject: [PATCH] fix(auth): optimize first user check in create_profile Replace inefficient table scan with limited single-column query. OLD: select(User) fetched all users into memory NEW: select(User.id).limit(1) fetches at most 1 id This avoids loading all user records when only checking if any exist. --- app/api/auth.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/app/api/auth.py b/app/api/auth.py index ad0f65f..1f34c33 100644 --- a/app/api/auth.py +++ b/app/api/auth.py @@ -76,9 +76,8 @@ async def create_profile( db: AsyncSession = Depends(get_db), ) -> UserProfile: # Check if any users exist; first user becomes admin automatically. - result = await db.execute(select(User)) - existing = result.scalars().all() - is_first_user = len(existing) == 0 + result = await db.execute(select(User.id).limit(1)) + is_first_user = result.scalar_one_or_none() is None user = User( id=str(uuid.uuid4()),