From a920570f042ff27e476429d1caaea69dce2120fa Mon Sep 17 00:00:00 2001 From: Graham Date: Sun, 14 Feb 2021 22:41:32 +0000 Subject: [PATCH] Fix total archive, group and key counts The previous code over-counted as the use of LEFT JOINs meant candidate group rows with the incorrect container_id were still included in the results. Using an IN clause with a subquery allows us to remove those rows, though it's a bit hacky. (Really I want to be able to use a JOIN on the right side of a LEFT JOIN to restrict the rows that appear in the results of the LEFT JOIN, but that doesn't seem to be possible.) Signed-off-by: Graham --- .../main/resources/org/openrs2/archive/V1__init.sql | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/archive/src/main/resources/org/openrs2/archive/V1__init.sql b/archive/src/main/resources/org/openrs2/archive/V1__init.sql index 80a7d275..de8a47e0 100644 --- a/archive/src/main/resources/org/openrs2/archive/V1__init.sql +++ b/archive/src/main/resources/org/openrs2/archive/V1__init.sql @@ -161,8 +161,9 @@ CREATE MATERIALIZED VIEW master_index_archive_stats (master_index_id, indexes, v SELECT a.master_index_id, COUNT(*), COUNT(i.container_id) FROM master_index_archives a LEFT JOIN groups g ON g.archive_id = 255 AND g.group_id = a.archive_id::INTEGER AND - g.version = a.version AND NOT g.version_truncated -LEFT JOIN containers c ON c.id = g.container_id AND c.crc32 = a.crc32 + g.version = a.version AND NOT g.version_truncated AND + g.container_id IN (SELECT id FROM containers WHERE crc32 = a.crc32) +LEFT JOIN containers c ON c.id = g.container_id LEFT JOIN indexes i ON i.container_id = g.container_id AND i.version = a.version GROUP BY a.master_index_id; @@ -172,7 +173,7 @@ CREATE MATERIALIZED VIEW master_index_group_stats (master_index_id, groups, vali SELECT v.master_index_id, COUNT(*), - COUNT(c.id), + COUNT(g.container_id), COUNT(*) FILTER (WHERE c.encrypted), COUNT(*) FILTER (WHERE c.key_id IS NOT NULL OR (c.empty_loc IS NOT NULL AND c.empty_loc)) FROM master_index_valid_indexes v @@ -180,8 +181,8 @@ JOIN index_groups ig ON ig.container_id = v.container_id LEFT JOIN groups g ON g.archive_id = v.archive_id AND g.group_id = ig.group_id AND ( (g.version = ig.version AND NOT g.version_truncated) OR (g.version = ig.version & 65535 AND g.version_truncated) -) -LEFT JOIN containers c ON c.id = g.container_id AND c.crc32 = ig.crc32 +) AND g.container_id IN (SELECT id FROM containers WHERE crc32 = ig.crc32) +LEFT JOIN containers c ON c.id = g.container_id LEFT JOIN keys k ON k.id = c.key_id GROUP BY v.master_index_id;