Skip to content

Join resource updates and versions by resource_id - #108

Open
pavog wants to merge 1 commit into
SpigotMC:masterfrom
pavog:join-updates-by-resource-id
Open

pavog wants to merge 1 commit into
SpigotMC:masterfrom
pavog:join-updates-by-resource-id

Conversation

@pavog

@pavog pavog commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

getResourceUpdates and getResourceUpdate join xf_resource_update and xf_resource_version on resource_update_id.
xf_resource_version has no index on that column, only PRIMARY and resource_id_release_date (resource_id, release_date).
So MySQL reads the whole xf_resource_version table for every request, even for a resource with a single update.
With about 550k versions this takes between 30 and 300 ms of database time per request, on every page.

This PR adds resource_id to the join condition, so MySQL can use the existing index resource_id_release_date.
getResourceUpdates now filters by rv.resource_id instead of r.resource_id.
PDO sends the id as a string, and then MySQL does not carry the filter on r.resource_id over to rv.
It would read all versions of the resource again for every update, about 900,000 rows on a late page of a resource with 1,000 updates.

Deliberate decisions:

  • No new index. The API should not change the schema of XenForo, and the existing index is enough.
  • The responses stay the same. I compared some requests (all actions, several pages and orders, missing ids, ...).

Not covered on purpose:

  • The new condition assumes that a version belongs to the same resource as its update.
    XenForo always sets it like this, but imported or manually changed data could differ.
    This query should return 0 (it reads both tables once):
    SELECT COUNT(*) FROM xf_resource_version rv INNER JOIN xf_resource_update ru ON ru.resource_update_id = rv.resource_update_id WHERE ru.resource_id != rv.resource_id;
  • I measured with MySQL 8.0.32 only. Other versions or MariaDB may choose other plans, but all of them can use the index now.
  • PR Add limit parameter to paginated actions #109 changes the same line in getResourceUpdates().
    Whichever is merged second needs $this->_resource_update('AND rv.resource_id = :resource_id', $limit, $offset, $order, $direction).

Benchmark

Measured locally with the tables and indexes of XenForo 1.5.24 and Resource Manager 1.1.2, MySQL 8.0.32, PHP 7.0.33 (PHP-FPM, nginx).
The database holds about as much data as SpigotMC has according to Spiget: ~92k resources, ~465k updates plus one description each, ~552k versions.
Database time and rows examined come from MySQL's performance_schema, every number is the median of three rounds.

Scenario master this PR
All updates of 300 random resources (421 requests) 69.4 s, 287,304,974 rows examined 44.5 ms, 21,410 rows examined
All updates of a resource with 1,001 updates (99 requests) 29.8 s, 109,443,707 rows examined 118 ms, 245,321 rows examined
getResourceUpdate for 100 random updates 3,258 ms, 28,673,124 rows examined 9.1 ms, 4,297 rows examined
getResourceUpdates with 16 parallel clients 78 requests/s, median 117 ms 11,157 requests per sec, median 1.2 ms

Alternative: an index on resource_update_id

Instead of changing the query, an index on xf_resource_version.resource_update_id fixes the slow requests as well:

ALTER TABLE xf_resource_version ADD INDEX resource_update_id (resource_update_id), ALGORITHM=INPLACE, LOCK=NONE;

I measured both ways with the same data, and they are about equally fast and return exactly the same results:

Case master only the index only this PR
Resource with 1 update 1,105,036 rows, 305 ms 70 rows, 0.20 ms 102 rows, 0.14 ms
Resource with 1,001 updates, offset 900 1,106,895 rows, 296 ms 4,674 rows, 3.6 ms 3,879 rows, 1.1 ms
getResourceUpdate 297,325 rows, 31 ms 2 rows, 0.07 ms 19 rows, 0.08 ms

The index does not depend on versions belonging to the same resource as their update, and the API code stays as it is.
But it changes the schema of a XenForo table. This PR works without any change to the database, so I chose it.
The index takes about 8.5 MB for 550,000 versions and can be added without locking the table on MySQL 8.
Both together do no harm, so the index is still an option if the query above finds versions that belong to another resource.

@pavog

pavog commented Nov 30, 2026

Copy link
Copy Markdown
Contributor Author

If xf_resource_version already has an index on resource_update_id in your database, this PR will make much less of a difference than in my benchmarks.
I used the default tables of Resource Manager 1.1.2, which don't have that index.

You can check it with:

SHOW CREATE TABLE xf_resource_version;

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.

1 participant