【原创】POSTGRESQL 分区表初次体验

php中文网
发布: 2016-06-07 17:37:29
原创
993人浏览过

POSTGRESQL的分区和MYSQL不同,MYSQL是有专门的分区表,而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$# > while i ='||i*100||' and id 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 and new.id =200 and new.id =300 and new.id

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

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 Seq Scan on num_master (cost=0.00..0.00 rows=1 width=4) Filter: ((id > 30) AND (id Seq Scan on num_slave1 (cost=0.00..2.50 rows=70 width=4) Filter: ((id > 30) AND (id Seq Scan on num_slave2 (cost=0.00..2.50 rows=20 width=4) Filter: ((id > 30) AND (id



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



本文出自 “上帝,咱们不见不散!” 博客,请务必保留此出处

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号