1
0

feat(subscription): use a recursive cte to fetch subscriptions of parent projects

Testing this locally resulted in improved response times from ~50ms to ~20ms when creating a project. It looks like even though the code running these sql queries uses different go routines, they affect each other (caused by IO or context switching?)
This commit is contained in:
kolaente
2024-03-03 15:31:42 +01:00
parent 22933dac4a
commit fe27dd59ad
2 changed files with 26 additions and 43 deletions

View File

@ -540,19 +540,24 @@ func getSavedFilterProjects(s *xorm.Session, doer *user.User) (savedFiltersProje
}
// GetAllParentProjects returns all parents of a given project
func (p *Project) GetAllParentProjects(s *xorm.Session) (err error) {
if p.ParentProjectID == 0 {
return
}
parent, err := GetProjectSimpleByID(s, p.ParentProjectID)
if err != nil {
return err
}
p.ParentProject = parent
return parent.GetAllParentProjects(s)
func GetAllParentProjects(s *xorm.Session, projectID int64) (allProjects map[int64]*Project, err error) {
allProjects = make(map[int64]*Project)
err = s.SQL(`WITH RECURSIVE all_projects AS (
SELECT
p.*
FROM
projects p
WHERE
p.id = ?
UNION ALL
SELECT
p.*
FROM
projects p
INNER JOIN all_projects pc ON p.ID = pc.parent_project_id
)
SELECT DISTINCT * FROM all_projects`, projectID).Find(&allProjects)
return
}
// addProjectDetails adds owner user objects and project tasks to all projects in the slice
@ -667,29 +672,9 @@ func checkProjectBeforeUpdateOrDelete(s *xorm.Session, project *Project) (err er
}
}
allProjects := make(map[int64]*Project)
err = s.SQL(`WITH RECURSIVE all_projects AS (
SELECT
p.id,
p.parent_project_id
FROM
projects p
WHERE
p.id = ?
UNION ALL
SELECT
p.id,
p.parent_project_id
FROM
projects p
INNER JOIN all_projects pc ON p.ID = pc.parent_project_id
)
SELECT
*
FROM
all_projects`, project.ParentProjectID).Find(&allProjects)
allProjects, err := GetAllParentProjects(s, project.ParentProjectID)
if err != nil {
return
return err
}
var parent *Project