Trying to speed up the query

This commit is contained in:
Asaf Gartner 2024-07-06 11:50:05 +03:00
parent 07154fb8b4
commit 3a492059de
1 changed files with 27 additions and 14 deletions

View File

@ -69,7 +69,32 @@ func FetchTimeline(
qb.Add( qb.Add(
` `
WITH snippet_item AS ( WITH visible_project AS (
SELECT *
FROM project
WHERE
$? = true
OR project.id = $?
OR (SELECT count(*) > 0 FROM user_project WHERE project_id = project.id AND user_id = $?)
OR (
project.lifecycle = ANY($?)
AND NOT project.hidden
AND (SELECT every(hmn_user.status = $?)
FROM user_project
JOIN hmn_user ON hmn_user.id = user_project.user_id
WHERE user_project.project_id = project.id
)
)
),`,
currentUserIsAdmin,
models.HMNProjectID,
currentUserId,
models.VisibleProjectLifecycles,
models.UserStatusApproved,
)
qb.Add(
`
snippet_item AS (
SELECT id, SELECT id,
"when", "when",
'snippet' AS timeline_type, 'snippet' AS timeline_type,
@ -155,22 +180,10 @@ func FetchTimeline(
FROM post FROM post
JOIN thread ON thread.id = thread_id JOIN thread ON thread.id = thread_id
JOIN post_version ON post_version.id = current_id JOIN post_version ON post_version.id = current_id
JOIN project ON project.id = post.project_id JOIN visible_project ON visible_project.id = post.project_id
WHERE WHERE
post.deleted = false AND thread.deleted = false post.deleted = false AND thread.deleted = false
AND ($? = true OR project.id = $? OR (
project.lifecycle = ANY($?) AND NOT project.hidden
AND (SELECT bool_or(user_project.user_id = $?) OR bool_and(hmn_user.status = $?)
FROM user_project
JOIN hmn_user ON hmn_user.id = user_project.user_id
WHERE user_project.project_id = project.id) = true
))
`, `,
currentUserIsAdmin,
models.HMNProjectID,
models.VisibleProjectLifecycles,
currentUserId,
models.UserStatusApproved,
) )
if len(q.OwnerIDs)+len(q.ProjectIDs) > 0 { if len(q.OwnerIDs)+len(q.ProjectIDs) > 0 {
qb.Add(`AND (`) qb.Add(`AND (`)