大家都知道对于分区字段必须是主键的一部分,那么建了复合主键之后,是否需要对分许字段再单独添加一个索引呢?有没有效果?来验证一下,下面话不多说了,来一起看看详细的介绍吧。
CREATE TABLE `effect_new` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`type` tinyint(4) NOT NULL DEFAULT '0',
`timezone` varchar(10) DEFAULT NULL,
`date` varchar(10) NOT NULL,
`hour` varchar(2) DEFAULT NULL,
`position` varchar(200) DEFAULT NULL,
`country` varchar(32) NOT NULL,
`create_time` datetime NOT NULL DEFAULT '1970-01-01 00:00:00',
PRIMARY KEY (`id`,`create_time`),
KEY `index_date_hour_coun` (`date`,`hour`,`country`)
) ENGINE=InnoDB AUTO_INCREMENT=983041 DEFAULT CHARSET=utf8
PARTITION BY RANGE (TO_DAYS (`create_time`))
(PARTITION p0 VALUES LESS THAN (736754) ENGINE = InnoDB,
PARTITION p1 VALUES LESS THAN (736785) ENGINE = InnoDB,
PARTITION p2 VALUES LESS THAN (736815) ENGINE = InnoDB,
PARTITION p3 VALUES LESS THAN (736846) ENGINE = InnoDB,
PARTITION p4 VALUES LESS THAN (736876) ENGINE = InnoDB,
PARTITION p5 VALUES LESS THAN (736907) ENGINE = InnoDB,
PARTITION p6 VALUES LESS THAN (736938) ENGINE = InnoDB,
PARTITION p7 VALUES LESS THAN (736968) ENGINE = InnoDB,
PARTITION p8 VALUES LESS THAN (736999) ENGINE = InnoDB,
PARTITION p9 VALUES LESS THAN (737029) ENGINE = InnoDB,
PARTITION p10 VALUES LESS THAN (737060) ENGINE = InnoDB);
INSERT INTO `effect_new` (`id`, `type`, `timezone`, `date`, `hour`, `position`, `country`, `create_time`) VALUES ('1', '0', 'GMT+8', '2017-07-01', '', 'M-NotiCleanFull-FamilyRecom-0026', '', '2017-07-02 00:07:02');
INSERT INTO `effect_new` (`id`, `type`, `timezone`, `date`, `hour`, `position`, `country`, `create_time`) VALUES ('2', '1', 'GMT+8', '2017-09-30', '23', 'Ma5dtJub', 'EG', '2017-10-01 00:00:00');
INSERT INTO `effect_new` (`id`, `type`, `timezone`, `date`, `hour`, `position`, `country`, `create_time`) VALUES ('3', '1', 'GMT+8', '2017-09-10', '10', '28', 'DZ', '2017-09-11 00:08:20');
INSERT INTO `effect_new` (`id`, `type`, `timezone`, `date`, `hour`, `position`, `country`, `create_time`) VALUES ('4', '1', 'GMT+8', '2017-02-03', '20', '32', 'AD', '2017-02-04 00:00:00');
INSERT INTO `effect_new` (`id`, `type`, `timezone`, `date`, `hour`, `position`, `country`, `create_time`) VALUES ('5', '0', 'GMT+8', '2017-03-05', '2', NULL, 'AI', '2017-03-06 02:10:00');
INSERT INTO `effect_new` (`id`, `type`, `timezone`, `date`, `hour`, `position`, `country`, `create_time`) VALUES ('6', '0', 'GMT+8', '2017-09-23', '13', 'M-BrandSplash-S-0038', 'AG', '2017-09-23 13:00:00');
INSERT INTO `effect_new` (`id`, `type`, `timezone`, `date`, `hour`, `position`, `country`, `create_time`) VALUES ('7', '1', NULL, '2017-10-13', '12', 'BB-Main-AppAd-0018', 'AF', '2017-10-14 12:00:00');
INSERT INTO `effect_new` (`id`, `type`, `timezone`, `date`, `hour`, `position`, `country`, `create_time`) VALUES ('8', '0', 'GMT+8', '2017-10-28', '2', 'M-ChargeReminder-S-0040', 'AE', '2017-10-29 00:00:00');
INSERT INTO `effect_new` (`id`, `type`, `timezone`, `date`, `hour`, `position`, `country`, `create_time`) VALUES ('9', '1', 'GMT+8', '2017-10-09', NULL, '30', 'AI', '2017-10-10 00:09:00');
INSERT INTO `effect_new` (`id`, `type`, `timezone`, `date`, `hour`, `position`, `country`, `create_time`) VALUES ('10', '0', 'GMT+8', '2017-10-05', '5', ' M-BrandSplash', 'LA', '2017-10-06 05:10:00');
有时候,主键不等于分区依据列,这时候主键又想建聚集索引的话,那么必须包含分区依据列,搞成复合主键。那么,这种情况下,分区依据列不就有索引了吗?是的,可是它不够快,如果在这个复合索引里面,分区依据列不排在第一位,就不够快,如果查找语句里常常用分区依据列作为过滤条件,就有必要为分区依据列额外单独建立一个索引。