MySQL 数据库中的数据是以文件的形势存在磁盘上的,
默认放在
/var/lib/mysql/
目录下面,我们可以通过
show variables like '%datadir%';
命令来查看:
show variables like '%datadir%';
我们进入到这个目录下,就可以看到我们定义的所有数据库了,
一个数据库就是一个目录,一个库中,有其对应的表的信息,如下:
cd /var/lib/mysql
ls
优点:
缺点:
在考虑使用分区时,需要综合考虑业务需求、查询模式、数据规模和硬件资源等因素,
并权衡分区带来的优势和缺点。对于特定的应用和数据场景,
分区可能是一个有效的解决方案,但并不适用于所有情况。
同时分区表也存在一些限制,如下:
限制:
分区表在以下情况下可以发挥其优势,适用于以下几种使用场景:
分区表并非适用于所有情况。在选择使用分区表时,需要综合考虑数据量、查询模式、存储资源和硬件能力等因素,并评估分区对性能和管理的影响。
- RANGE分区
RANGE分区是MySQL中的一种分区策略,根据某一列的范围值将数据分布到不同的分区。
每个分区包含特定的范围。下面是RANGE分区的定义方式、特点以及代码示例。
定义方式:
PARTITION BY RANGE
指定使用RANGE分区策略。VALUES LESS THAN
子句定义每个分区的范围。RANGE分区的特点:
以下是一个使用RANGE分区的代码示例:
create table sale(
sale_id int,
sale_date date,
amount decimal(10,2)
) partition by range(year(sale_date))(
partition p0 values less than (2020),
partition p1 values less than (2021),
partition p2 values less than (2022),
partition p3 values less than (2023),
partition p4 values less than (2024),
partition p5 values less than (2025)
);
insert into sale values (1,'2020-11-11',9999.00);
insert into sale values (2,'2020-12-12',6666.00);
insert into sale values (3,'2021-11-11',12000.00);
insert into sale values (4,'2021-12-12',13000.00);
insert into sale values (5,'2022-11-11',13000.00);
insert into sale values (6,'2022-12-12',12500.00);
insert into sale values (7,'2023-11-11',12500.00);
insert into sale values (8,'2023-12-12',13000.00);
select * from sale partition(p0);
select * from sale partition(p1);
LIST分区是根据某一列的离散值将数据分布到不同的分区。每个分区包含特定的列值列表。下面是LIST分区的定义方式、特点以及代码示例。
定义方式:
PARTITION BY LIST
指定使用LIST分区策略。VALUES IN
子句定义每个分区包含的列值列表。LIST分区的特点:
以下是一个使用LIST分区的代码示例:
create table people(
id_card varchar(18),
name varchar(50),
province int
) partition by list(province)(
partition huabei values in (11,12,13,14,15),
partition dongbei values in (21,22,23),
partition huazhong values in (31,32,33,34,35,36,37),
partition huadong values in (41,42,43,44,45,46),
partition xinan values in (50,51,52,53,55),
partition xibei values in (61,62,63,64,65),
partition gang_ao_tai values in (71,81,91)
);
insert into people values ('410182198903224674','邱赣',41);
insert into people values ('411429199211019071','何天',41);
insert into people values ('310182198903224674','方加牡',31);
insert into people values ('210182198903224674','贺巧',21);
insert into people values ('110182198903224674','闾丘饱乾',11);
insert into people values ('510182198903224674','丁经',51);
insert into people values ('610182198903224674','韦散',61);
insert into people values ('710182198903224674','东方让',71);
insert into people values ('810182198903224674','赖队瞻',81);
insert into people values ('910182198903224674','郭叹',91);
insert into people values ('120182198903224674','慕容芋岛',12);
insert into people values ('220182198903224674','孙劣',22);
insert into people values ('320182198903224674','王桃',32);
insert into people values ('420182198903224674','郝郑惭',42);
insert into people values ('520182198903224674','余烂',52);
insert into people values ('620182198903224674','宇文酚',62);
select * from people partition(huabei);
select * from people partition(dongbei);
select * from people partition(huazhong);
select * from people partition(huadong);
select * from people partition(xibei);
select * from people partition(gang_ao_tai);
HASH分区是使用哈希算法将数据均匀地分布到多个分区中。下面是HASH分区的定义方式、特点以及代码示例。
定义方式:
PARTITION BY HASH
指定使用HASH分区策略。PARTITIONS
关键字指定分区的数量。HASH分区的特点:
以下是一个使用HASH分区的代码示例:
create table user (
id int auto_increment,
name varchar(50),
primary key (id)
)
partition by hash (id)
partitions 4;
insert into user (name) values ('邱赣');
insert into user (name) values ('何天');
insert into user (name) values ('方加牡');
insert into user (name) values ('贺巧');
insert into user (name) values ('闾丘饱乾');
insert into user (name) values ('丁经');
insert into user (name) values ('韦散');
insert into user (name) values ('东方让');
insert into user (name) values ('赖队瞻');
insert into user (name) values ('郭叹');
insert into user (name) values ('慕容芋岛');
insert into user (name) values ('孙劣');
insert into user (name) values ('王桃');
insert into user (name) values ('郝郑惭');
insert into user (name) values ('余烂');
insert into user (name) values ('宇文酚');
select * from user;
select * from user partition(p0);
select * from user partition(p1);
select * from user partition(p2);
select * from user partition(p3);
KEY分区是根据某一列的哈希值将数据分布到不同的分区。不同于HASH分区,KEY分区使用的是列值的哈希值而不是哈希函数。下面是KEY分区的定义方式、特点以及代码示例。
定义方式:
PARTITION BY KEY
指定使用KEY分区策略。PARTITIONS
关键字指定分区的数量。KEY分区的特点:
以下是一个使用KEY分区的代码示例:
create table person (
id int auto_increment,
name varchar(50),
primary key (id)
)
partition by key(id)
partitions 4;
insert into person (name) values ('邱赣');
insert into person (name) values ('何天');
insert into person (name) values ('方加牡');
insert into person (name) values ('贺巧');
insert into person (name) values ('闾丘饱乾');
insert into person (name) values ('丁经');
insert into person (name) values ('韦散');
insert into person (name) values ('东方让');
insert into person (name) values ('赖队瞻');
insert into person (name) values ('郭叹');
insert into person (name) values ('慕容芋岛');
insert into person (name) values ('孙劣');
insert into person (name) values ('王桃');
insert into person (name) values ('郝郑惭');
insert into person (name) values ('余烂');
insert into person (name) values ('宇文酚');
select * from person;
select * from person partition(p0);
select * from person partition(p1);
select * from person partition(p2);
select * from person partition(p3);
MySQL在5.5版本引入了COLUMNS分区类型,其中包括RANGE COLUMNS分区和LIST COLUMNS分区。以下是对这两种COLUMNS分区的详细说明:
RANGE COLUMNS分区: RANGE COLUMNS分区是根据列的范围值将数据分布到不同的分区的分区策略。它类似于RANGE分区,但是根据多个列的范围值进行分区,而不是只根据一个列。这使得范围的定义更加灵活,可以基于多个列的组合来进行分区。
下面是一个RANGE COLUMNS分区的代码示例:
create table product(
product_id int,
product_name varchar(50),
product_location int,
category_id int
) partition by range columns(category_id,product_location)(
partition p11 values less than (1,1),
partition p12 values less than (1,2),
partition p21 values less than (2,1),
partition p22 values less than (2,2)
);
每个分区根据这两列的范围值进行划分。
LIST COLUMNS分区: LIST COLUMNS分区是根据列的离散值将数据分布到不同的分区的分区策略。它类似于LIST分区,但是根据多个列的离散值进行分区,而不是只根据一个列。这使得离散值的定义更加灵活,可以基于多个列的组合来进行分区。
下面是一个LIST COLUMNS分区的代码示例:
create table product(
product_id int,
product_name varchar(50),
product_location int,
category_id int
) partition by list columns(category_id,product_location)(
partition p12 values in ((1,1),(1,2)),
partition p22 values in ((2,1),(2,2))
);
alter table sale add partition (partition p6 values less than (2026));
alter table sale drop partition p6;
alter table sale REORGANIZE PARTITION p0, p1, p2,p3, p4, p5 INTO (
partition p0 values less than (2021),
partition p1 values less than (2023),
partition p2 values less than (2025)
);
alter table sale analyze partition p0;