From 0fc6a3c4e79acc5b7c2ce0e9e5e842fc6848ccb9 Mon Sep 17 00:00:00 2001 From: Zack Fu Zi Xiang Date: Tue, 3 Sep 2024 12:08:37 +0800 Subject: [PATCH] feat: use common table expression to improve user profile sql --- libs/database/src/workspace.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/libs/database/src/workspace.rs b/libs/database/src/workspace.rs index 7e4ec3ae..2b638f4f 100644 --- a/libs/database/src/workspace.rs +++ b/libs/database/src/workspace.rs @@ -584,8 +584,26 @@ pub async fn select_user_profile<'a, E: Executor<'a, Database = Postgres>>( let user_profile = sqlx::query_as!( AFUserProfileRow, r#" - SELECT * - FROM public.af_user_profile_view WHERE uuid = $1 + WITH af_user_row AS ( + SELECT * FROM af_user WHERE uuid = $1 + ) + SELECT + af_user_row.uid, + af_user_row.uuid, + af_user_row.email, + af_user_row.password, + af_user_row.name, + af_user_row.metadata, + af_user_row.encryption_sign, + af_user_row.deleted_at, + af_user_row.updated_at, + af_user_row.created_at, + (SELECT workspace_id + FROM af_workspace_member + WHERE uid = af_user_row.uid + ORDER BY updated_at DESC + LIMIT 1) as latest_workspace_id + FROM af_user_row "#, user_uuid )