Repository navigation
Speed up the awarded certificates and achievements lists - #3489
Merged
Merged
Conversation
The awarded certificate/achievement user lookup filtered lifterlms_user_postmeta only on unindexed columns, so it scanned the whole table once per list row. Restrict it to the award's author, which is the earner, so it uses the user_id index. Fixes #3487 Amp-Thread-ID: https://ampcode.com/threads/T-01a1e448-7786-70b5-85e1-0b6cbb96299b Co-authored-by: brianhogg <brian@brianhogg.com>
Avoids interpolating a separately prepared clause into the SQL, which plugin checkers flag. With no author, `0 = 0` keeps the previous unfiltered lookup. Refs #3487 Amp-Thread-ID: https://ampcode.com/threads/T-01a1e448-7786-70b5-85e1-0b6cbb96299b Co-authored-by: brianhogg <brian@brianhogg.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
HS-282689
Description
The awarded certificates and achievements lists look up each row's earner with
LLMS_Abstract_User_Engagement::get_user_postmeta(). That query filterswp_lifterlms_user_postmetaonly onmeta_keyandmeta_value, which aren't indexed, so it reads the whole table once per row. On a site with a few million rows, that's about 2.5 s × 20 rows per page.Since 6.0.0, every award stores its earner as
post_author:LLMS_Engagement_Handlerwritespost_authorand the_{type}_earnedrow for the same user, and the 6.0.0 migration copied the user topost_authorfor older awards. The lookup now also filters onuser_id = post_authorwhen the award has an author, so it uses the existinguser_idindex. Awards without an author keep the previous query.The same lookup also runs when a certificate is viewed or merged and in the REST API awarded-certificates responses, so those paths speed up too.
Fixes #3487
How has this been tested?
On a Kinsta staging copy of a reporting site (LifterLMS 11.0.1, MariaDB, no persistent object cache, about 23,000 orders and transactions), the official 11.0.1 zip with only this change applied was installed, measured with Query Monitor, and then replaced with the stock 11.0.1 zip:
The User and Template columns of the list (20 rows) were compared between the two builds and are identical. Query Monitor confirmed the new query runs, for example
... WHERE meta_value = 477790 AND meta_key = '_certificate_earned' AND user_id = 21948in 0.0092 s.Locally on MariaDB 11.4 with a synthetic 3-million-row
lifterlms_user_postmetatable: 2.9–3.5 s before, 0.7 ms after (EXPLAIN:refonuser_id, 204 rows examined).php -land PHPCS (phpcs.xml) pass on the changed file. PHPUnit wasn't run locally; the existingget_user_postmeta(),get_user_id(), andget_related_post_id()tests for certificates and achievements cover this method and will run in CI.Screenshots
N/A
Types of changes
Bug fix (non-breaking change which fixes an issue)
Checklist: