MySQL子分区

时间:2013-01-08 07:42:53

标签: mysql partitioning

我有以下目前已分区的MySQL表。

我想在'message_read'键上添加子分区 - 这是为了优化通过分区搜索未读取的消息。

CREATE TABLE `messages` (
  `pid` int(50) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Primary Key',
  `userid1` int(10) unsigned NOT NULL COMMENT 'Message Sender',
  `userid2` int(10) unsigned NOT NULL COMMENT 'Message Receiver',
  `message` text NOT NULL COMMENT 'Actual Message',
  `message_read` tinyint(1) NOT NULL DEFAULT '0' COMMENT '0=Not Read & 1=Read',
  `datetime` datetime NOT NULL COMMENT 'Date Time',
  PRIMARY KEY (`pid`,`datetime`),
  KEY `userid1` (`userid1`),
  KEY `userid2` (`userid2`),
  KEY `message_read` (`message_read`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
/*!50100 PARTITION BY RANGE (TO_DAYS(`datetime`))
(PARTITION p0 VALUES LESS THAN (735234) ENGINE = InnoDB,
 PARTITION p1 VALUES LESS THAN (735265) ENGINE = InnoDB,
 PARTITION p2 VALUES LESS THAN (735293) ENGINE = InnoDB,
 PARTITION p3 VALUES LESS THAN (735324) ENGINE = InnoDB,
 PARTITION p4 VALUES LESS THAN (735354) ENGINE = InnoDB,
 PARTITION p5 VALUES LESS THAN (735385) ENGINE = InnoDB,
 PARTITION p6 VALUES LESS THAN (735415) ENGINE = InnoDB) */

当前分区在日期时间戳上。我如何在'message_read'上添加子分区?

三江源

2 个答案:

答案 0 :(得分:0)

您需要将message_read添加到主键。 然后(您可以使用散列或密钥进行子分区):

CREATE TABLE `messages` (
  `pid` INT(50) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'Primary Key',
  `userid1` INT(10) UNSIGNED NOT NULL COMMENT 'Message Sender',
  `userid2` INT(10) UNSIGNED NOT NULL COMMENT 'Message Receiver',
  `message` TEXT NOT NULL COMMENT 'Actual Message',
  `message_read` TINYINT(1) NOT NULL DEFAULT '0' COMMENT '0=Not Read & 1=Read',
  `datetime` DATETIME NOT NULL COMMENT 'Date Time',
  PRIMARY KEY (`pid`,`datetime`, message_read),
  KEY `userid1` (`userid1`),
  KEY `userid2` (`userid2`),
  KEY `message_read` (`message_read`)
) ENGINE=INNODB DEFAULT CHARSET=utf8
PARTITION BY RANGE (TO_DAYS(`datetime`))
SUBPARTITION BY HASH (message_read)
(PARTITION p0 VALUES LESS THAN (735234)(SUBPARTITION s0, SUBPARTITION s1),
 PARTITION p1 VALUES LESS THAN (735265)(SUBPARTITION s2, SUBPARTITION s3),
 PARTITION p2 VALUES LESS THAN (735293)(SUBPARTITION s4, SUBPARTITION s5),
 PARTITION p3 VALUES LESS THAN (735324)(SUBPARTITION s6, SUBPARTITION s7),
 PARTITION p4 VALUES LESS THAN (735354)(SUBPARTITION s8, SUBPARTITION s9),
 PARTITION p5 VALUES LESS THAN (735385)(SUBPARTITION s10, SUBPARTITION s11),
 PARTITION p6 VALUES LESS THAN (735415)(SUBPARTITION s12, SUBPARTITION s13)) ;

希望这就是你要找的东西。

谢谢, ÿ

答案 1 :(得分:0)

在这种情况下 - 由于每个分区分为两部分, mysql如何将记录分配给子分区? 毕竟mysql不知道将在每个分区/子分区中输入多少条记录