Skip to content

Speed up the awarded certificates and achievements lists - #3489

Merged
brianhogg merged 2 commits into
devfrom
fix/awarded-list-user-lookup
Nov 16, 2026
Merged

brianhogg merged 2 commits into
devfrom
fix/awarded-list-user-lookup

Conversation

@brianhogg

@brianhogg brianhogg commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

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 filters wp_lifterlms_user_postmeta only on meta_key and meta_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_Handler writes post_author and the _{type}_earned row for the same user, and the 6.0.0 migration copied the user to post_author for older awards. The lookup now also filters on user_id = post_author when the award has an author, so it uses the existing user_id index. 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:

Request Stock 11.0.1 With this change
Awarded certificates list (3 loads) 45.6–51.2 s server time, 44.6–50.1 s in the database 1.1–2.4 s server time, 0.22–0.33 s in the database
One certificate lookup 2.6–3.1 s about 0.009 s
Orders & Transactions while 4 certificate lists load no response in 150 s 3.1 s to first byte
The 4 certificate lists in that test 2 took about 110 s, 2 got no response in 150 s 2.6–5.3 s

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 = 21948 in 0.0092 s.

Locally on MariaDB 11.4 with a synthetic 3-million-row lifterlms_user_postmeta table: 2.9–3.5 s before, 0.7 ms after (EXPLAIN: ref on user_id, 204 rows examined).

php -l and PHPCS (phpcs.xml) pass on the changed file. PHPUnit wasn't run locally; the existing get_user_postmeta(), get_user_id(), and get_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:

  • This PR requires and contains at least one changelog file.
  • My code has been tested.
  • My code passes all existing automated tests.
  • My code follows the LifterLMS Coding & Documentation Standards.

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>
@brianhogg brianhogg moved this to Awaiting Review in Development Nov 16, 2026
@brianhogg brianhogg moved this from Awaiting Review to Review in Progress in Development Nov 16, 2026
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>
@brianhogg
brianhogg merged commit a0119d8 into dev Nov 16, 2026
16 checks passed
@brianhogg
brianhogg deleted the fix/awarded-list-user-lookup branch November 16, 2026 19:33
@github-project-automation github-project-automation Bot moved this from Review in Progress to Done in Development Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants