performance: refactor sql queries to improve performance, ref. #19478.
templates: fix template that caused test fail.
views: fix failing test.