POSTGRESQL 分区表初次体验

POSTGRESQL的分区和MYSQL不同,MYSQL是有专门的分区表, 而POSTGRESQL的分区则利用它本身的面向对象的特性来做。 下面我们来简单的体验下。


我们先创建一张父表。 记住,所有的分区表都得继承他。


t_girl=# create table num_master (id int not null primary key);
CREATE TABLE




接下来我们创建一个简单的函数来动态创建分区表。
t_girl=# create or replace function create_partition_table () returns void as $$
t_girl$# declare i int;
t_girl$# declare cnt int;
t_girl$# declare stmt text;
t_girl$# begin
t_girl$# -- Created by ytt at 2013/12/15. Dynamic creating partition tables.
t_girl$# i:= 0;
t_girl$# cnt:=4;
t_girl$# <<lable1>> while i < cnt loop
t_girl$#   stmt := 'create table num_slave'||i+1||'(check(id >='||i*100||' and id <'||(i+1)*100||')) inherits(num_master)';
t_girl$#   execute stmt;
t_girl$#   i:=i + 1;
t_girl$# end loop lable1;
t_girl$# return;
t_girl$# end;
t_girl$# $$ language plpgsql;
CREATE FUNCTION
t_girl=# 




OK。 现在可以执行了。
t_girl=# select create_partition_table();
 create_partition_table 
------------------------
 
(1 row)





列出所有的表
t_girl=# \d
           List of relations
 Schema |    Name    | Type  |  Owner   
--------+------------+-------+----------
 ytt    | num_master | table | postgres
 ytt    | num_slave1 | table | postgres
 ytt    | num_slave2 | table | postgres
 ytt    | num_slave3 | table | postgres
 ytt    | num_slave4 | table | postgres
 ytt    | t1         | table | t_girl
(6 rows)




我们针对父表建立一个触发器函数体,对应其分区表的数据分布。
t_girl=# create or replace function num_insert_trigger()
t_girl-# returns trigger as $$
t_girl$# begin
t_girl$# -- Created by ytt at 2013/12/15. Do how to distribute data.
t_girl$# if (new.id >=0 and new.id <100) then
t_girl$# insert into num_slave1 values (new.*);
t_girl$# elsif (new.id >=100 and new.id <200) then
t_girl$# insert into num_slave2 values(new.*);
t_girl$# elsif (new.id >=200 and new.id <300) then
t_girl$# insert into num_slave3 values (new.*);
t_girl$# elsif (new.id >=300 and new.id <400) then
t_girl$# insert into num_slave4 values (new.*);
t_girl$# else
t_girl$# raise exception 'Column id out of range.';
t_girl$# end if;
t_girl$# return null;
t_girl$# end;
t_girl$# $$
t_girl-# language plpgsql;
CREATE FUNCTION




我们看看已经建好的触发器:


t_girl=# \d+ num_master
                       Table "ytt.num_master"
 Column |  Type   | Modifiers | Storage | Stats target | Description 
--------+---------+-----------+---------+--------------+-------------
 id     | integer | not null  | plain   |              | 
Indexes:
    "num_master_pkey" PRIMARY KEY, btree (id)
Triggers:
    insert_num_slave_trigger BEFORE INSERT ON num_master FOR EACH ROW EXECUTE PROCEDURE ytt.num_insert_trigger()
Child tables: num_slave1,
              num_slave2,
              num_slave3,
              num_slave4
Has OIDs: no






我们现在生成简单的测试数据。
t_girl=# select func_create_sample_data();
 func_create_sample_data 
-------------------------
 
(1 row)



上面的函数生成了大概400行的数据。




为了查看优化器是如何处理查询的,我们来看看简单的查询
t_girl=# explain select * from num_master where id > 30 and id < 120;
                           QUERY PLAN                            
-----------------------------------------------------------------
 Append  (cost=0.00..5.00 rows=91 width=4)
   ->  Seq Scan on num_master  (cost=0.00..0.00 rows=1 width=4)
         Filter: ((id > 30) AND (id < 120))
   ->  Seq Scan on num_slave1  (cost=0.00..2.50 rows=70 width=4)
         Filter: ((id > 30) AND (id < 120))
   ->  Seq Scan on num_slave2  (cost=0.00..2.50 rows=20 width=4)
         Filter: ((id > 30) AND (id < 120))
(7 rows)


t_girl=# 




我也是今天刚刚接触到POSTGRESQL的分区表,有问题,还希望提出。



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、付费专栏及课程。

余额充值