zitadel/internal/query/user_notify_by_id.sql
Silvan 4df138286b
perf(query): reduce user query duration (#10037)
# Which Problems Are Solved

The resource usage to query user(s) on the database was high and
therefore could have performance impact.

# How the Problems Are Solved

Database queries involving the users and loginnames table were improved
and an index was added for user by email query.

# Additional Changes

- spellchecks
- updated apis on load tests

# additional info

needs cherry pick to v3
2025-06-06 08:48:29 +00:00

52 lines
1.1 KiB
SQL

SELECT
u.id
, u.creation_date
, u.change_date
, u.resource_owner
, u.sequence
, u.state
, u.type
, u.username
, login_names.login_names AS login_names
, login_names.preferred_login_name AS preferred_login_name
, h.user_id
, h.first_name
, h.last_name
, h.nick_name
, h.display_name
, h.preferred_language
, h.gender
, h.avatar_key
, n.user_id
, n.last_email
, n.verified_email
, n.last_phone
, n.verified_phone
, n.password_set
, count(*) OVER ()
FROM projections.users14 u
LEFT JOIN
projections.users14_humans h
ON
u.id = h.user_id
AND u.instance_id = h.instance_id
LEFT JOIN
projections.users14_notifications n
ON
u.id = n.user_id
AND u.instance_id = n.instance_id
LEFT JOIN LATERAL (
SELECT
ARRAY_AGG(ln.login_name ORDER BY ln.login_name) AS login_names,
MAX(CASE WHEN ln.is_primary THEN ln.login_name ELSE NULL END) AS preferred_login_name
FROM
projections.login_names3 AS ln
WHERE
ln.user_id = u.id
AND ln.instance_id = u.instance_id
) AS login_names ON TRUE
WHERE
u.id = $1
AND u.instance_id = $2
LIMIT 1
;