Mysql horizontal partitioning learn and practice notes

SHOW PLUGINS;sql can check partitionthe Statuswhether ACTIVEthe

Using mydatetimehorizontal partitioning Case:

CREATE TABLE test_users (
    `id` INT (10) NOT NULL AUTO_INCREMENT,
    `mydatetime` datetime NOT NULL,
    `email` VARCHAR (255) NOT NULL,
    UNIQUE INDEX (`email`),
    PRIMARY KEY (`id`)
);

-- 如果表已创建时的操作

-- 修改主键的类型
ALTER TABLE test_users CHANGE COLUMN `id` `id` INT (10) UNSIGNED NOT NULL;
-- 删除主键
ALTER TABLE test_users DROP PRIMARY KEY;
-- 将 主键id和分区字段mydatetime 同作为主键
ALTER TABLE test_users ADD PRIMARY KEY (id, mydatetime);
-- 给主机 id 加上 自动增长
ALTER TABLE test_users CHANGE COLUMN `id` `id` INT (10) UNSIGNED NOT NULL AUTO_INCREMENT;
-- 删除索引 email
ALTER TABLE test_users DROP INDEX email;
-- 将 email和mydatetime字段 同作为名为 email的唯一索引
ALTER TABLE test_users ADD UNIQUE KEY `email` (email, mydatetime);
-- 根据 mydatetime的日期值,将小于 2018-12-31放在 p1,小于2019-06-30放在p2,小于最大值的放在p8分区,名字是自己定义的
alter table test_users PARTITION by range(TO_DAYS(mydatetime))(
    PARTITION p1 VALUES LESS THAN (TO_DAYS('2018-12-31')),
    PARTITION p2 VALUES LESS THAN (TO_DAYS('2019-06-30')),
    PARTITION p8 VALUES LESS THAN MAXVALUE 
);
-- 分析查询语句,测试分区是否有用
EXPLAIN SELECT * FROM test_users WHERE mydatetime <= '2019-07-01';

[Err]1503 - A UNIQUE INDEX must include all columns in the table's partitioning function
[Err] 1486 - Constant, random or timezone-dependent expressions in (sub)partitioning function are not allowed

Try 1:

[Err] 1503 - A PRIMARY KEY must include all columns in the table's partitioning function

Primary key field need to include partition

Attempted to delete the original master key, and then re-create a composite primary key

[Err]1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key

Removes primary key

Perform the following in the sql

ALTER TABLE test_table CHANGE COLUMN `id` `id` int(10) unsigned NOT NULL ;
ALTER TABLE test_table DROP PRIMARY KEY; #删除主键
ALTER TABLE test_table add PRIMARY KEY(id); #添加主键
References
  1. Failure Case --mysql5.5 a pit partition table
  2. mysql automatic partitioning practice
  3. MySQL partition table to explore
  4. MySQL 表分区 A PRIMARY KEY must include all columns in the table's partitioning function
  5. A primary must include all columns in the table's partitioning location error? Needs and primary key, a partitioned field as processing unique keyi
  6. [MySQL] AUTO_INCREMENT column change is annoying ... mysql [err] 1075
  7. MySQL Add / Remove primary key, foreign key, unique key, indexes, auto-increment
  8. 错误代码:1503 A UNIQUE INDEX must include all columns in the table's partitioning function

Guess you like

Origin www.cnblogs.com/fsong/p/11258947.html