Postgresql分区表

PostgreSQL 提供了三种分区表实现方式:

  1. range :范围分区
  2. list :列表分区
  3. hash :哈希分区

一、范围分区

根据某个字段的值,将数据存入不同的分区表中。

  1. 创建父表

    create table test_person_table
    (
        name varchar(64),
        age  int
    ) partition by range (age);
    

    根据age 字段进行范围分区

  2. 创建分区表

    create table test_person_table_r1 partition of test_person_table for values from (minvalue )to(10);
    create table test_person_table_r2 partition of test_person_table for values from (11 )to(20);
    create table test_person_table_r3 partition of test_person_table for values from (21 )to(30);
    create table test_person_table_r4 partition of test_person_table for values from (31 )to(40);
    create table test_person_table_r5 partition of test_person_table for values from (41 )to(maxvalue );
    
    • 第一个分区表,范围是从最小值到10
    • 最后一个分区表,范围是从41到最大值
    • 如果插入的数值,没有分区能够覆盖,则会报错

    父表和分区表都创建成功:

    image-20231202112954472

  3. 插入数据

    insert into test_person_table (name, age) values ('xiaoxiao1', 5);
    insert into test_person_table (name, age) values ('xiaoxiao2', 15);
    insert into test_person_table (name, age) values ('xiaoxiao3', 25);
    insert into test_person_table (name, age) values ('xiaoxiao4', 35);
    insert into test_person_table (name, age) values ('xiaoxiao5', 45);
    
  4. 查询数据

    • 查询父表

      image-20231202112727033

    • 查询分区表

      image-20231202112850103

二、列表分区

  1. 创建父表

    create table test_table_partition_by_list
    (
        name varchar(64),
        city varchar(64)
    )partition by list(city);
    

    按照city 字段进行分区

  2. 创建分区表

    create table test_table_partition_by_list_sichuan partition of test_table_partition_by_list for values in ('成都','绵阳');
    create table test_table_partition_by_list_guangdong partition of test_table_partition_by_list for values in ('广州', '深圳');
    create table test_table_partition_by_list_other partition of test_table_partition_by_list default ;
    

    image-20231202114414724

  3. 插入数据

    insert into test_table_partition_by_list (name, city) values ('xiaoxiao1', '成都');
    insert into test_table_partition_by_list (name, city) values ('xiaoxiao2', '绵阳');
    insert into test_table_partition_by_list (name, city) values ('xiaoxiao3', '广州');
    insert into test_table_partition_by_list (name, city) values ('xiaoxiao4', '深圳');
    insert into test_table_partition_by_list (name, city) values ('xiaoxiao5', '北京');
    
  4. 查询数据

    • 父表

      image-20231202114537332

    • 分区表

      image-20231202114556188

      image-20231202114619060

三、哈希分区

  1. 创建父表

    create table test_table_partition_by_hash(
        name varchar(64),
        city varchar(64)
    )partition by hash (city);
    

    按照cityhash值进行分区

  2. 创建分区表

    create table test_table_partition_by_hash_h1 partition of test_table_partition_by_hash for values with (modulus 3, remainder 0);
    create table test_table_partition_by_hash_h2 partition of test_table_partition_by_hash for values with (modulus 3, remainder 1);
    create table test_table_partition_by_hash_h3 partition of test_table_partition_by_hash for values with (modulus 3, remainder 2);
    

    image-20231202115351281

  3. 插入数据

    insert into test_table_partition_by_hash (name, city) values ('xiaoxiao1', 'chengdu');
    insert into test_table_partition_by_hash (name, city) values ('xiaoxiao2', 'chengdu');
    
    insert into test_table_partition_by_hash (name, city) values ('xiaoxiao3', 'guangzhou');
    insert into test_table_partition_by_hash (name, city) values ('xiaoxiao4', 'guangzhou');
    
    insert into test_table_partition_by_hash (name, city) values ('xiaoxiao5', 'shenzhen');
    insert into test_table_partition_by_hash (name, city) values ('xiaoxiao6', 'shanghai');
    insert into test_table_partition_by_hash (name, city) values ('xiaoxiao7', 'beijing');
    
  4. 查询数据

    查询父表

    image-20231202115319441

    查询子表:

    image-20231202115455553

相关推荐

  1. PostgreSQL管理-分区

    2023-12-06 11:36:04       29 阅读
  2. PostgreSQL管理-继承

    2023-12-06 11:36:04       34 阅读
  3. postgresql 锁定问题处理

    2023-12-06 11:36:04       29 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-06 11:36:04       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-06 11:36:04       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-06 11:36:04       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-06 11:36:04       18 阅读

热门阅读

  1. Vue3中定义变量是选择ref还是reactive?

    2023-12-06 11:36:04       30 阅读
  2. KM算法,C语言版本和Matlab版本

    2023-12-06 11:36:04       37 阅读
  3. Flask 最佳实践(二)

    2023-12-06 11:36:04       30 阅读
  4. jQuery&layui

    2023-12-06 11:36:04       33 阅读
  5. xml中的特殊字符

    2023-12-06 11:36:04       26 阅读
  6. python实现模拟用户点击行为测试

    2023-12-06 11:36:04       36 阅读
  7. 使用Vue写一个日期选择器

    2023-12-06 11:36:04       39 阅读
  8. 方法重写,函数重载,方法覆盖的区别

    2023-12-06 11:36:04       42 阅读