diff --git a/src/wp-admin/includes/schema.php b/src/wp-admin/includes/schema.php index 911314ea2cbf3..4bfd038eac953 100644 --- a/src/wp-admin/includes/schema.php +++ b/src/wp-admin/includes/schema.php @@ -154,7 +154,8 @@ function wp_get_db_schema( $scope = 'all', $blog_id = null ) { meta_value longtext, PRIMARY KEY (meta_id), KEY post_id (post_id), - KEY meta_key (meta_key($max_index_length)) + KEY meta_key (meta_key($max_index_length)), + KEY meta_key_id (meta_key($max_index_length),post_id) ) $charset_collate; CREATE TABLE $wpdb->posts ( ID bigint(20) unsigned NOT NULL auto_increment, diff --git a/src/wp-admin/includes/upgrade.php b/src/wp-admin/includes/upgrade.php index 6adb0521ff295..c68fda8e0f54a 100644 --- a/src/wp-admin/includes/upgrade.php +++ b/src/wp-admin/includes/upgrade.php @@ -2508,6 +2508,26 @@ function upgrade_700() { ) ); } + + // Add composite index for meta_key and post_id on postmeta table. + if ( $wp_current_db_version < 61697 ) { + $max_index_length = 191; + // Check if the index already exists. + $index_exists = $wpdb->get_var( + $wpdb->prepare( + "SELECT COUNT(1) FROM INFORMATION_SCHEMA.STATISTICS + WHERE table_schema = %s + AND table_name = %s + AND index_name = 'meta_key_id'", + DB_NAME, + $wpdb->postmeta + ) + ); + + if ( ! $index_exists ) { + $wpdb->query( "ALTER TABLE $wpdb->postmeta ADD INDEX meta_key_id (meta_key($max_index_length),post_id)" ); + } + } } /** diff --git a/src/wp-includes/version.php b/src/wp-includes/version.php index 9bf095bff6bdb..b560d5ce2e0b6 100644 --- a/src/wp-includes/version.php +++ b/src/wp-includes/version.php @@ -23,7 +23,7 @@ * * @global int $wp_db_version */ -$wp_db_version = 61696; +$wp_db_version = 61697; /** * Holds the TinyMCE version. diff --git a/tests/phpunit/tests/admin/includesSchema.php b/tests/phpunit/tests/admin/includesSchema.php index 9f2d19f0c16aa..2d32c2058293d 100644 --- a/tests/phpunit/tests/admin/includesSchema.php +++ b/tests/phpunit/tests/admin/includesSchema.php @@ -331,4 +331,68 @@ public function data_populate_network_meta() { ), ); } + + /** + * Test that upgrade_700() adds the composite index to postmeta table. + */ + public function test_upgrade_700_adds_postmeta_composite_index() { + global $wpdb, $wp_current_db_version; + + require_once ABSPATH . 'wp-admin/includes/upgrade.php'; + + // Check if index exists first. + $index_exists = $wpdb->get_var( + $wpdb->prepare( + "SELECT COUNT(1) FROM INFORMATION_SCHEMA.STATISTICS + WHERE table_schema = %s + AND table_name = %s + AND index_name = 'meta_key_id'", + DB_NAME, + $wpdb->postmeta + ) + ); + + // Drop the index if it exists to simulate an upgrade scenario. + if ( $index_exists > 0 ) { + $wpdb->query( "ALTER TABLE $wpdb->postmeta DROP INDEX meta_key_id" ); + } + + // Verify the index doesn't exist. + $index_exists_before = $wpdb->get_var( + $wpdb->prepare( + "SELECT COUNT(1) FROM INFORMATION_SCHEMA.STATISTICS + WHERE table_schema = %s + AND table_name = %s + AND index_name = 'meta_key_id'", + DB_NAME, + $wpdb->postmeta + ) + ); + + $this->assertSame( 0, (int) $index_exists_before, 'The meta_key_id index should not exist before upgrade.' ); + + // Simulate an old database version. + $original_db_version = $wp_current_db_version; + $wp_current_db_version = 61696; + + // Run the upgrade function. + upgrade_700(); + + // Restore original version. + $wp_current_db_version = $original_db_version; + + // Verify the index now exists. + $index_exists_after = $wpdb->get_var( + $wpdb->prepare( + "SELECT COUNT(1) FROM INFORMATION_SCHEMA.STATISTICS + WHERE table_schema = %s + AND table_name = %s + AND index_name = 'meta_key_id'", + DB_NAME, + $wpdb->postmeta + ) + ); + + $this->assertGreaterThan( 0, $index_exists_after, 'The meta_key_id composite index should exist after upgrade_700().' ); + } } diff --git a/tests/phpunit/tests/db/dbDelta.php b/tests/phpunit/tests/db/dbDelta.php index 23ad16399609e..028d9bb976136 100644 --- a/tests/phpunit/tests/db/dbDelta.php +++ b/tests/phpunit/tests/db/dbDelta.php @@ -758,6 +758,32 @@ public function test_wp_get_db_schema_does_not_alter_queries_on_existing_install $this->assertEmpty( $updates ); } + /** + * Test that the postmeta table includes the composite index meta_key_id. + */ + public function test_postmeta_composite_index_in_schema() { + global $wpdb; + + $schema = wp_get_db_schema(); + + // Verify that the schema contains the composite index definition. + $this->assertStringContainsString( 'KEY meta_key_id', $schema, 'Schema should contain the meta_key_id composite index.' ); + + // Verify that the index exists in the actual database. + $index_exists = $wpdb->get_var( + $wpdb->prepare( + "SELECT COUNT(1) FROM INFORMATION_SCHEMA.STATISTICS + WHERE table_schema = %s + AND table_name = %s + AND index_name = 'meta_key_id'", + DB_NAME, + $wpdb->postmeta + ) + ); + + $this->assertGreaterThan( 0, $index_exists, 'The meta_key_id composite index should exist in the postmeta table.' ); + } + /** * @ticket 20263 */