MySQL数据库表的合并及分区

1,合并表:把多个结果相同的的表合并为一个容器。

容器的类型:Myisam,存储引擎:merge

存在的问题:有重复的行

<pre class="hljs sql" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 0.75em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;">create table packtable(
  id ....
)engine=merge unique=(table1,table2);

2,表的分区:

更多技术知识请关注我的V-X-公-众hao:编程经验共享

2.1,水平分区:根据某个字段进行分区(RANGE分区)

<pre class="prettyprint hljs sql" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;">create table test1(
id int(10) primary key auto_increment,
 score int(3)
)engine=innodb default charset=utf8 partition by range(score)(
//根据score字段分区,score小于60的在p1分区
  partition p1 values less than(60),
//根据score字段分区,score小于70的在p2 分区
 partition p2 values less than(70),
 //根据score字段分区,score大于70的在p3 分区
  partition p3 values lessthan maxvalue
 );

2.2 list分区:第一选择基于某 列的值是否属于某个 集合

<pre class="prettyprint hljs sql" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;">create table test1(
     id int(10) primary key auto_increment,
     branch_id int(3)
)engine=innodb default charset=utf8 partition by list(branch_id  )(
    //根据branch_id 字段分区,branch_id  在1,2,3之中的为p1分区
     partition p1 values less in(1,2,3),
    //根据branch_id 字段分区,branch_id  在7,8,9 之中的为p2分区
     partition p2 values less in(7,8,9)
);

2.3 hash分区:支持数值类型

<pre class="prettyprint hljs sql" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;">--
-- 根据birthda字段获取月份,再根据月份进行分区储存,一共分12个区;
--
create table test1(
 id int(10) primary key auto_increment,
 birthday date
)engine=innodb default charset=utf8 partition by hash(month(birthday))  partitions 12;

2.4 线性分区(linear hash):大数据是增加,合并,拆分速度更快

<pre class="prettyprint hljs sql" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;">--
-- 根据branch_id字段进行分区储存,一共分5个区;
--
create table test1(
 id int(10) primary key auto_increment,
 branch_id int(3)
)engine=innodb default charset=utf8 partition by linear hash (branch_id  ) partitions 5;

2.5 key分区:可以计算一列或者多列进行分区

<pre class="prettyprint hljs sql" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;">--
-- 根据branch_id字段进行分区储存,一共分5个区;
--
create table test1(
 id int(10) primary key auto_increment,
 branch_id int(3)
)engine=innodb default charset=utf8 partition by key (branch_id  ) partitions 5;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值