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