Skip to content

MBS-14548: Add tag vote count indexes to entity tag tables - #3916

Open
adwitiyabisht wants to merge 1 commit into
metabrainz:masterfrom
adwitiyabisht:master
Open

adwitiyabisht wants to merge 1 commit into
metabrainz:masterfrom
adwitiyabisht:master

Conversation

@adwitiyabisht

@adwitiyabisht adwitiyabisht commented Nov 20, 2026 •

Copy link
Copy Markdown

Problem

Fix MBS-14548.

Entity tag page queries (find_entities in lib/MusicBrainz/Server/Data/EntityTag.pm) filter {entity}_tag by tag and positive vote count, then order results by vote count. The existing tag-only indexes do not efficiently support this filtering and ordering together, which can cause PostgreSQL to process and sort many rows before applying pagination.

Solution

Added a partial composite index on each of the 11 *_tag tables:

CREATE INDEX <entity>_tag_idx_tag_count
    ON <entity>_tag (tag, count, <entity>)
    WHERE count > 0;

Tables covered: area_tag, artist_tag, event_tag, instrument_tag, label_tag, place_tag, recording_tag, release_tag, release_group_tag, series_tag and work_tag. These are all the aggregated *_tag tables in CreateTables.sql, and they match the entity types that use the shared tag role. The existing *_idx_tag indexes are left in place.

Also updated:

  • admin/sql/CreateIndexes.sql
  • admin/sql/DropIndexes.sql
  • Added migration 20261120-tag-vote-count-indexes.sql (replaces the earlier single-table 20261120-release-group-tag-vote-count-index.sql)
  • Registered the migration in upgrade.json
  • added the migration to the schema-change 32 script.

AI usage

  • Used Gemini during development to assist with exploring the repository's migration conventions and reviewing the implementation and PostgreSQL execution plans.
  • Used ChatGPT to format this PR description.
  • The implementation and database testing were performed and verified locally. I understand the changes made and can explain the implementation and test results.

Testing

Tested locally using Docker and PostgreSQL with EXPLAIN (ANALYZE, BUFFERS).

Dataset: Tag ID 7, 6,160 rows with positive vote counts, page size 50.

Query:

SELECT tt.count, release_group.id, release_group.gid, release_group.name
FROM release_group
JOIN release_group_tag tt
  ON release_group.id = tt.release_group
WHERE tt.tag = 7
  AND tt.count > 0
ORDER BY tt.count DESC, release_group.name COLLATE musicbrainz, release_group.id
LIMIT ? OFFSET ?;

Measurements

Query Without index With index
LIMIT 50 OFFSET 0 6.276 ms 0.155 ms
LIMIT 50 OFFSET 500 7.154 ms 1.722 ms
LIMIT 50 OFFSET 4900 12.431 ms 11.240 ms

Times are medians of five local executions for each configuration. The first single-run measurements for OFFSET 0 were 60.628 ms without the index and 11.065 ms with the index. The table reports medians from five executions.

The measurements were taken on a local development database and are not directly comparable to the replication-mirror measurements in the issue. Repeated runs also benefited from PostgreSQL's cache.

Timings were only measured on release_group_tag. The other 10 tables use the same query shape and index definition.
As an additional execution-plan check, EXPLAIN on artist_tag (whose ORDER BY uses sort_name as a tiebreaker) shows a backward Index Only Scan using artist_tag_idx_tag_count, followed by an Incremental Sort with tt.count as the presorted key. This was a plan check on a small fixture, not a performance measurement.

@mwiencek

Copy link
Copy Markdown
Member

Thanks! Presumably this would also help with the other ${entity}_tag tables, and we'd want to add indices to those for parity. Since you're using AI, it should be trivial to ask your agent to implement the rest.

@adwitiyabisht

Copy link
Copy Markdown
Author

Thank you @mwiencek. I will extend the changes to other applicable tables and update the PR shortly.

@adwitiyabisht adwitiyabisht changed the title MBS-14548: Add release group tag vote count index MBS-14548: Add tag vote count indexes to entity tag tables Nov 23, 2026
@adwitiyabisht

Copy link
Copy Markdown
Author

Updated the implementation to add the partial index to all 11 *_tag tables, as suggested.

Performance benchmarks were run on release_group_tag and EXPLAIN check on artist_tag confirmed that PostgreSQL also uses the new index there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants