Delete the (now redundant) product_or_parent_id_term_id index

in the product attributes lookup table.
This commit is contained in:
Nestor Soriano 2022-03-15 17:03:51 +01:00
parent 1caf5aa7da
commit eb94672f76
No known key found for this signature in database
GPG Key ID: 08110F3518C12CAD
2 changed files with 43 additions and 5 deletions

View File

@ -6,6 +6,7 @@
namespace Automattic\WooCommerce\Internal\ProductAttributesLookup;
use Automattic\WooCommerce\Internal\ProductAttributesLookup\LookupDataStore;
use Automattic\WooCommerce\Internal\Utilities\DatabaseUtil;
use Automattic\WooCommerce\Utilities\ArrayUtil;
defined( 'ABSPATH' ) || exit;
@ -466,27 +467,31 @@ class DataRegenerator {
term_id bigint(20) NOT NULL,
is_variation_attribute tinyint(1) NOT NULL,
in_stock tinyint(1) NOT NULL,
INDEX product_or_parent_id_term_id (product_or_parent_id, term_id),
INDEX is_variation_attribute_term_id (is_variation_attribute, term_id),
PRIMARY KEY (`product_id`, `product_or_parent_id`, `taxonomy`, `term_id`)
PRIMARY KEY ( `product_or_parent_id`, `term_id`, `product_id`, `taxonomy` )
) $collate;";
}
/**
* Create the primary key for the table if it doesn't exist already.
* It also deletes the product_or_parent_id_term_id index if it exists, since it's now redundant.
*
* @return void
*/
public function create_table_primary_index() {
global $wpdb;
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
// phpcs:disable WordPress.DB.PreparedSQL
$wpdb->query(
"
ALTER TABLE {$this->lookup_table_name}
ADD PRIMARY KEY IF NOT EXISTS (`product_id`, `product_or_parent_id`, `taxonomy`, `term_id`)"
ADD PRIMARY KEY IF NOT EXISTS ( `product_or_parent_id`, `term_id`, `product_id`, `taxonomy` )"
);
// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
// phpcs:enable WordPress.DB.PreparedSQL
wc_get_container()
->get( DatabaseUtil::class )
->drop_table_index( $this->lookup_table_name, 'product_or_parent_id_term_id' );
}
/**

View File

@ -70,4 +70,37 @@ class DatabaseUtil {
//phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
return $wpdb->query( "DROP TABLE IF EXISTS `{$table_name}`" );
}
/**
* Drops a table index, if both the table and the index exist.
*
* @param string $table_name The name of the table that contains the index.
* @param string $index_name The name of the index to be dropped.
* @return bool True if the index has been dropped, false if either the table or the index don't exist.
*/
public function drop_table_index( string $table_name, string $index_name ): bool {
global $wpdb;
// phpcs:disable WordPress.DB.PreparedSQL
$index_count = intval(
$wpdb->get_var(
"
SELECT COUNT(1) FROM INFORMATION_SCHEMA.STATISTICS
WHERE table_name='$table_name'
AND table_schema='" . DB_NAME . "'
AND index_name='$index_name'"
)
);
if ( 0 === $index_count ) {
return false;
}
$wpdb->query( "ALTER TABLE $table_name DROP INDEX $index_name" );
// phpcs:enable WordPress.DB.PreparedSQL
return true;
}
}