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).
This commit is contained in:
gitgiggety 2021-08-17 05:43:22 +02:00 committed by GitHub
parent 0fc5a06332
commit fc6cafa15f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 7 deletions

View file

@ -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...)
}

View file

@ -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))