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 <gpe@openrs2.org>
bzip2
Graham 4 years ago
parent 1cbd97d3c9
commit a920570f04
  1. 11
      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) SELECT a.master_index_id, COUNT(*), COUNT(i.container_id)
FROM master_index_archives a FROM master_index_archives a
LEFT JOIN groups g ON g.archive_id = 255 AND g.group_id = a.archive_id::INTEGER AND 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 g.version = a.version AND NOT g.version_truncated AND
LEFT JOIN containers c ON c.id = g.container_id AND c.crc32 = a.crc32 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 LEFT JOIN indexes i ON i.container_id = g.container_id AND i.version = a.version
GROUP BY a.master_index_id; GROUP BY a.master_index_id;
@ -172,7 +173,7 @@ CREATE MATERIALIZED VIEW master_index_group_stats (master_index_id, groups, vali
SELECT SELECT
v.master_index_id, v.master_index_id,
COUNT(*), COUNT(*),
COUNT(c.id), COUNT(g.container_id),
COUNT(*) FILTER (WHERE c.encrypted), 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)) 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 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 ( 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 AND NOT g.version_truncated) OR
(g.version = ig.version & 65535 AND g.version_truncated) (g.version = ig.version & 65535 AND g.version_truncated)
) ) AND g.container_id IN (SELECT id FROM containers WHERE crc32 = ig.crc32)
LEFT JOIN containers c ON c.id = g.container_id AND c.crc32 = ig.crc32 LEFT JOIN containers c ON c.id = g.container_id
LEFT JOIN keys k ON k.id = c.key_id LEFT JOIN keys k ON k.id = c.key_id
GROUP BY v.master_index_id; GROUP BY v.master_index_id;

Loading…
Cancel
Save