前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >MySQL创建分区表相关

MySQL创建分区表相关

作者头像
ianzhi
发布2023-12-17 11:04:29
发布2023-12-17 11:04:29
13800
代码可运行
举报
文章被收录于专栏:LNMP开发那些事LNMP开发那些事
运行总次数:0
代码可运行

MySQL创建分区表相关

背景:一个记录表,类似日志的信息,查询大量集中在某个用户个人的数据,分区需要尽量保证一个人的数据在一个分区里。因此采用通过user_id进行hash分区的方式。

代码语言:javascript
代码运行次数:0
复制
-- 将分区字段添加为主键
alter table logs modify column id int not null;
alter table logs drop primary key;
alter table logs add primary key(id, user_id);
alter table logs modify column id int not null auto_increment;

-- 创建带分区的表
CREATE TABLE `logs_withs_partitions` (
  `id` int NOT NULL AUTO_INCREMENT,
  `user_id` int NOT NULL,
  ...
  PRIMARY KEY (`id`,`user_id`)
) PARTITION BY HASH(user_id) PARTITIONS 5;
-- 将数据复制到带分区的表
insert into logs_withs_partitions
select * from logs;

-- 重命名表
rename table logs to logs_without_partitions;
rename table logs_withs_partitions to logs;

-- 删除不带分区的表
drop table logs_without_partitions;
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • MySQL创建分区表相关
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档