Oracle表分区

表分区

目的:

  1. 安全(鸡蛋不要放在一个篮子里)
  2. 效率****(快速找到南方it学院所有姓张的人)
  3. 便于维护

场景:

  1. 数据量极大(大于 2G)
  2. 历史数据比重比较大

分类:

  1. 范围分区
  2. 列表分区
  3. 哈希分区(hash)

在以上分区的基础上,可以两两结合,形成 复合分区,但常用的就是两种:

  1. 范围-列表分区
  2. 范围-哈希分区

范围分区:

 
  1. -- 创建一个普通表的语句

  2. create table person1 (id int primary key, name varchar2(20), birth date);

  3. -- 数据将会在同一个表空间同一个段内

  4. insert into person1 values (1, 'sss', sysdate);

  5.  
  6. -- 创建一个分区表

  7. -- 这里是按照生日进行范围分区

  8. -- 语句的基本格式就是在普通建表的语句上,增加 partition by 语句块

  9. create table person2 (name varchar2(20), birth date)

  10. partition by range (birth)

  11. (

  12. partition p1 values less than (to_date('19950101','yyyymmdd')), -- 'values less than'

  13. partition p2 values less than (to_date('20000101','yyyymmdd')),

  14. partition p3 values less than (maxvalue) -- 默认分区

  15. );

  16. -- 插入,数据会根据分区的情况进入不同的分区内

  17. insert into person2 values ('张三', to_date('19940707'));

  18. insert into person2 values ('李四', to_date('19980707'));

  19. insert into person2 values ('王五', to_date('20040707'));

  20. -- 查询表中所有数据

  21. select * from person2;

  22. -- 查询特定分区上数据

  23. select * from person2 partition (p3);

  24.  
  25.  
  26. -- 可以为不同的分区指定不同的表空间

  27. -- 没有指定表空间的分区,使用用户的默认表空间

  28. -- 所以,一个表内的数据可以存在于不同表空间里,也就是可以存放在不同数据文件中,不同磁盘上

  29. -- 因此,分区表能增强数据的安全性

  30. create table person3 (name varchar2(20), birth date)

  31. partition by range (birth)

  32. (

  33. partition p1 values less than (to_date('19950101','yyyymmdd')) tablespace system,

  34. partition p2 values less than (to_date('20000101','yyyymmdd')) tablespace sysaux,

  35. partition p3 values less than (maxvalue) tablespace users

  36. );

  37.  
  38.  
  39. -- 可以在其他类型上进行范围分区

  40. -- 也可以在多个字段上进行范围分区

  41. create table person4 (name varchar2(20), birth date, score number)

  42. partition by range (birth, score)

  43. (

  44. partition p1 values less than (to_date('19900101','yyyymmdd'), 60),

  45. partition p2 values less than (to_date('19900101','yyyymmdd'), 90),

  46. partition p3 values less than (to_date('19990101','yyyymmdd'), 60),

  47. partition p4 values less than (to_date('19990101','yyyymmdd'), 90),

  48. partition p5 values less than (maxvalue, maxvalue)

  49. );

列表分区:

 
  1. -- 如果是生日的这样的字段,数据是连续的,应该使用分为分区

  2. create table person (name varchar2(20), birth date)

  3. partition by range(birth)

  4. (

  5. partition p1 values less than (to_date('19900101', 'yyyymmdd')) tablespace users,

  6. partition p2 values less than (maxvalue)

  7. );

  8. insert into person values ('aaa', to_date('19871212', 'yyyymmdd'));

  9. select * from person partition (p1);

  10.  
  11.  
  12. /*

  13. where birth between 1987 and 1990

  14. where sex in ('男', '女')

  15. */

  16.  
  17. -- 但是像性别、民族等字段,更适合使用的是列表分区

  18. -- 下面一个例子,使用性别作为分区字段,男的一个区,女的一个区

  19. create table person2 (name varchar2(20), sex varchar(10))

  20. partition by list (sex)

  21. (

  22. partition p1 values ('男'),

  23. partition p2 values ('女')

  24. );

  25. insert into person2 values ('aaa', '男');

  26. insert into person2 values ('bbb', '女');

  27. insert into person2 values ('ccc', '未知'); -- 报错

  28. select * from person2 partition (p2);

  29.  
  30. -- 默认分区的写法

  31. create table person3 (name varchar2(20), sex varchar(10))

  32. partition by list (sex)

  33. (

  34. partition p1 values ('男'),

  35. partition p2 values ('女'),

  36. partition p3 values (default)

  37. );

  38. insert into person3 values ('ccc', '未知');

  39. select * from person3 partition (p3);

  40.  
  41.  
  42. -- 可以为每个分区指定表空间

  43. create table person3 (name varchar2(20), sex varchar(10))

  44. partition by list (sex)

  45. (

  46. partition p1 values ('男') tablespace users,

  47. partition p2 values ('女') tablespace system,

  48. partition p3 values (default)

  49. );

哈希分区:

 
  1. -- 哈希分区

  2. -- 主要用在一些比较离散,不好分类的数据上,比如产品名字

  3. -- 让 oracle 使用哈希算法自动计算数据的分区

  4.  
  5. -- 创建语句,非常简单

  6. create table person4 (name varchar2(20), sex varchar2(10))

  7. partition by hash (name)

  8. (

  9. partition p1,

  10. partition p2 tablespace users

  11. );

  12. insert into person4 values ('aaa', '男');

  13. insert into person4 values ('收款', '男');

  14. select * from person4 partition (p1);

  15.  
  16. -- 上面的语句可以进一步简化为:

  17. create table person5 (name varchar2(20), sex varchar2(10))

  18. partition by hash (name)

  19. partitions 5;

  20.  
  21. -- 为每个分区指定表空间

  22. create table person6 (name varchar2(20), sex varchar2(10))

  23. partition by hash (name)

  24. partitions 3 store in (users, system, sysaux);

范围-列表分区:

 
  1. -- 首先,按照生日进行列表分区,分了三个区

  2. -- 其次,在每个分区内,又按照性别分了三个区

  3. -- 所以,总共是 3 个分区 9 个子分区

  4. create table person8 (name varchar2(20), sex varchar2(10), birth date)

  5. partition by range(birth)

  6. subpartition by list(sex)

  7. subpartition template

  8. (

  9. subpartition sp01 values ('男'),

  10. subpartition sp02 values ('女'),

  11. subpartition sp03 values (default)

  12. )

  13. (

  14. partition p1 values less than (to_date('19900101', 'yyyymmdd')),

  15. partition p2 values less than (to_date('20000101', 'yyyymmdd')),

  16. partition p3 values less than (maxvalue)

  17. );

  18.  
  19. insert into person8 values ('aaa', '男', to_date('19900202'));

  20. -- 查询这条数据,有以下三种方式:

  21. select * from person8;

  22. select * from person8 partition (p1);

  23. select * from person8 subpartition (p1_sp01);

范围-哈希分区:

 
  1. -- 先按照生日,将数据分为三个区

  2. -- 然后在每个分区内,又按照哈希算法分成了三个区

  3. -- 这样就保证了每个分区内的数据尽量的少,而且分区进行平衡

  4. create table person7 (name varchar2(20), birth date)

  5. partition by range (birth)

  6. subpartition by hash (name) subpartitions 3

  7. (

  8. partition p1 values less than (to_date('19900101', 'yyyymmdd')),

  9. partition p2 values less than (to_date('20000101', 'yyyymmdd')),

  10. partition p3 values less than (maxvalue)

  11. );

相关字典表:

 
  1. select * from user_objects where object_name ='PERSON8';

  2. select * from user_tables where table_name = 'PERSON8';

  3. select * from user_tab_partitions where table_name = 'PERSON8';

  4. select * from user_tab_subpartitions where table_name = 'PERSON8';

操作表分区:

 
  1. -- 添加分区

  2. alter table person add partition p9 values less than (MAXVALUE);

  3. alter table person add partition p9 values (1, 2); -- 针对 list 分区

  4. alter table person add partition; -- 针对 hash 分区

  5.  
  6. -- 删除分区

  7. alter table person drop partition p3;

  8.  
  9. -- 删除分区内数据

  10. alter table person truncate partition p3;

  11.  
  12. -- 合并相邻分区

  13. alter table person merge partitions p2, p3 into partition p8;

  14.  
  15. -- 拆分分区

  16. alter table person split partition p2 at (3000) into (partition p3, partition p14); -- 范围分区的拆分

  17. alter table person split partition p2 values (1,2) into (partition p3, partition p4); -- 列表分区的拆分

  18. alter table person split partition p2 into (partition p3 values (1, 2), partition p4 values (3), partition p5); -- 列表分区的拆分

  19.  
  20. -- 重命名分区

  21.  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值