I am trying to optimize the schemas for tables by removing redundant keys. Both the percona toolkit and common_schema tell me that the following key is redundant:
mysql> SELECT redundant_index_name, sql_drop_index FROM redundant_keys;
+----------------------+-------------------------------------------------------------------------------+
| redundant_index_name | sql_drop_index |
+----------------------+-------------------------------------------------------------------------------+
| deviceName | ALTER TABLE `reporting`.`tbCardData` DROP INDEX `deviceName` |
+----------------------+-------------------------------------------------------------------------------+
1 rows in set (0.18 sec)
mysql> show create table `reporting`.`tbCardData`;
CREATE TABLE `tbCardData` (
`pkCardDataId` bigint(12) unsigned NOT NULL AUTO_INCREMENT,
`deviceName` varchar(64) DEFAULT NULL,
`shelfId` smallint(3) unsigned DEFAULT NULL,
`cardId` smallint(3) unsigned DEFAULT NULL,
`cardName` varchar(64) DEFAULT NULL,
`cardType` smallint(3) unsigned DEFAULT NULL,
`cardSubType` smallint(3) unsigned DEFAULT NULL,
`cardSpareGroupId` smallint(3) unsigned DEFAULT NULL,
`cardSerialNum` varchar(64) DEFAULT NULL,
`cardCarrierSerialNum` varchar(64) DEFAULT NULL,
`dom` tinyint(2) unsigned NOT NULL DEFAULT '0',
`updateTime` int(11) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`pkCardDataId`),
UNIQUE KEY `devchascarddom` (`deviceName`,`shelfId`,`cardId`,`dom`),
KEY `deviceName` (`deviceName`),
KEY `dom` (`dom`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
I understand that deviceName Key and the unique key devchascarddom share the leftmost attribute, deviceName, but it would seem to me that the Unique Key occurs once, whereas there are several deviceNames in the list. I guess what i am saying is, dropping the Key deviceName doesn't seem to make sense to me here, but i am no mysql guru -- should i drop it or is this just the way these tools are reporting back to me that i'll have to discard?
MySQL can use the first part of the compound index devchascarddom
in the same way it can use deviceName
. These tools are telling you the truth. The deviceName
index will be smaller, and if you can get rid of devchascarddom
instead, that would be better. You'll have to look at the EXPLAIN
output for your queries to see if that's possible.