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

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PostgreSQL 支持分区表,它允许将表数据根据特定的条件拆分成多个子表,从而提高查询性能和管理数据。下面是一些关于 PostgreSQL 分区表的基本信息: 1. 分区表定义:在创建表时,可以使用 PARTITION BY 子句指定分区键。常见的分区键类型包括范围(range)、列表(list)和哈希(hash)。 2. 范围分区(Range partitioning):根据某个列的值范围进行分区,例如按时间范围、按数值范围等。可以使用 CREATE TABLE 语句的 PARTITION OF 子句定义每个分区。 3. 列表分区List partitioning):根据某个列的值列表进行分区,例如按地区、按部门等。也可以使用 CREATE TABLE 语句的 PARTITION OF 子句定义每个分区。 4. 哈希分区(Hash partitioning):根据某个列的哈希值进行分区,通常用于数据平均分布的场景。使用 CREATE TABLE 语句的 PARTITION OF 子句定义每个分区。 5. 分区表管理:分区表可以通过 ALTER TABLE 添加或删除分区。还可以使用 EXCHANGE PARTITION 子句将数据从非分区表或已有分区中交换进入分区表。 6. 查询优化:PostgreSQL 的查询优化器会在执行查询时自动识别并只查询相关分区,从而提高查询性能。同时,可以通过查询约束来进一步减少查询的分区范围。 需要注意的是,分区表数据库中的使用需要根据具体的业务需求和数据特点来决定,同时需要合理设计和规划分区键,以及考虑数据维护和查询优化等方面的因素。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值