From fc6cafa15f42516373e452e75eacacf981e70c2e Mon Sep 17 00:00:00 2001 From: gitgiggety <79809426+gitgiggety@users.noreply.github.com> Date: Tue, 17 Aug 2021 05:43:22 +0200 Subject: [PATCH] Fix hierarchical criteria performance issue (#1643) * Fix hierarchical criteria performance issue Don't apply recursive clause to hierarchical criteria when the depth is set to 0 (i.e.: no recursion is needed). This undoes the current performance penalty on for example the studios page. This as reported in #1519, using a database of 4M images, 30K scenes and 500 studios. Without this fix loading the studios overview, with the default of 40 items per page, takes 6 to 7 seconds. With this fix it only takes 0,07 seconds reverting the performance back to the pre-hierarchical filtering performance (tested against 508f7b84 which was the last commit before #1397 was merged). --- pkg/sqlite/filter.go | 23 +++++++++++++------ .../src/components/Changelog/versions/v090.md | 1 + 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/pkg/sqlite/filter.go b/pkg/sqlite/filter.go index cb8e56e97..1517bf99a 100644 --- a/pkg/sqlite/filter.go +++ b/pkg/sqlite/filter.go @@ -539,18 +539,27 @@ func addHierarchicalWithClause(f *filterBuilder, value []string, derivedTable, t depthCondition = fmt.Sprintf("WHERE depth < %d", depth) } - withClause := utils.StrFormat(`RECURSIVE {derivedTable} AS ( -SELECT id as id, id as child_id, 0 as depth FROM {table} -WHERE id in {inBinding} -UNION SELECT p.id, c.id, depth + 1 FROM {table} as c -INNER JOIN {derivedTable} as p ON c.{parentFK} = p.child_id {depthCondition}) -`, utils.StrFormatMap{ + withClauseMap := utils.StrFormatMap{ "derivedTable": derivedTable, "table": table, "inBinding": getInBinding(inCount), "parentFK": parentFK, "depthCondition": depthCondition, - }) + "unionClause": "", + } + + if depth != 0 { + withClauseMap["unionClause"] = utils.StrFormat(` +UNION SELECT p.id, c.id, depth + 1 FROM {table} as c +INNER JOIN {derivedTable} as p ON c.{parentFK} = p.child_id {depthCondition} +`, withClauseMap) + } + + withClause := utils.StrFormat(`RECURSIVE {derivedTable} AS ( +SELECT id as id, id as child_id, 0 as depth FROM {table} +WHERE id in {inBinding} +{unionClause}) +`, withClauseMap) f.addWith(withClause, args...) } diff --git a/ui/v2.5/src/components/Changelog/versions/v090.md b/ui/v2.5/src/components/Changelog/versions/v090.md index 51c28b421..69c23e827 100644 --- a/ui/v2.5/src/components/Changelog/versions/v090.md +++ b/ui/v2.5/src/components/Changelog/versions/v090.md @@ -19,6 +19,7 @@ * Added de-DE language option. ([#1578](https://github.com/stashapp/stash/pull/1578)) ### 🐛 Bug fixes +* Fix performance issue on Studios page getting studio image count. ([#1643](https://github.com/stashapp/stash/pull/1643)) * Regenerate scene phash if overwrite flag is set. ([#1633](https://github.com/stashapp/stash/pull/1633)) * Create .stash directory in $HOME only if required. ([#1623](https://github.com/stashapp/stash/pull/1623)) * Include stash id when scraping performer from stash-box. ([#1608](https://github.com/stashapp/stash/pull/1608))