Commit 9d0dd71b7e for woocommerce

commit 9d0dd71b7ec7e98cee7f2f143d5f3da34fb76683
Author: David Stone <david@nnucomputerwhiz.com>
Date:   Fri Jan 16 08:55:55 2026 -0700

    Optimize switch_blog hook (#60174)

    * Optimize switch_blog hook

    `switch_blog` is triggered in a multisite install when iterating over
    many sites. With large network this will result in the hook being triggerd
    hundreds or thousands of times during a single request, this results in the
    `$wpdb->tables` array being filled with duplicate table names. This optimization
    only modifies prefixed table and stops filling the array with duplicates.

    * Add changefile(s) from automation for the following project(s): woocommerce

    * Revert changing method name

    * Simplify code

    * remove unused code

    * Revert back

    * Improve changelog message

    * Add changefile(s) from automation for the following project(s): woocommerce

    * Add changefile(s) from automation for the following project(s): woocommerce

    ---------

    Co-authored-by: github-actions <github-actions@github.com>
    Co-authored-by: Michael Pretty <prettyboymp@users.noreply.github.com>
    Co-authored-by: woocommercebot <woocommercebot@users.noreply.github.com>

diff --git a/plugins/woocommerce/changelog/60174-improve-switch-blog b/plugins/woocommerce/changelog/60174-improve-switch-blog
new file mode 100644
index 0000000000..34923237d5
--- /dev/null
+++ b/plugins/woocommerce/changelog/60174-improve-switch-blog
@@ -0,0 +1,4 @@
+Significance: patch
+Type: performance
+
+Prevent duplicate table names from accumulating in $wpdb->tables during multisite blog switching.
\ No newline at end of file
diff --git a/plugins/woocommerce/includes/class-woocommerce.php b/plugins/woocommerce/includes/class-woocommerce.php
index 2340d3c809..257e53834f 100644
--- a/plugins/woocommerce/includes/class-woocommerce.php
+++ b/plugins/woocommerce/includes/class-woocommerce.php
@@ -533,8 +533,10 @@ final class WooCommerce {
 		);

 		foreach ( $tables as $name => $table ) {
-			$wpdb->$name    = $wpdb->prefix . $table;
-			$wpdb->tables[] = $table;
+			$wpdb->$name = $wpdb->prefix . $table;
+			if ( ! in_array( $table, $wpdb->tables, true ) ) {
+				$wpdb->tables[] = $table;
+			}
 		}
 	}