当前位置:网站首页>非分区表转换成分区表以及注意事项
非分区表转换成分区表以及注意事项
2022-07-08 01:00:00 【仙凌阁】
有时候刚开始设计的时候没考虑到数据归档。数据归档不是简单的导出delete。因为一般我们的数据库都是delete不释放空间。而我们一般数据库遇到空间不足,就不得不面对这个问题。当然也可以做碎片整理的空间回收。我不太喜欢这样,尤其大的表的时候。MySQL一旦超过100G的,在线环境我都不敢去做。Oracle的在线回收我做过20T的表,碎片800G,回收了一次做了55个小时(不停机)
我个人觉得最好的方式是采用分区,至于是迁移分区还是归档分区都好说。一般都是截断分区这样操作,很快。但是如果一个表之前没有做分区怎么办?
我们举例
create table b (id int ,a varchar2(10),time date);普通堆表
模拟5条数据
insert into b values (1,'a',sysdate-120);
insert into b values (2,'b',sysdate-90);
insert into b values (3,'c',sysdate-60);
insert into b values (4,'d',sysdate-30);
insert into b values (5,'e',sysdate);
一般来说我们都要使用到时间。所以时间建立索引。
create index b1 on b (time);
一般表都有主键,在Oracle中应该使用业务单据号来作为主键,在MySQL中用ID自增无业务含义。在PG中按照Oracle的来就行。
Alter table b add constraint pkb primary key(id);
为B表增加主键,这里注意一定要是自己命名的而不是数据库自己命令的。因为正常主从都没有问题,但是有OGG的场景这样的话就是一个坑。后面我来讲为什么。
ALTER TABLE b MODIFY
PARTITION BY RANGE (time)
(
PARTITION b1 VALUES LESS THAN (TO_DATE ('2022-03-01', 'YYYY-MM-DD')),
PARTITION b2 VALUES LESS THAN (TO_DATE ('2022-04-01', 'YYYY-MM-DD')),
PARTITION b3 VALUES LESS THAN (TO_DATE ('2022-05-01', 'YYYY-MM-DD')),
PARTITION b4 VALUES LESS THAN (TO_DATE ('2022-06-01', 'YYYY-MM-DD')),
PARTITION b5 VALUES LESS THAN (TO_DATE ('2022-07-01', 'YYYY-MM-DD'))
) online
UPDATE INDEXES
(
pkb GLOBAL,
b1 LOCAL
);
这个就是在线把非分区表转换成分别表的命令。注意红色部分我这里指定了主键名,如果是自动的那么就是自动的名字。上面讲的坑就在这里,因为在OGG场景下,目标端也就是下游数据库的自动的名字和源端上游不一样。执行这个命令时候就会引发OGG的中断。
那么这个是数据库的问题还是OGG的问题?我觉得都不是,是熟悉产品的问题。执行完毕以后看到表已经变成分区的了。
这个时候再截断过期数据只要drop partition就可以了,当然注意一个UPDATE GLOBAL INDEXES;否则会出现数据无法写入的问题。
源码附件已经打包好上传到百度云了,大家自行下载即可~
链接: https://pan.baidu.com/s/14G-bpVthImHD4eosZUNSFA?pwd=yu27
提取码: yu27
百度云链接不稳定,随时可能会失效,大家抓紧保存哈。
如果百度云链接失效了的话,请留言告诉我,我看到后会及时更新~
开源地址
码云地址:
http://github.crmeb.net/u/defu
Github 地址:
http://github.crmeb.net/u/defu
链接:http://blog.itpub.net/637517/viewspace-2904267
边栏推荐
- XMeter Newsletter 2022-06|企业版 v3.2.3 发布,错误日志与测试报告图表优化
- The bank needs to build the middle office capability of the intelligent customer service module to drive the upgrade of the whole scene intelligent customer service
- Force buckle 5_ 876. Intermediate node of linked list
- Redisson distributed lock unlocking exception
- Force buckle 4_ 412. Fizz Buzz
- JVM memory and garbage collection-3-direct memory
- Keras' deep learning practice -- gender classification based on inception V3
- Thread deadlock -- conditions for deadlock generation
- Le chemin du poisson et des crevettes
- 很多小夥伴不太了解ORM框架的底層原理,這不,冰河帶你10分鐘手擼一個極簡版ORM框架(趕快收藏吧)
猜你喜欢
#797div3 A---C
leetcode 873. Length of Longest Fibonacci Subsequence | 873. 最长的斐波那契子序列的长度
Strive to ensure that domestic events should be held as much as possible, and the State General Administration of sports has made it clear that offline sports events should be resumed safely and order
Monthly observation of internet medical field in May 2022
Talk about the realization of authority control and transaction record function of SAP system
Ml self realization /knn/ classification / weightlessness
CorelDRAW2022下载安装电脑系统要求技术规格
Ml backward propagation
JVM memory and garbage collection -4-string
Beaucoup d'enfants ne savent pas grand - chose sur le principe sous - jacent du cadre orm, non, ice River vous emmène 10 minutes à la main "un cadre orm minimaliste" (collectionnez - le maintenant)
随机推荐
metasploit
How to use diffusion models for interpolation—— Principle analysis and code practice
Popular science | what is soul binding token SBT? What is the value?
Random walk reasoning and learning in large-scale knowledge base
Semantic segmentation | learning record (5) FCN network structure officially implemented by pytoch
Disk rust -- add a log to the program
Force buckle 5_ 876. Intermediate node of linked list
力扣5_876. 链表的中间结点
leetcode 869. Reordered Power of 2 | 869. Reorder to a power of 2 (state compression)
Relationship between bizdevops and Devops
OpenGL/WebGL着色器开发入门指南
Principle of least square method and matlab code implementation
JVM memory and garbage collection -4-string
发现值守设备被攻击后分析思路
文盘Rust -- 给程序加个日志
Anan's judgment
咋吃都不胖的朋友,Nature告诉你原因:是基因突变了
[knowledge map] interpretable recommendation based on knowledge map through deep reinforcement learning
Discrimination gradient descent
很多小伙伴不太了解ORM框架的底层原理,这不,冰河带你10分钟手撸一个极简版ORM框架(赶快收藏吧)