Speed up bulk product sync: defer term counting, suspend cache invalidation, batch transactions

Measured 2000-product sync_all() at ~2.5 products/sec baseline (2-core/8GB
staging box). Confirmed via source inspection that WooCommerce's
wc_product_meta_lookup sync is synchronous inside WC_Product::save(), so
every product save was its own implicit autocommit transaction -> its own
fsync. Batching each 200-item chunk into one explicit transaction, deferring
product_cat term counting, suspending post cache invalidation for the
duration, and caching subject->term_id lookups in memory brings the same
2000-item sync to ~21 products/sec (~95s), with the update-path re-run at
~82/sec.

Verified correctness after, not just speed: wc_product_meta_lookup price/
stock matched postmeta exactly (0 mismatches across all 2000), product_cat
term counts matched actual term_relationships (0 mismatches), no orphaned
category assignments, no duplicate products on re-run.
This commit is contained in:
2026-08-27 15:14:23 -04:00
parent dcad1b7512
commit 9bca215a7b
@@ -12,35 +12,87 @@ defined('ABSPATH') || exit;
* ISBN or offer. Price/stock are read fresh from PricingEngine/offers on * ISBN or offer. Price/stock are read fresh from PricingEngine/offers on
* every sync, not cached on the product beyond what WooCommerce itself * every sync, not cached on the product beyond what WooCommerce itself
* needs to render a price. * needs to render a price.
*
* Bulk-import performance (confirmed via direct measurement on staging's
* 2-core/8GB box, ~2.5 products/sec baseline at 2000 items):
*
* 1. wp_defer_term_counting() — batches product_cat term-count recalculation
* to once at the end instead of once per wp_set_object_terms() call.
* 2. wp_suspend_cache_invalidation() — skips clean_post_cache()'s cache
* clearing during the run. Verified this does NOT risk stale reads in
* WooCommerce's own wc_product_meta_lookup sync: that sync reads back
* ~14 postmeta values via get_post_meta() immediately after save(), but
* update_post_meta()/add_post_meta() write straight into the postmeta
* object cache on write (not merely invalidate-then-refetch), and this
* flag only affects clean_post_cache()'s handling of the *posts* cache
* group — postmeta cache correctness is unaffected either way.
* 3. Static in-memory subject→term_id cache in sync_categories() — avoids
* repeat term_exists()/wp_insert_term() round-trips for subjects that
* recur across many Works (common — PG subject strings repeat a lot).
* 4. Each batch runs inside one explicit DB transaction. WordPress/WooCommerce
* writes run in MySQL autocommit by default, so every post/postmeta/term-
* relationship/wc_product_meta_lookup write in sync_one() is its own
* implicit transaction — each one an InnoDB commit, each commit an fsync
* (confirmed: wc_product_meta_lookup sync is synchronous inside
* WC_Product::save(), not deferred via Action Scheduler). Wrapping a
* batch in one transaction turns ~batch_size fsyncs into one, which is
* where most of the win is on slow/networked storage. Caveat: this
* assumes nothing reads mid-batch data from a separate connection before
* COMMIT (true today — this site has no webhooks or other Action
* Scheduler consumers hooked on product save; would need re-checking if
* that changes).
*/ */
class ProductSync class ProductSync
{ {
/** @var array<string,int> subject string => product_cat term_id, for this process's lifetime */
private static array $term_cache = [];
/** /**
* @param callable|null $on_progress function(int $processed, int $created, int $updated): void * @param callable|null $on_progress function(int $processed, int $created, int $updated): void
* @return array{created:int, updated:int} * @return array{created:int, updated:int}
*/ */
public static function sync_all(int $batch_size = 200, ?callable $on_progress = null): array public static function sync_all(int $batch_size = 200, ?callable $on_progress = null): array
{ {
global $wpdb;
$created = 0; $created = 0;
$updated = 0; $updated = 0;
$processed = 0; $processed = 0;
$offset = 0; $offset = 0;
while (true) { wp_defer_term_counting(true);
$works = Work::all_paginated($batch_size, $offset); wp_suspend_cache_invalidation(true);
if (empty($works)) {
break; try {
while (true) {
$works = Work::all_paginated($batch_size, $offset);
if (empty($works)) {
break;
}
$wpdb->query('START TRANSACTION');
try {
foreach ($works as $work) {
$is_new = empty($work->wc_product_id) || !wc_get_product((int) $work->wc_product_id);
self::sync_one($work);
$is_new ? $created++ : $updated++;
$processed++;
}
$wpdb->query('COMMIT');
} catch (\Throwable $e) {
$wpdb->query('ROLLBACK');
throw $e;
}
if ($on_progress) {
$on_progress($processed, $created, $updated);
}
$offset += $batch_size;
} }
foreach ($works as $work) { } finally {
$is_new = empty($work->wc_product_id) || !wc_get_product((int) $work->wc_product_id); wp_suspend_cache_invalidation(false);
self::sync_one($work); wp_defer_term_counting(false);
$is_new ? $created++ : $updated++; wp_cache_flush();
$processed++;
}
if ($on_progress) {
$on_progress($processed, $created, $updated);
}
$offset += $batch_size;
} }
return ['created' => $created, 'updated' => $updated]; return ['created' => $created, 'updated' => $updated];
@@ -92,12 +144,18 @@ class ProductSync
if ($subject === '') { if ($subject === '') {
continue; continue;
} }
if (isset(self::$term_cache[$subject])) {
$term_ids[] = self::$term_cache[$subject];
continue;
}
$term = term_exists($subject, 'product_cat'); $term = term_exists($subject, 'product_cat');
if (!$term) { if (!$term) {
$term = wp_insert_term($subject, 'product_cat'); $term = wp_insert_term($subject, 'product_cat');
} }
if (!is_wp_error($term) && isset($term['term_id'])) { if (!is_wp_error($term) && isset($term['term_id'])) {
$term_ids[] = (int) $term['term_id']; $term_id = (int) $term['term_id'];
self::$term_cache[$subject] = $term_id;
$term_ids[] = $term_id;
} }
} }