Optimize get_claim_count() query for large stores#1313
Conversation
Query the small claims table with EXISTS instead of scanning millions of rows in the actions table. Falls back to the original query if the claims-table path fails. Adds tests for the optimized method. Co-Authored-By: Claude Opus 4.6 <[email protected]>
bor0
left a comment
There was a problem hiding this comment.
Approving based on previous WCCOM review and neat results 🎉
|
Hey @jorgeatorres 👋 Please let me know if you're the best person to review this PR. I'd appreciate someone else taking a look at it but I'm also happy to reassign review to someone else. Thanks! |
jorgeatorres
left a comment
There was a problem hiding this comment.
@mcliwanow: thanks for the PR! This looks good to me and the improvements are quite nice. I left some feedback about the fallback query so we can discuss, but that's pretty much it. Thanks again!
This path wouldn't be reached anyway, it was overly defensive coding
jorgeatorres
left a comment
There was a problem hiding this comment.
LGTM. Ran a few tests with concurrent batches and also with actions marked completed without releasing the claim to confirm that the get_claim_count() matched the expected values. Thanks for the PR @mcliwanow! 🥇
Copy the updated ActionScheduler_DBStore.php into wp content/plugins/woocommerce/packages/action-scheduler/classes/data-stores/ [...]
💡 For future reference, you can always clone this repo and add it to your test site as a normal WP plugin. While enabled, it'll override the version of A-S bundled in WC, so no need to modify WC itself (and no need to even have WC installed).
Oh nice. That's good to know. Thanks! |
Description
Optimizes
get_claim_count()inActionScheduler_DBStorefor stores with largeactionscheduler_actionstables.The original query scans the (potentially huge) actions table:
On a store with ~3M action rows, this took ~28 seconds to return 0.
The new query starts from the small actionscheduler_claims table and uses EXISTS to check the actions table:
This returns in ~0.078 seconds (350x faster), because the claims table only holds active claims (typically a handful of rows), and EXISTS stops scanning at the first match.
If the claims-table query fails (e.g. the table doesn't exist yet during migration), the method falls back to the original query shape and logs the error.
Ported from WCCOM
Test instructions
Setup
Run the unit tests to verify correctness:
./vendor/bin/phpunit tests/phpunit/jobstore/ActionScheduler_DBStore_Test.php \ -c tests/phpunit.xml.dist --filter test_get_claim_countAll 7 new test cases should pass (runs as 42 with timezone rotation).
Manual verification
Prerequisites: A local WooCommerce site. Copy the updated
ActionScheduler_DBStore.phpintowp-content/plugins/woocommerce/packages/action-scheduler/classes/data-stores/.wp-content/mu-plugins/log-claim-count.php:SAVEQUERIESfromwp-config.php