创建分区的字段必须是主键
不是分区表字段查询是全表扫描,如果是分区表字段查询速度比不分区快百倍左右
select count(*) from tablesName 这种全表扫描查询在分区表中非常慢
创建hash分区表
CREATE TABLE `m_user_info` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL COMMENT '名称',
`age` int(11) DEFAULT '0' COMMENT '上级',
`model` int(11) DEFAULT '1',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=820105 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT='省市区数据' PARTITION by HASH(id) PARTITIONS 10;
(分区字段必须在 PRIMARY KEY中)
explain select * from m_user_info where name=212



创建range 范围类型的分区
CREATE TABLE `m_user_info` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL COMMENT '名称',
`age` int(11) DEFAULT '0' COMMENT '上级',
`model` int(11) DEFAULT '1',
'create_time' datetime
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=820105 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT='省市区数据'
partition by RANGE(YEAR(login_time))(
partition p0 values less than(2014),
partition p1 values less than(2015),
partition p2 values less than(2017)
);
创建关键字 KEY 分区
新增分区
我们可以使用alter table tablename add partition 方式再最后面添加分区
#1.新添加分区
alter table user_login_tb add partition(partition p3 values less than(2019));
分区迁移
alter table user_login_tb exchange partition p2 with table arch_login_log
删除分区
alter table user_login_tb drop partition p1;

被折叠的 条评论
为什么被折叠?



