mysql--分区(partition )

一张表分成若干个小表。

原理:

由多个表底层组成,每个底层表都是一个句柄对象表示(指针),

建立存储引擎实际上就是在每个底层表都建立相同的存储索引,通过分区关键字直接访问底层表,

操作select时,分区层打开锁住所有底层表,sql优化器开始过滤部分分区,通过存储引擎再打开查问分区表的数据。

操作update时,分区层打开锁住所有底层表,确定数据在哪个分区,取出数据更新,先不对分区表的数据做存储,先把之前的数据删除,再重新把新改的数据写入分区表。

操作delete时,分区层打开锁住所有底层表,确定哪个分区,去底层表里删除数据。

操作insert时,分区层打开锁住所有底层表,确定哪个分区,去底层表里写入数据。

优势:

可以扩充表的数据量。

对于访问量不大的分区,可以通过分区很容易删除数据,如果有新增加的数据,可以很容易去扩展分区。

减少访问数据时,减少一些不必要数据的索引数据检索量,

根据分区来查找数据,提高数据的查询效率,

像分组,统计这种函数,分区表可以实现多个磁盘并行的去分散查询数据,来提高数据查询的吞吐量。

减少查询是的响应时间

优势:

  • 一个表最多只能有1024个分区(mysql5.6之后支持8192个分区)。
  • 在mysql5.1中分区表达式必须是整数,或者是返回整数的表达式,在5.5之后,某些场景可以直接使用字符串列和日期类型列来进行分区(使用varchar字符串类型列时,一般还是字符串的日期作为分区)。
  • 如果分区字段中有主键或者唯一索引列,那么所有主键列和唯一索引列都必须包含进来,如果表中有主键或唯一索引,那么分区键必须是主键或唯一索引。
  • 分区表中无法使用外键约束。
  • mysql数据库支持的分区类型为水平分区,并不支持垂直分区,因此,mysql数据库的分区中索引是局部分区索引,一个分区中既存放了数据又存放了索引,而全局分区是指的数据库放在各个分区中,但是所有的数据的索引放在另外一个对象中
  • 目前mysql不支持空间类型和临时表类型进行分区。不支持全文索引。

类型:

1、RANGE分区(常用)

范围分区

create table employees ( 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 (store_id) ( partition p0 values less than (6), partition p1 values less than (11), partition p2 values less than (16), partition p3 values less than (21), partition p3 values less than maxvalue );

2、LIST分区

散列值,不如,1、3、5放到a分区,2、4、6、7放到b分区。

create table employees ( 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 list(store_id) partition pNorth values in (3,5,6,9,17), partition pEast values in (1,2,10,11,19,20), partition pWest values in (4,12,13,14,18), partition pCentral values in (7,8,15,16) );

3、HASH分区:

定义的表达式来选择分区,根据选择分区用的列值来计算表达式。

种类:;linear hash

create table employees ( 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 hash(store_id) partitions 4;

4、KEY分区:

类似HASH,支持多列,mysql提高自身的哈希算法,必须包含整数值。

create table tk ( col1 int not null, col2 char(5), col3 date ) partition by linear key (col1) partitions 3;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值