MySQL 分区表 partition线上修改分区字段,后续进一步学习partition (1)
公司线上在用partition,有一个表的分区字段错了,需要重建,结果发现没有办法像修改主键字段或者修改索引字段那样直接一条sql搞定。而是需要建临时表,有down time,所以去仔细看了文档,研究下partition的细节问题。
自己公司线上采取的时候,凌晨1点业务低峰期,执行:
建立临时表
CREATE TABLE tbname_TMP (
SHARD_ID INT NOT NULL,
...
xxx_DATE DATETIME NOT NULL,
PRIMARY KEY (xxx_DATE,shard_id)
) ENGINE=INNODB DEFAULT CHARSET=utf8 COLLATE=utf8_bin
PARTITION BY LIST(MONTH(xxx_DATE)) (
PARTITION m1 VALUES IN (1),
PARTITION m2 VALUES IN (2),
PARTITION m3 VALUES IN (3),
PARTITION m4 VALUES IN (4),
PARTITION m5 VALUES IN (5),
PARTITION m6 VALUES IN (6),
PARTITION m7 VALUES IN (7),
PARTITION m8 VALUES IN (8),
PARTITION m9 VALUES IN (9),
PARTITION m10 VALUES IN (10),
PARTITION m11 VALUES IN (11),
PARTITION m12 VALUES IN (12)
);
切换表名字,修改表结构
RENAME TABLE xxx TO xxx_DELETED, xxx_TMP TO xxx;
导入原始数据
insert into xxx select * from xxx_DELETEDxxx_DELETED;
OK,一切搞定,整个过程50分钟,MMM failover切换中后outline操作表结构变更以及数据导入,实际downtime不包括修改表结构分区字段的时间,只包括failover切换时间 为30秒
MySQL Partition,看的官方英文资料,翻译水平有限,有些不翻译成中文了,直接贴英文了。
1 list partition table
mysql> CREATE TABLE `eh` (
-> `id` int(11) NOT NULL,
-> `ENTITLEMENT_HIST_ID` bigint(20) NOT NULL,
-> `ENTITLEMENT_ID` bigint(20) NOT NULL,
-> `USER_ID` bigint(20) NOT NULL,
-> `DATE_CREATED` datetime NOT NULL,
-> `STATUS` smallint(6) NOT NULL,
-> `CREATED_BY` varchar(32) COLLATE utf8_bin DEFAULT NULL,
-> `MODIFIED_BY` varchar(32) COLLATE utf8_bin DEFAULT NULL,
-> `DATE_MODIFIED` datetime NOT NULL,
-> PRIMARY KEY (`DATE_MODIFIED`,`id`)
-> ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin
-> /*!50100 PARTITION BY LIST (MONTH(DATE_MODIFIED))
-> (PARTITION m1 VALUES IN (1) ENGINE = InnoDB,
-> PARTITION m2 VALUES IN (2) ENGINE = InnoDB,
-> PARTITION m3 VALUES IN (3) ENGINE = InnoDB,
-> PARTITION m4 VALUES IN (4) ENGINE = InnoDB,
-> PARTITION m5 VALUES IN (5) ENGINE = InnoDB,
-> PARTITION m6 VALUES IN (6) ENGINE = InnoDB,
-> PARTITION m7 VALUES IN (7) ENGINE = InnoDB,
-> PARTITION m8 VALUES IN (8) ENGINE = InnoDB,
-> PARTITION m9 VALUES IN (9) ENGINE = InnoDB,
-> PARTITION m10 VALUES IN (10) ENGINE = InnoDB,
-> PARTITION m11 VALUES IN (11) ENGINE = InnoDB,
-> PARTITION m12 VALUES IN (12) ENGINE = InnoDB) */;
Query OK, 0 rows affected (0.10 sec)
2 rang partition table
mysql> CREATE TABLE rcx (
-> a INT,
-> b INT,
-> c CHAR(3),
-> d INT
-> )
-> PARTITION BY RANGE COLUMNS(a,d,c) (
-> PARTITION p0 VALUES LESS THAN (5,10,'ggg'),
-> PARTITION p1 VALUES LESS THAN (10,20,'mmmm'),
-> PARTITION p2 VALUES LESS THAN (15,30,'sss'),
-> PARTITION p3 VALUES LESS THAN (MAXVALUE,MAXVALUE,MAXVALUE)
-> );
Query OK, 0 rows affected (0.15 sec)
3 create range use less character
CREATE TABLE employees_by_lname (
id INT NOT NULL,
fname VARCHAR(30),
lname VARCHAR(30),
hired DATE NOT NULL DEFAULT '1970-01-01',
separated DATE NOT NULL DEFAULT '9999-12-31',
job_code INT NOT NULL,
store_id INT NOT NULL
)
PARTITION BY RANGE COLUMNS (lname) (
PARTITION p0 VALUES LESS THAN ('g'),
PARTITION p1 VALUES LESS THAN ('m'),
PARTITION p2 VALUES LESS THAN ('t'),
PARTITION p3 VALUES LESS THAN (MAXVALUE)
);
alter table structure,add a new partition block
ALTER TABLE employees_by_lname PARTITION BY RANGE COLUMNS (lname) (
PARTITION p0 VALUES LESS THAN ('g'),
PARTITION p1 VALUES LESS THAN ('m'),
PARTITION p2 VALUES LESS THAN ('t'),
PARTITION p3 VALUES LESS THAN ('u'),
PARTITION p4 VALUES LESS THAN (MAXVALUE)
);
4 List columns partitioning
character column
CREATE TABLE customers_1 (
first_name VARCHAR(25),
last_name VARCHAR(25),
street_1 VARCHAR(30),
street_2 VARCHAR(30),
city VARCHAR(15),
renewal DATE
)
PARTITION BY LIST COLUMNS(city) (
PARTITION pRegion_1 VALUES IN('Oskarshamn', 'H?gsby', 'M?nster?s'),
PARTITION pRegion_2 VALUES IN('Vimmerby', 'Hultsfred', 'V?stervik'),
PARTITION pRegion_3 VALUES IN('N?ssj?', 'Eksj?', 'Vetlanda'),
PARTITION pRegion_4 VALUES IN('Uppvidinge', 'Alvesta', 'V?xjo')
);
date column
CREATE TABLE customers_2 (
first_name VARCHAR(25),
last_name VARCHAR(25),
street_1 VARCHAR(30),
street_2 VARCHAR(30),
city VARCHAR(15),
renewal DATE
)
PARTITION BY LIST COLUMNS(renewal) (
PARTITION pWeek_1 VALUES IN('2010-02-01', '2010-02-02', '2010-02-03',
'2010-02-04', '2010-02-05', '2010-02-06', '2010-02-07'),
PARTITION pWeek_2 VALUES IN('2010-02-08', '2010-02-09', '2010-02-10',
'2010-02-11', '2010-02-12', '2010-02-13', '2010-02-14'),
PARTITION pWeek_3 VALUES IN('2010-02-15', '2010-02-16', '2010-02-17',
'2010-02-18', '2010-02-19', '2010-02-20', '2010-02-21'),
PARTITION pWeek_4 VALUES IN('2010-02-22', '2010-02-23', '2010-02-24',
'2010-02-25', '2010-02-26', '2010-02-27', '2010-02-28')
);
5 HASH Partitioning
int column,it can use digital function
CREATE TABLE employeesint (
id INT NOT NULL,
fname VARCHAR(30),
lname VARCHAR(30),
hired DATE NOT NULL DEFAULT '1970-01-01',
separated DATE NOT NULL DEFAULT '9999-12-31',
job_code INT,
store_id INT
)
PARTITION BY HASH(MOD(store_id,4))
PARTITIONS 4;
If you do not include a PARTITIONS clause, the number of partitions defaults to 1. as below:
CREATE TABLE employeestest (
id INT NOT NULL,
fname VARCHAR(30),
lname VARCHAR(30),
hired DATE NOT NULL DEFAULT '1970-01-01',
separated DATE NOT NULL DEFAULT '9999-12-31',
job_code INT,
store_id INT
)
PARTITION BY HASH(store_id);
date colum
CREATE TABLE employees2 (
id INT NOT NULL,
fname VARCHAR(30),
lname VARCHAR(30),
hired DATE NOT NULL DEFAULT '1970-01-01',
separated DATE NOT NULL DEFAULT '9999-12-31',
job_code INT,
store_id INT
)
PARTITION BY HASH( YEAR(hired) )
PARTITIONS 4;
truncate all data rows: alter table rcx truncate PARTITION;
6 LINEAR HASH Partitioning
CREATE TABLE employees_linear (
id INT NOT NULL,
fname VARCHAR(30),
lname VARCHAR(30),
hired DATE NOT NULL DEFAULT '1970-01-01',
separated DATE NOT NULL DEFAULT '9999-12-31',
job_code INT,
store_id INT
)
PARTITION BY LINEAR HASH( YEAR(hired) )
PARTITIONS 4;
Given an expression expr, the partition in which the record is stored when linear hashing is used is partition number N from among num partitions, where N is derived according to the following algorithm:
(1) Find the next power of 2 greater than num. We call this value V; it can be calculated as:
V = POWER(2, CEILING(LOG(2, num)))
(Suppose that num is 13. Then LOG(2,13) is 3.7004397181411. CEILING(3.7004397181411) is 4, and V = POWER(2,4), which is 16.)
(2) Set N = F(column_list) & (V - 1).
(3) While N >= num:
Set V = CEIL(V / 2)
Set N = N & (V - 1)
[注释] & 在SQL里面的计算原理为:比如
把十进制转化进制成二进制,就得到了 http://zh.wikipedia.org/wiki/%E4%BA%8C%E8%BF%9B%E5%88%B6
首先按右对齐,例如变成0011和1000,按照每一位的数字来判断,如果两个都是1,则结果的相应位置就是1,否则就是0
如果是1011和1000,结果就是1000
如果是0110和1010,则结果就是0010
但是3是0011,8 是1000,所以3&8结果就是0
CEILING(X) CEIL(X): 返回不小于X 的最小整数值。
LOG(X) LOG(B,X) :若用一个参数调用,这个函数就会返回X 的自然对数。
POWER(X,Y) : 返回X 的Y乘方的结果值。
数据分布在哪个片区的计算方法:
Suppose that the table t1, using linear hash partitioning and having 6 partitions, is created using this statement:
CREATE TABLE t1 (col1 INT, col2 CHAR(5), col3 DATE)
PARTITION BY LINEAR HASH( YEAR(col3) )
PARTITIONS 6;
Now assume that you want to insert two records into t1 having the col3 column values '2003-04-14' and '1998-10-19'. The partition number for the first of these is determined as follows:
V = POWER(2, CEILING( LOG(2,6) )) = 8
N = YEAR('2003-04-14') & (8 - 1)
= 2003 & 7
= 3
(3 >= 6 is FALSE: record stored in partition #3)
The number of the partition where the second record is stored is calculated as shown here:
V = 8
N = YEAR('1998-10-19') & (8-1)
= 1998 & 7
= 6
(6 >= 6 is TRUE: additional step required)
N = 6 & CEILING(8 / 2)
= 6 & 3
= 2
(2 >= 6 is FALSE: record stored in partition #2)
The advantage in partitioning by linear hash is that the adding, dropping, merging, and splitting of partitions is made much faster, which can be beneficial when dealing with tables containing extremely large amounts (terabytes) of data. The disadvantage is that data is less likely to be evenly distributed between partitions as compared with the distribution obtained using regular hash partitioning.
疑问之一:MySQL 如何用一条SQL,不需要用临时表来删除分区字段?将分区表变成普通的表?
MySQL 分区表 partition线上修改分区字段,后续进一步学习partition (1)的更多相关文章
- MySQL数据库如何线上修改表结构
一.MDL元数据锁 在修改表结构之前,先来看下可能存在的问题. 1.什么是MDL锁 MySQL有一个把锁,叫做MDL元数据锁,当对表修改的时候,会自动给表加上这把锁,也就是不需要自己显式使用. 当对表 ...
- MySQL慢日志线上问题分析及功能优化
本文来源于数据库内核专栏. MySQL慢日志(slow log)是MySQL DBA及其他开发.运维人员需经常关注的一类信息.使用慢日志可找出执行时间较长或未走索引等SQL语句,为进行系统调优提供依据 ...
- hdfs线上修改 nameserivce
hdfs线上修改 nameserivce(ns1 修改为 ns2) 1.去core-site.xml.hdfs-site.xml 把ns1 -> ns2 同步所有节点 2.去journal 数据 ...
- 【MySQL】实现线上千万数据表添加字段操作以及缓存刷新
需求背景: 由于业务需求,需要在线上用户表添加渠道字段,用于区分不同渠道注册的用户,目前该表有20+个字段,8个索引 线上用户数据大概1500W左右,需要不停机增加数据库字段,同时需要刷新Redis缓 ...
- Mysql唯一索引线上故障记录
问题现象: Mysql插入一条数据时,未指定自增键的值却报错:自增键重复,无法插入! 执行SQL INSERT INTO `test`.`test_sort`(`id`, `name`, `age`, ...
- MySQL死锁系列-线上死锁问题排查思路
前言 MySQL 死锁异常是我们经常会遇到的线上异常类别,一旦线上业务日间复杂,各种业务操作之间往往会产生锁冲突,有些会导致死锁异常.这种死锁异常一般要在特定时间特定数据和特定业务操作才会复现,并且分 ...
- MySQL模拟:线上误update的恢复
作为DBA,细心.沉稳是首要的基本素质.不过总有那么一会心烦意乱或者开发同学出现误操作之类的...这里模拟一个误update操作,然后恢复. 如果开发同学有误操作之后最好先别乱动生产环境,需要记录几个 ...
- mysql + excel 校正线上数据
积分问题处理 1. 所有应补汇总 select driver_id, SUM(integral) from detail_score group by driver_id; 2. 原汇总积分 sele ...
- grep线上环境精典案例后续
请执行命令取出 linux 中 eth0 的 IP 地址(请用 cut,有能力者也可分别用 awk,sed 命令答). 自己的方法: [root@nginx_back ~]# ifconfig eth ...
随机推荐
- 类似a:hover的伪类的注解
a:link { font-size: 14pt; text-decoration: underline; color: blue; } /*设置a对象在未被访问前的样式表属性 .*/ a:hover ...
- php实现网页HTML标签补全方法
如果你的网页内容的html标签显示不全,有些表格标签不完整而导致页面混乱,或者把你的内容之外的局部html页面给包含进去了,我们可以写个函数方法来补全html标签以及过滤掉无用的html标签. php ...
- $ npm install opencv ? 你试试?! 在windows环境下,使用node.js调用opencv攻略
博主之前写过一篇文章<html5与EmguCV前后端实现——人脸识别篇>,叙述的是opencv和C#的故事.最近在公司服务器上更新了一套nodejs环境,早就听闻npm上有opencv模块 ...
- 关于C51的中断函数要注意的几个问题
转载自:http://blog.21ic.com/user1/531/archives/2006/16909.html 最近在虾潭逛,发现一些小虾米对C51中断函数有些不了解,今天周末,抽空发个技术帖 ...
- Inno Setup for Windows service
Inno Setup for Windows service? up vote86down votefavorite 77 I have a .Net Windows service. I want ...
- 5s
整理,整顿,清楚,清洁,身美
- Bonobos | IT桔子
Trumaker | IT桔子 Trumaker trumaker.com
- PHP中数组转换为XML格式
最近公司要做一个API接口,输出格式要有JSON与XML格式, 在PHP中,输入JSON直接json_encode就可以,但输出XML没有提供函数,于是决定自己写一个. <?php /** * ...
- lua的几个时间相关处理函数
随手写的,项目中没用,不一定对,只作参考. --游戏时间相关函数 local gt = {} local math = math local ONE_HOUR = ONE_MINUTE * ONE_M ...
- WinXP 无线提示“区域中找不到无线网络”的一种可能原因!
貌似WinXP还是无限经典,我也一直还在用,不知道哪天才会放弃.这次遇见的问题,或许也有XP爱好者也遇得见,记下点文字备忘.----------------------- 单调的切割线 ------- ...