Commit 6efafc73b3e for woocommerce
commit 6efafc73b3ed5cfe51804886b59537dda18b9893
Author: Néstor Soriano <konamiman@konamiman.com>
Date: Wed Apr 8 11:32:29 2026 +0200
Simplify the meta_key_value index in the wp_wc_orders_meta table (#63897)
diff --git a/plugins/woocommerce/changelog/pr-63897 b/plugins/woocommerce/changelog/pr-63897
new file mode 100644
index 00000000000..157b926cbcd
--- /dev/null
+++ b/plugins/woocommerce/changelog/pr-63897
@@ -0,0 +1,4 @@
+Significance: patch
+Type: update
+
+Modify the meta_key_value in wp_wc_orders_meta to only track the meta_key column
diff --git a/plugins/woocommerce/includes/class-wc-install.php b/plugins/woocommerce/includes/class-wc-install.php
index 016b3d6c80f..208637a793a 100644
--- a/plugins/woocommerce/includes/class-wc-install.php
+++ b/plugins/woocommerce/includes/class-wc-install.php
@@ -327,6 +327,7 @@ class WC_Install {
),
'10.8.0' => array(
'wc_update_1080_migrate_analytics_import_option',
+ 'wc_update_1080_slim_orders_meta_key_index',
),
);
diff --git a/plugins/woocommerce/includes/wc-update-functions.php b/plugins/woocommerce/includes/wc-update-functions.php
index d2178f97928..7813a09e06a 100644
--- a/plugins/woocommerce/includes/wc-update-functions.php
+++ b/plugins/woocommerce/includes/wc-update-functions.php
@@ -3459,3 +3459,39 @@ function wc_update_1080_migrate_analytics_import_option(): void {
delete_option( $legacy_option );
}
}
+
+/**
+ * Slim the `meta_key_value` index on `wc_orders_meta` by removing the `meta_value` column.
+ *
+ * The original composite index `(meta_key(100), meta_value(82))` overlaps heavily with
+ * `order_id_meta_key_meta_value` and the `meta_value` prefix adds significant storage
+ * overhead with negligible selectivity benefit. All core queries that use this index
+ * filter primarily by `meta_key`.
+ *
+ * @since 10.8.0
+ *
+ * @return void
+ */
+function wc_update_1080_slim_orders_meta_key_index(): void {
+ global $wpdb;
+
+ $table_name = $wpdb->prefix . 'wc_orders_meta';
+ $index_name = 'meta_key_value';
+
+ // phpcs:disable WordPress.DB.PreparedSQL.NotPrepared
+ $index = $wpdb->get_row(
+ $wpdb->prepare(
+ 'SHOW INDEX FROM ' . $table_name . ' WHERE Key_name = %s AND Column_name = %s',
+ $index_name,
+ 'meta_value'
+ )
+ );
+ // phpcs:enable WordPress.DB.PreparedSQL.NotPrepared
+
+ if ( is_null( $index ) ) {
+ return;
+ }
+
+ // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
+ $wpdb->query( "ALTER TABLE {$table_name} DROP INDEX {$index_name}, ADD INDEX {$index_name} (meta_key(100))" );
+}
diff --git a/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableDataStore.php b/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableDataStore.php
index 96f65413003..4ae8e0d70c5 100644
--- a/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableDataStore.php
+++ b/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableDataStore.php
@@ -3309,7 +3309,7 @@ CREATE TABLE $meta_table (
order_id bigint(20) unsigned null,
meta_key varchar(255),
meta_value text null,
- KEY meta_key_value (meta_key(100), meta_value($composite_meta_value_index_length)),
+ KEY meta_key_value (meta_key(100)),
KEY order_id_meta_key_meta_value (order_id, meta_key(100), meta_value($composite_meta_value_index_length))
) $collate;
";