Main при коментировании user', cc.user все становится норм, но более тяжеловесные связи не дают такоих тормазов)
with
cc as
(select
cc.*,
jsonb_build_object('id', "user".id, 'first_name', user.first_name, 'last_name', user.last_name, 'email', user.email, 'role', user.role) as "user",
from consultation_configuration cc
left join "user" u on cc.user_id = u.id),
-- duration
duration as
(select
cc.*,
jsonb_agg(jsonb_build_object('id', spwt.id, 'key', spwt.key, 'sort', spwt.sort, 'translations', spwt.translations)) as durations
from
consultation_configuration_duration ccp
join
(select
sp.*,
jsonb_agg(jsonb_build_object('id', sptwl.id, 'title', sptwl.title, 'entity_id', sptwl.entity_id, 'language', sptwl.language)) as translations
from ref.study_duration sp
join
(select
sdt.*,
jsonb_build_object('id', lws.id, 'title', lws.title, 'status', lws.status, 'key', lws.key, 'sort', lws.sort) as language
from ref.study_duration_translate sdt
join
(select
l.*,
jsonb_build_object('id', ls.id, 'title', ls.title, 'key', ls.key, 'sort', ls.sort) as status
from
ref.language l
join ref.language_status ls on l.language_status_id = ls.id) lws on sdt.language_id = lws.id) sptwl on sp.id = sptwl.entity_id
group by sp.id) spwt
on ccp.ref_study_duration_id = spwt.id
join consultation_configuration cc on ccp.consultation_configuration_id = cc.id
group by cc.id)
-- User
"user" as
(select
u.*,
jsonb_build_object('id', rtwt.id, 'key', rtwt.key, 'sort', rtwt.sort, 'translations', rtwt.translations) as role
from public.user u
left join
(select
r.*,
jsonb_agg(jsonb_build_object('id', rtwl.id, 'title', rtwl.title, 'entity_id', rtwl.entity_id, 'language', rtwl.language)) as translations
from ref.role r
left join
(select
rt.*,
jsonb_build_object('id', lws.id, 'title', lws.title, 'status', lws.status, 'key', lws.key, 'sort', lws.sort) as language
from ref.role_translate rt
left join lws on rt.language_id = lws.id) rtwl on r.id = rtwl.entity_id
group by r.id) rtwt on u.ref_role_id = rtwt.id)
-- Main
select
t1.*,
jsonb_build_object(
'user', cc.user,
) as consultation_configuration,
cc_program.program,
cc_duration.duration
from t1 ts
left join
(select
cc.*,
duration.durations
from consultation_configuration cc
join duration on cc.id = duration.id) cc on ts.consultation_configuration_id = cc.id
https://t.me/pgsql/303899
Обсуждают сегодня