Merge pull request #783 from AppFlowy-IO/user-profile-improvement

feat: use common table expression to improve user profile sql
This commit is contained in:
Zack 2024-09-03 13:06:44 +08:00 committed by GitHub
commit 5fe1a874c0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 10 deletions

View File

@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
"query": "\n SELECT *\n FROM public.af_user_profile_view WHERE uuid = $1\n ",
"query": "\n WITH af_user_row AS (\n SELECT * FROM af_user WHERE uuid = $1\n )\n SELECT\n af_user_row.uid,\n af_user_row.uuid,\n af_user_row.email,\n af_user_row.password,\n af_user_row.name,\n af_user_row.metadata,\n af_user_row.encryption_sign,\n af_user_row.deleted_at,\n af_user_row.updated_at,\n af_user_row.created_at,\n (SELECT workspace_id\n FROM af_workspace_member\n WHERE uid = af_user_row.uid\n ORDER BY updated_at DESC\n LIMIT 1) as latest_workspace_id\n FROM af_user_row\n ",
"describe": {
"columns": [
{
@ -65,18 +65,18 @@
]
},
"nullable": [
false,
false,
false,
false,
false,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true
null
]
},
"hash": "7438ddaf9ed9fab7ba4cb1b9c79ff541fa2e63a629cad4f8e9692a50a4e5e03c"
"hash": "a8443c8307a099fbfa3e04232fdc7719c28dc5acac088686e86980544fe4bbc9"
}

View File

@ -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
)