PostgreSQL 10分区表及性能测试报告小结
作者简介:
中国比较早的postgresql使用者,2001年就开始使用postgresql,自2003年底至2014年一直担任pgsql中国社区论坛postgresql的论坛板块版主、管理员,参与postgresql讨论和发表专题文章7000多贴.拥有15年的erp设计,开发和实施经验,开源mrp系统postmrp就是我的作品,该应用软件是一套基于postgresql专业的制造业管理软件系统.目前任职于--中国第一物流控股有限公司/运力宝(北京)科技有限公司,为公司的研发部经理
一、 测试环境
操作系统:centos 6.4
postgresql版本号:10.0
cpu:intel(r) xeon(r) cpu e5-2407 v2 @ 2.40ghz 4核心 4线程
内存:32g
硬盘:2t sas 7200
二、 编译安装postgresql 10
--编译安装及初始化
[root@ad source]# git clone git://git.postgresql.org/git/postgresql.git [root@ad source]# cd postgresql [root@ad source]# ./configure --prefix=/usr/local/pgsql10 [root@ad postgresql]# gmake -j 4 [root@ad postgresql]# gmake install [root@ad postgresql]# su postgres [postgres@ad postgresql]# /usr/local/pgsql10/bin/initdb --no-locale -e utf8 -d /home/postgres/data10/ -u postgres
--修改一些参数
postgresql.conf listen_addresses = '*' port = 10000 shared_buffers = 8096mb maintenance_work_mem = 512mb effective_cache_size = 30gb log_destination = 'csvlog' logging_collector = on log_directory = 'log' log_filename = 'postgresql-%y-%m-%d_%h%m%s.log' log_file_mode = 0600 log_checkpoints = off log_connections = off log_disconnections = off log_duration = off log_line_prefix = '%m %h %a %u %d %x [%p] ' log_statement = 'none' log_timezone = 'prc' track_activity_query_size = 4096 max_wal_size = 32gb min_wal_size = 2gb checkpoint_completion_target = 0.5
pg_hba.conf增加许可条目
host all all 192.168.1.0/24 trust
--启动服务
[postgres@ad data10]$ /usr/local/pgsql10/bin/pg_ctl start -d /home/postgres/data10/ --连接数据库 [postgres@ad data10]$ /usr/local/pgsql10/bin/psql -p 10000 -u postgres -h 127.0.0.1 -d postgres psql (10devel) type "help" for help. postgres=#
postgresql的分区表跟先前版本一样,也要先建立主表,然后再建立子表,使用继承的特性,但不需要手工写规则了,这个比较赞阿。目前支持range、list分区,10正式版本发布时不知会不会支持其它方法。
range分区表
1、分区主表
create table order_range(id bigserial not null,userid integer,product text, createdate date) partition by range ( createdate );
分区主表不能建立全局约束,使用partition by range(xxx)说明分区的方式,xxx可以是多个字段,表达式……,具体见https://www.postgresql.org/docs/devel/static/sql-createtable.html
2、分区子表
create table order_range(id bigserial not null,userid integer,product text, createdate date not null) partition by range ( createdate ); create table order_range_201701 partition of order_range(id primary key,userid,product, createdate) for values from ('2017-01-01') to ('2017-02-01'); create table order_range_201702 partition of order_range(id primary key,userid,product, createdate) for values from ('2017-02-01') to ('2017-03-01');
- 说明:
- 建立分区表时必需指定主表。
- 分区表和主表的 列数量,定义 必须完全一致。
- 分区表的列可以单独增加default值,或约束。
- 当用户向主表插入数据库时,系统自动路由到对应的分区,如果没有找到对应分区,则抛出错误。
- 指定分区约束的值(范围,list值),范围,list不能重叠,重叠的路由会卡壳。
- 指定分区的列必需设置成not null,如建立主表时没设置系统会自动加上。
- range分区范围为 >=最小值 and <最大值……
- 不支持通过更新的方法把数据从一个区移动到另外一个区,这样做会报错。如果要这样做的话需要删除原来的记录,再insert一条新的记录。
- 修改主表的字段名,字段类型时,会自动同时修改所有的分区。
- truncate 主表时,会清除所有继承表分区的记录,如果要清除单个分区,请对分区进行操作。
- drop主表时会把所有子表一起给drop掉,如果drop单个分区,请对分区进行操作。
- 使用psql能查看分区表的详细定义。
postgres=# \d+ order_range table "public.order_range" column | type | collation | nullable | default | storage | stats target | description ------------+---------+-----------+----------+-----------------------------------------+----------+--------------+------------- id | bigint | | not null | nextval('order_range_id_seq'::regclass) | plain | | userid | integer | | | | plain | | product | text | | | | extended | | createdate | date | | not null | | plain | | partition key: range (createdate) partitions: order_range_201701 for values from ('2017-01-01') to ('2017-02-01'), order_range_201702 for values from ('2017-02-01') to ('2017-03-01') postgres=#
list分区表
1、分区主表
create table order_list(id bigserial not null,userid integer,product text,area text, createdate date) partition by list( area );
2、分区子表
create table order_list_gd partition of order_list(id primary key,userid,product,area,createdate) for values in ('广东'); create table order_list_bj partition of order_list(id primary key,userid,product,area,createdate) for values in ('北京');
多级分区表
先按地区分区,再按日期分区
1、主表
create table order_range_list(id bigserial not null,userid integer,product text,area text, createdate date) partition by list ( area );
2、一级分区表
create table order_range_list_gd partition of order_range_list for values in ('广东') partition by range(createdate); create table order_range_list_bj partition of order_range_list for values in ('北京') partition by range(createdate);
3、二级分区表
create table order_range_list_gd_201701 partition of order_range_list_gd(id primary key,userid,product,area,createdate) for values from ('2017-01-01') to ('2017-02-01'); create table order_range_list_gd_201702 partition of order_range_list_gd(id primary key,userid,product,area,createdate) for values from ('2017-02-01') to ('2017-03-01'); create table order_range_list_bj_201701 partition of order_range_list_bj(id primary key,userid,product,area,createdate) for values from ('2017-01-01') to ('2017-02-01'); create table order_range_list_bj_201702 partition of order_range_list_bj(id primary key,userid,product,area,createdate) for values from ('2017-02-01') to ('2017-03-01');
直接操作分区也要受分区规则的约束
postgres=# insert into order_range_201702 (id,userid,product,createdate) values(1,
(random()::numeric(7,6)*1000000)::integer,md5(random()::text),('2017-01-01'));
error: new row for relation "order_range_201702" violates partition constraint
detail: failing row contains (1, 322345, 51a9357a78416d11a018949a42dd2f8d, 2017-01-01).
insert提示违反了分区约束
postgres=# update order_range_201701 set createdate='2017-02-01' where createdate='2017-01-17';
error: new row for relation "order_range_201701" violates partition constraint
detail: failing row contains (1, 163357, 7e8fbe7b632a54ba1ec401d969f3259a, 2017-02-01).
update提示违反了分区约束
如果分区表是外部表,则约束失效,后面有介绍
使用alter table xxx attach[detach] partition 增加或删除分区
1、移除分区
录入2条测试数据
postgres=# insert into order_range (userid,product,createdate) values((random()::numeric(7,6)*1000000)::integer,md5(random()::text),('2017-01-01'::date+ (random()*31)::integer)); insert 0 1 time: 25.006 ms postgres=# insert into order_range (userid,product,createdate) values((random()::numeric(7,6)*1000000)::integer,md5(random()::text),('2017-01-01'::date+ (random()*31)::integer)); insert 0 1 time: 7.601 ms postgres=# select * from order_range; id | userid | product | createdate ----+--------+----------------------------------+------------ 1 | 163357 | 7e8fbe7b632a54ba1ec401d969f3259a | 2017-01-17 2 | 349759 | 8095c9036295d3c800dace9069f9c102 | 2017-01-27 (2 rows)
删除分区
postgres=# alter table order_range detach partition order_range_201701; alter table time: 14.129 ms
查看确认分区没了
postgres=# \d+ order_range; table "public.order_range" column | type | collation | nullable | default | storage | stats target | description ------------+---------+-----------+----------+-----------------------------------------+----------+--------------+------------- id | bigint | | not null | nextval('order_range_id_seq'::regclass) | plain | | userid | integer | | | | plain | | product | text | | | | extended | | createdate | date | | not null | | plain | | partition key: range (createdate) partitions: order_range_201702 for values from ('2017-02-01') to ('2017-03-01') postgres=#
数据也查不出来了
postgres=# select * from order_range; id | userid | product | createdate ----+--------+---------+------------ (0 rows) time: 0.505 ms
但分区表还在
postgres=# select * from order_range_201701; id | userid | product | createdate ----+--------+----------------------------------+------------ 1 | 163357 | 7e8fbe7b632a54ba1ec401d969f3259a | 2017-01-17 2 | 349759 | 8095c9036295d3c800dace9069f9c102 | 2017-01-27 (2 rows) time: 0.727 ms
2、添加分区
postgres=# alter table order_range attach partition order_range_201701 for values from ('2017-01-01') to ('2017-02-01'); error: column "createdate" in child table must be marked not null time: 0.564 ms
增加子表里,约束需要与主表一致
postgres=# alter table order_range_201701 alter column createdate set not null; alter table time: 17.345 ms postgres=# alter table order_range attach partition order_range_201701 for values from ('2017-01-01') to ('2017-01-15'); error: partition constraint is violated by some row time: 1.276 ms
加回来时可以修改其约束范围,但数据必需在约束的规则范围内
postgres=# alter table order_range attach partition order_range_201701 for values from ('2017-01-01') to ('2017-02-01'); alter table time: 18.407 ms
分区表又加回来了
postgres=# \d+ order_range table "public.order_range" column | type | collation | nullable | default | storage | stats target | description ------------+---------+-----------+----------+-----------------------------------------+----------+--------------+------------- id | bigint | | not null | nextval('order_range_id_seq'::regclass) | plain | | userid | integer | | | | plain | | product | text | | | | extended | | createdate | date | | not null | | plain | | partition key: range (createdate) partitions: order_range_201701 for values from ('2017-01-01') to ('2017-02-01'), order_range_201702 for values from ('2017-02-01') to ('2017-03-01') postgres=# select * from order_range; id | userid | product | createdate ----+--------+----------------------------------+------------ 1 | 163357 | 7e8fbe7b632a54ba1ec401d969f3259a | 2017-01-17 2 | 349759 | 8095c9036295d3c800dace9069f9c102 | 2017-01-27 (2 rows) time: 0.627 ms
添加外部表作为分区表
--增加一个新库,建立需要的外部表
[postgres@ad root]$ /usr/local/pgsql10/bin/psql -p 10000 -u postgres -h 127.0.0.1 -d postgres psql (10devel) type "help" for help. #建立数据库 postgres=# create database postgres_fdw; create database postgres_fdw=# create table order_range_fdw(id bigserial not null,userid integer,product text, createdate date not null); create table postgres_fdw=# #录入一条测试数据 postgres_fdw=# insert into order_range_fdw (userid,product,createdate) values((random()::numeric(7,6)*1000000)::integer,md5(random()::text),('2017-01-01'::date- (random()*31)::integer)); insert 0 1 postgres_fdw=# select * from order_range_fdw; id | userid | product | createdate ----+--------+----------------------------------+------------ 2 | 300686 | 55956a07742d6aebdef7ebb78c2400d7 | 2016-12-22 (1 row)
--在postgres库中增加外部表order_range_fdw
[postgres@ad root]$ /usr/local/pgsql10/bin/psql -p 10000 -u postgres -h 127.0.0.1 -d postgres psql (10devel) type "help" for help. #增加postgres_fdw模块 postgres=# create extension postgres_fdw; create extension #建立外部服务器 postgres=# create server foreign_server foreign data wrapper postgres_fdw options (host '192.168.1.10', port '10000', dbname 'postgres_fdw'); create server #建立外部服务器用户标识 postgres=# create user mapping for postgres postgres-# server foreign_server postgres-# options (user 'postgres', password ''); create user mapping #建立外部表 postgres=# create foreign table order_range_fdw ( postgres(# id bigint not null, postgres(# userid integer, postgres(# product text, postgres(# createdate date not null postgres(# ) postgres-# server foreign_server postgres-# options (schema_name 'public', table_name 'order_range_fdw'); create foreign table #查询数据 postgres=# select * from order_range_fdw; id | userid | product | createdate ----+--------+----------------------------------+------------ 2 | 300686 | 55956a07742d6aebdef7ebb78c2400d7 | 2016-12-22 (1 row) --将外部表作为分区表添加到order_range下 #添加分区表 postgres=# alter table order_range attach partition order_range_fdw for values from ('1900-01-01') to ('2017-01-01'); alter table #查看order_range下的所有分区表 postgres=# \d+ order_range table "public.order_range" column | type | collation | nullable | default | storage | stats target | description ------------+---------+-----------+----------+-----------------------------------------+----------+--------------+------------- id | bigint | | not null | nextval('order_range_id_seq'::regclass) | plain | | userid | integer | | | | plain | | product | text | | | | extended | | createdate | date | | not null | | plain | | partition key: range (createdate) partitions: order_range_201701 for values from ('2017-01-01') to ('2017-02-01'), order_range_201702 for values from ('2017-02-01') to ('2017-03-01'), order_range_fdw for values from ('1900-01-01') to ('2017-01-01') #查询数据 postgres=# select * from order_range where createdate<'2017-01-01'; id | userid | product | createdate ----+--------+----------------------------------+------------ 2 | 300686 | 55956a07742d6aebdef7ebb78c2400d7 | 2016-12-22 (1 row) #查看执行计划 postgres=# explain select * from order_range where createdate<'2017-01-01'; query plan -------------------------------------------------------------------------------- append (cost=100.00..131.79 rows=379 width=48) -> foreign scan on order_range_fdw (cost=100.00..131.79 rows=379 width=48) (2 rows) #测试看看能不能更新数据 postgres=# insert into order_range (userid,product,createdate) values((random()::numeric(7,6)*1000000)::integer,md5(random()::text),('2017-01-01'::date- (random()*31)::integer)); error: cannot route inserted tuples to a foreign table postgres=# update order_range set createdate='2016-12-01' where createdate='2016-12-22'; update 1 postgres=# select * from order_range where createdate<'2017-01-01'; id | userid | product | createdate ----+--------+----------------------------------+------------ 2 | 300686 | 55956a07742d6aebdef7ebb78c2400d7 | 2016-12-01 (1 row) postgres=# delete from order_range where createdate='2016-12-01'; delete 1 postgres=# select * from order_range where createdate<'2017-01-01'; id | userid | product | createdate ----+--------+---------+------------ (0 rows) postgres=#
插入数据时竟然不能路由到外部表,这个是处于什么考虑呢???,源码中只是提示 /* we do not yet have a way to insert into a foreign partition */
还没有办法这样做,猜猜后面的版本应该能实现
下面再说说使用外部表作为分区表还有一些问题
1、无法约束向分区表插入约束外的数据,如下所示
postgres=# \d+ order_range table "public.order_range" column | type | collation | nullable | default | storage | stats target | description ------------+---------+-----------+----------+-----------------------------------------+----------+--------------+------------- id | bigint | | not null | nextval('order_range_id_seq'::regclass) | plain | | userid | integer | | | | plain | | product | text | | | | extended | | createdate | date | | not null | | plain | | partition key: range (createdate) partitions: order_range_201701 for values from ('2017-01-01') to ('2017-02-01'), order_range_201702 for values from ('2017-02-01') to ('2017-03-01'), order_range_fdw for values from ('1900-01-01') to ('2017-01-01') postgres=# postgres=# insert into order_range_fdw (id,userid,product,createdate) values(1, (random()::numeric(7,6)*1000000)::integer,md5(random()::text),('2017-01-01')); insert 0 1 postgres=# select * from order_range; id | userid | product | createdate ----+--------+----------------------------------+------------ 1 | 163357 | 7e8fbe7b632a54ba1ec401d969f3259a | 2017-01-17 2 | 349759 | 8095c9036295d3c800dace9069f9c102 | 2017-01-27 1 | 621895 | 5546c6e2a7006b52b5c2df55e19b3759 | 2017-02-01 4 | 313019 | 445316004208e09fb4e7eda2bf5b0865 | 2017-01-01 1 | 505836 | 6e9232c4863c82a2e97b9157996572ea | 2017-01-01 (5 rows) postgres=# select * from order_range where createdate ='2017-01-01'; id | userid | product | createdate ----+--------+---------+------------ (0 rows)
如果这样操作会导致数据查询出现不匹配。
2、sql执行时无法下推
sql执行无法下推的话对于聚集函数的执行存在很大的性能问题,使用时一定要特别的注意,如下所示
postgres=# delete from order_range_fdw; delete 1 postgres=# insert into order_range_fdw (id,userid,product,createdate) values(1, (random()::numeric(7,6)*1000000)::integer,md5(random()::text),('2016-01-01')); insert 0 1 postgres=# insert into order_range_fdw (id,userid,product,createdate) values(1, (random()::numeric(7,6)*1000000)::integer,md5(random()::text),('2016-02-01')); insert 0 1 #访问order_range,基执行是 postgres=# explain analyze select count(1) from order_range where createdate<'2017-01-01'; query plan ------------------------------------------------------------------------------------------ aggregate (cost=178.27..178.28 rows=1 width=8) (actual time=0.656..0.656 rows=1 loops=1) -> append (cost=100.00..175.42 rows=1138 width=0) (actual time=0.647..0.649 rows=2 loops=1) -> foreign scan on order_range_fdw (cost=100.00..175.42 rows=1138 width=0) (actual time=0.647..0.648 rows=2 loops=1) planning time: 0.267 ms execution time: 1.122 ms (5 rows) #直接访问外部表 postgres=# explain analyze select count(1) from order_range_fdw where createdate<'2017-01-01'; query plan ------------------------------------------------------------------------------------------- foreign scan (cost=102.84..155.54 rows=1 width=8) (actual time=0.661..0.662 rows=1 loops=1) relations: aggregate on (public.order_range_fdw) planning time: 0.154 ms execution time: 1.051 ms (4 rows)
3、sql查询需要访问的分区表中包含了“外部分区表”和“非外部分区表”时, 无法使用parallel seq scan,如下所示
#插入100w数据到分区表中 postgres=# insert into order_range (userid,product,createdate) select (random()::numeric(7,6)*1000000)::integer,md5(random()::text),('2017-01-01'::date+ (random()*58)::integer) from generate_series(1,1000000); insert 0 1000000 #访问所有的分区表 postgres=# explain select count(1) from order_range; query plan --------------------------------------------------------------------------------------- aggregate (cost=24325.22..24325.23 rows=1 width=8) -> append (cost=0.00..21558.23 rows=1106797 width=0) -> seq scan on order_range_201701 (cost=0.00..11231.82 rows=580582 width=0) -> seq scan on order_range_201702 (cost=0.00..10114.02 rows=522802 width=0) -> foreign scan on order_range_fdw (cost=100.00..212.39 rows=3413 width=0) (5 rows) #只访问“非外部分区表” postgres=# explain select count(1) from order_range where createdate>='2017-01-01'; query plan ------------------------------------------------------------------------------------- finalize aggregate (cost=17169.84..17169.85 rows=1 width=8) -> gather (cost=17169.62..17169.83 rows=2 width=8) workers planned: 2 -> partial aggregate (cost=16169.62..16169.63 rows=1 width=8) -> append (cost=0.00..15803.52 rows=146440 width=0) -> parallel seq scan on order_range_201701 (cost=0.00..8449.86 rows=80636 width=0) filter: (createdate >= '2017-01-01'::date) -> parallel seq scan on order_range_201702 (cost=0.00..7353.66 rows=65804 width=0) filter: (createdate >= '2017-01-01'::date) (9 rows) postgres=#
外部分区表的应用场景
将业务库上的不再修改的冷数全部分离到另一个节点上面,然后做为外部分区表挂上来。这样可以保持业务库的容量尽可以的轻,同时也不会对业务有侵入,这一点是非常的友好。但要注意sql执行无法下推的问题,无法使用parallel seq scan问题。
如果在后面版本中能解决fdw partition insert路由问题和sql语句执行下推问题那么就可以拿来做olap应用了。
四、建立测试业务表
下面模似一个用户收支流水表
--非分区表
create table t_pay_all (id serial not null primary key,userid integer not null,pay_money float8 not null,createdate date not null); create index t_pay_all_userid_idx on t_pay_all using btree(userid); create index t_pay_all_createdate_idx on t_pay_all using btree(createdate);
--分区表
生成12个分区,一个月份一个表
create table t_pay (id serial not null,userid integer not null,pay_money float8 not null,createdate date not null) partition by range (createdate); create table t_pay_201701 partition of t_pay(id primary key,userid,pay_money,createdate) for values from ('2017-01-01') to ('2017-02-01'); create index t_pay_201701_createdate_idx on t_pay_201701 using btree(createdate); create index t_pay_201701_userid_idx on t_pay_201701 using btree(userid); create table t_pay_201702 partition of t_pay(id primary key,userid,pay_money,createdate) for values from ('2017-02-01') to ('2017-03-01'); create index t_pay_201702_createdate_idx on t_pay_201702 using btree(createdate); create index t_pay_201702_userid_idx on t_pay_201702 using btree(userid); create table t_pay_201703 partition of t_pay(id primary key,userid,pay_money,createdate) for values from ('2017-03-01') to ('2017-04-01'); create index t_pay_201703_createdate_idx on t_pay_201703 using btree(createdate); create index t_pay_201703_userid_idx on t_pay_201703 using btree(userid); create table t_pay_201704 partition of t_pay(id primary key,userid,pay_money,createdate) for values from ('2017-04-01') to ('2017-05-01'); create index t_pay_201704_createdate_idx on t_pay_201704 using btree(createdate); create index t_pay_201704_userid_idx on t_pay_201704 using btree(userid); create table t_pay_201705 partition of t_pay(id primary key,userid,pay_money,createdate) for values from ('2017-05-01') to ('2017-06-01'); create index t_pay_201705_createdate_idx on t_pay_201705 using btree(createdate); create index t_pay_201705_userid_idx on t_pay_201705 using btree(userid); create table t_pay_201706 partition of t_pay(id primary key,userid,pay_money,createdate) for values from ('2017-06-01') to ('2017-07-01'); create index t_pay_201706_createdate_idx on t_pay_201706 using btree(createdate); create index t_pay_201706_userid_idx on t_pay_201706 using btree(userid); create table t_pay_201707 partition of t_pay(id primary key,userid,pay_money,createdate) for values from ('2017-07-01') to ('2017-08-01'); create index t_pay_201707_createdate_idx on t_pay_201707 using btree(createdate); create index t_pay_201707_userid_idx on t_pay_201707 using btree(userid); create table t_pay_201708 partition of t_pay(id primary key,userid,pay_money,createdate) for values from ('2017-08-01') to ('2017-09-01'); create index t_pay_201708_createdate_idx on t_pay_201708 using btree(createdate); create index t_pay_201708_userid_idx on t_pay_201708 using btree(userid); create table t_pay_201709 partition of t_pay(id primary key,userid,pay_money,createdate) for values from ('2017-09-01') to ('2017-10-01'); create index t_pay_201709_createdate_idx on t_pay_201709 using btree(createdate); create index t_pay_201709_userid_idx on t_pay_201709 using btree(userid); create table t_pay_201710 partition of t_pay(id primary key,userid,pay_money,createdate) for values from ('2017-10-01') to ('2017-11-01'); create index t_pay_201710_createdate_idx on t_pay_201710 using btree(createdate); create index t_pay_201710_userid_idx on t_pay_201710 using btree(userid); create table t_pay_201711 partition of t_pay(id primary key,userid,pay_money,createdate) for values from ('2017-11-01') to ('2017-12-01'); create index t_pay_201711_createdate_idx on t_pay_201711 using btree(createdate); create index t_pay_201711_userid_idx on t_pay_201711 using btree(userid); create table t_pay_201712 partition of t_pay(id primary key,userid,pay_money,createdate) for values from ('2017-12-01') to ('2018-01-01'); create index t_pay_201712_createdate_idx on t_pay_201712 using btree(createdate); create index t_pay_201712_userid_idx on t_pay_201712 using btree(userid);
五、性能测试
数据导入
--生成测试数据1000w条记录(尽可能平均分布)
postgres=# copy (select (random()::numeric(7,6)*1000000)::integer as userid,round((random()*100)::numeric,2) as pay_money,('2017-01-01'::date+ (random()*364)::integer) as createtime from generate_series(1,10000000)) to '/home/pg/data.txt'; copy 10000000 time: 42674.548 ms (00:42.675)
--非分区表数据导入测试
postgres=# copy t_pay_all(userid,pay_money,createdate) from '/home/pg/data.txt'; copy 10000000 time: 114258.743 ms (01:54.259)
--分区表数据导入测试
postgres=# copy t_pay(userid,pay_money,createdate) from '/home/pg/data.txt'; copy 10000000 time: 186358.447 ms (03:06.358) postgres=#
结论:数据导入时性能相差大约是一半,所以大数据量导入时最好直接导成分区表数据,然后直接对分区表进行操作
查询某一天的数据--直接从cache里取数据
--非分区表
postgres=# explain (analyze,buffers) select * from t_pay_all where createdate ='2017-06-01'; query plan ------------------------------------------------------------------------------------------- bitmap heap scan on t_pay_all (cost=592.06..50797.88 rows=27307 width=20) (actual time=14.544..49.039 rows=27384 loops=1) recheck cond: (createdate = '2017-06-01'::date) heap blocks: exact=22197 buffers: shared hit=22289 -> bitmap index scan on t_pay_all_createdate_idx (cost=0.00..585.24 rows=27307 width=0) (actual time=7.121..7.121 rows=27384 loops=1) index cond: (createdate = '2017-06-01'::date) buffers: shared hit=92 planning time: 0.153 ms execution time: 51.583 ms (9 rows) time: 52.272 ms
--分区表
postgres=# explain (analyze,buffers) select * from t_pay where createdate ='2017-06-01'; query plan ---------------------------------------------------------------------------------------------- append (cost=608.92..6212.11 rows=27935 width=20) (actual time=4.880..27.032 rows=27384 loops=1) buffers: shared hit=5323 -> bitmap heap scan on t_pay_201706 (cost=608.92..6212.11 rows=27935 width=20) (actual time=4.879..21.990 rows=27384 loops=1) recheck cond: (createdate = '2017-06-01'::date) heap blocks: exact=5226 buffers: shared hit=5323 -> bitmap index scan on t_pay_201706_createdate_idx (cost=0.00..601.94 rows=27935 width=0) (actual time=3.399..3.399 rows=27384 loops=1) index cond: (createdate = '2017-06-01'::date) buffers: shared hit=97 planning time: 0.521 ms execution time: 30.061 ms (11 rows)
结论:分区表的planning time时间明显比非分区表要高,但比起execution time基本可以忽略。
查询某个时间范围的数据
1、时间范围落在同一个分区内
--非分区表
postgres=# explain (analyze,buffers)select * from t_pay_all where createdate >='2017-06-01' and createdate<'2017-07-01'; query plan ------------------------------------------------------------------------------------------ bitmap heap scan on t_pay_all (cost=19802.01..95862.00 rows=819666 width=20) (actual time=115.210..459.547 rows=824865 loops=1) recheck cond: ((createdate >= '2017-06-01'::date) and (createdate < '2017-07-01'::date)) heap blocks: exact=63701 buffers: shared read=66578 -> bitmap index scan on t_pay_all_createdate_idx (cost=0.00..19597.10 rows=819666 width=0) (actual time=101.453..101.453 rows=825865 loops=1) index cond: ((createdate >= '2017-06-01'::date) and (createdate < '2017-07-01'::date)) buffers: shared read=2877 planning time: 0.166 ms execution time: 504.297 ms (9 rows) time: 505.021 ms postgres=# explain (analyze,buffers)select count(1) from t_pay_all where createdate >='2017-06-01' and createdate<'2017-07-01'; query plan ---------------------------------------------------------------------------------------------- finalize aggregate (cost=90543.96..90543.97 rows=1 width=8) (actual time=335.334..335.335 rows=1 loops=1) buffers: shared hit=351 read=66593 -> gather (cost=90543.74..90543.95 rows=2 width=8) (actual time=334.988..335.327 rows=3 loops=1) workers planned: 2 workers launched: 2 buffers: shared hit=351 read=66593 -> partial aggregate (cost=89543.74..89543.75 rows=1 width=8) (actual time=330.796..330.797 rows=1 loops=3) buffers: shared read=66578 -> parallel bitmap heap scan on t_pay_all (cost=19802.01..88689.92 rows=341528 width=0) (actual time=124.126..303.125 rows=274955 loops=3) recheck cond: ((createdate >= '2017-06-01'::date) and (createdate < '2017-07-01'::date)) heap blocks: exact=25882 buffers: shared read=66578 -> bitmap index scan on t_pay_all_createdate_idx (cost=0.00..19597.10 rows=819666 width=0) (actual time=111.233..111.233 rows=825865 loops=1) index cond: ((createdate >= '2017-06-01'::date) and (createdate < '2017-07-01'::date)) buffers: shared read=2877 planning time: 0.213 ms execution time: 344.013 ms (17 rows) time: 344.759 ms postgres=#
--分区表
postgres=# explain (analyze,buffers)select * from t_pay where createdate >='2017-06-01' and createdate<'2017-07-01'; query plan ------------------------------------------------------------------------------------------- append (cost=0.00..17633.97 rows=824865 width=20) (actual time=0.020..272.926 rows=824865 loops=1) buffers: shared hit=5261 -> seq scan on t_pay_201706 (cost=0.00..17633.97 rows=824865 width=20) (actual time=0.019..170.128 rows=824865 loops=1) filter: ((createdate >= '2017-06-01'::date) and (createdate < '2017-07-01'::date)) buffers: shared hit=5261 planning time: 0.779 ms execution time: 335.351 ms (7 rows) time: 336.676 ms postgres=# explain (analyze,buffers)select count(1) from t_pay where createdate >='2017-06-01' and createdate<'2017-07-01'; query plan -------------------------------------------------------------------------------------------- finalize aggregate (cost=12275.86..12275.87 rows=1 width=8) (actual time=144.023..144.023 rows=1 loops=1) buffers: shared hit=5429 -> gather (cost=12275.64..12275.85 rows=2 width=8) (actual time=143.966..144.016 rows=3 loops=1) workers planned: 2 workers launched: 2 buffers: shared hit=5429 -> partial aggregate (cost=11275.64..11275.65 rows=1 width=8) (actual time=140.230..140.230 rows=1 loops=3) buffers: shared hit=5261 -> append (cost=0.00..10416.41 rows=343694 width=0) (actual time=0.022..106.973 rows=274955 loops=3) buffers: shared hit=5261 -> parallel seq scan on t_pay_201706 (cost=0.00..10416.41 rows=343694 width=0) (actual time=0.020..68.952 rows=274955 loops=3) filter: ((createdate >= '2017-06-01'::date) and (createdate < '2017-07-01'::date)) buffers: shared hit=5261 planning time: 0.760 ms execution time: 145.289 ms (15 rows) time: 146.610 ms
在同一个分区内查询优势明显
2、不在同一个分区内
--非分区表
postgres=# explain (analyze,buffers)select count(1) from t_pay_all where createdate >='2017-06-01' and createdate<'2017-12-01'; query plan ------------------------------------------------------------------------------------------- finalize aggregate (cost=132593.42..132593.43 rows=1 width=8) (actual time=717.848..717.848 rows=1 loops=1) buffers: shared hit=33571 read=30446 dirtied=9508 written=4485 -> gather (cost=132593.20..132593.41 rows=2 width=8) (actual time=717.782..717.841 rows=3 loops=1) workers planned: 2 workers launched: 2 buffers: shared hit=33571 read=30446 dirtied=9508 written=4485 -> partial aggregate (cost=131593.20..131593.21 rows=1 width=8) (actual time=714.096..714.097 rows=1 loops=3) buffers: shared hit=33319 read=30446 dirtied=9508 written=4485 -> parallel seq scan on t_pay_all (cost=0.00..126330.64 rows=2105024 width=0) (actual time=0.059..545.016 rows=1675464 loops=3) filter: ((createdate >= '2017-06-01'::date) and (createdate < '2017-12-01'::date)) rows removed by filter: 1661203 buffers: shared hit=33319 read=30446 dirtied=9508 written=4485 planning time: 0.178 ms execution time: 721.822 ms (14 rows) time: 722.521 ms
--分区表
postgres=# explain (analyze,buffers)select count(1) from t_pay where createdate >='2017-06-01' and createdate<'2017-12-01'; query plan ------------------------------------------------------------------------------------------ finalize aggregate (cost=69675.98..69675.99 rows=1 width=8) (actual time=714.560..714.560 rows=1 loops=1) buffers: shared hit=27002 read=5251 -> gather (cost=69675.77..69675.98 rows=2 width=8) (actual time=714.426..714.551 rows=3 loops=1) workers planned: 2 workers launched: 2 buffers: shared hit=27002 read=5251 -> partial aggregate (cost=68675.77..68675.78 rows=1 width=8) (actual time=710.416..710.416 rows=1 loops=3) buffers: shared hit=26774 read=5251 -> append (cost=0.00..63439.94 rows=2094330 width=0) (actual time=0.023..536.033 rows=1675464 loops=3) buffers: shared hit=26774 read=5251 -> parallel seq scan on t_pay_201706 (cost=0.00..10416.41 rows=343694 width=0) (actual time=0.021..67.935 rows=274955 loops=3) filter: ((createdate >= '2017-06-01'::date) and (createdate < '2017-12-01'::date)) buffers: shared hit=5261 -> parallel seq scan on t_pay_201707 (cost=0.00..10728.06 rows=354204 width=0) (actual time=0.007..54.999 rows=283363 loops=3) filter: ((createdate >= '2017-06-01'::date) and (createdate < '2017-12-01'::date)) buffers: shared hit=5415 -> parallel seq scan on t_pay_201708 (cost=0.00..10744.08 rows=354738 width=0) (actual time=0.007..55.117 rows=283791 loops=3) filter: ((createdate >= '2017-06-01'::date) and (createdate < '2017-12-01'::date)) buffers: shared hit=5423 -> parallel seq scan on t_pay_201709 (cost=0.00..10410.71 rows=343714 width=0) (actual time=0.007..53.402 rows=274971 loops=3) filter: ((createdate >= '2017-06-01'::date) and (createdate < '2017-12-01'::date)) buffers: shared hit=5255 -> parallel seq scan on t_pay_201710 (cost=0.00..10737.41 rows=354494 width=0) (actual time=0.007..55.475 rows=283595 loops=3) filter: ((createdate >= '2017-06-01'::date) and (createdate < '2017-12-01'::date)) buffers: shared hit=5420 -> parallel seq scan on t_pay_201711 (cost=0.00..10403.29 rows=343486 width=0) (actual time=0.036..57.635 rows=274789 loops=3) filter: ((createdate >= '2017-06-01'::date) and (createdate < '2017-12-01'::date)) buffers: shared read=5251 planning time: 1.217 ms execution time: 718.372 ms (30 rows)
跨分区查询,大约在跨一半分区时性能相当。
查询某个月里某个用户数据--直接从cache里取数据
1、数据都落在所在分区,并且数据量极少
--非分区表
postgres=# explain (analyze,buffers) select * from t_pay_all where createdate>='2017-06-01' and createdate<'2017-07-01' and userid=268460; query plan -------------------------------------------------------------------------------------------- index scan using t_pay_all_userid_idx on t_pay_all (cost=0.43..48.68 rows=1 width=20) (actual time=0.053..0.071 rows=7 loops=1) index cond: (userid = 268460) filter: ((createdate >= '2017-06-01'::date) and (createdate < '2017-07-01'::date)) rows removed by filter: 10 buffers: shared hit=20 planning time: 0.149 ms execution time: 0.101 ms (7 rows) time: 0.676 ms
--分区表
postgres=# explain (analyze,buffers) select * from t_pay where createdate >='2017-06-01' and createdate<'2017-07-01' and userid=268460; query plan ------------------------------------------------------------------------------------------ append (cost=0.42..12.47 rows=2 width=20) (actual time=0.019..0.032 rows=7 loops=1) buffers: shared hit=10 -> index scan using t_pay_201706_userid_idx on t_pay_201706 (cost=0.42..12.47 rows=2 width=20) (actual time=0.018..0.029 rows=7 loops=1) index cond: (userid = 268460) filter: ((createdate >= '2017-06-01'::date) and (createdate < '2017-07-01'::date)) buffers: shared hit=10 planning time: 0.728 ms execution time: 0.064 ms (8 rows) time: 1.279 ms
在返回记录极少的情况下由于分布表的planning time开销较大,所以非分区表有优势
2、数据落在其它分区,并且数据量比较大
--非分区表
postgres=# explain (analyze,buffers) select * from t_pay_all where createdate >='2017-06-01' and createdate<'2017-07-01' and userid=302283 ; query plan --------------------------------------------------------------------------------------------- bitmap heap scan on t_pay_all (cost=19780.69..22301.97 rows=683 width=20) (actual time=91.778..91.803 rows=2 loops=1) recheck cond: ((userid = 302283) and (createdate >= '2017-06-01'::date) and (createdate < '2017-07-01'::date)) heap blocks: exact=9 buffers: shared hit=2927 -> bitmapand (cost=19780.69..19780.69 rows=683 width=0) (actual time=91.767..91.767 rows=0 loops=1) buffers: shared hit=2918 -> bitmap index scan on t_pay_all_userid_idx (cost=0.00..183.00 rows=8342 width=0) (actual time=0.916..0.916 rows=11013 loops=1) index cond: (userid = 302283) buffers: shared hit=41 -> bitmap index scan on t_pay_all_createdate_idx (cost=0.00..19597.10 rows=819666 width=0) (actual time=90.837..90.837 rows=825865 loops=1) index cond: ((createdate >= '2017-06-01'::date) and (createdate < '2017-07-01'::date)) buffers: shared hit=2877 planning time: 0.172 ms execution time: 91.851 ms (14 rows) time: 92.534 ms
--分区表
postgres=# explain (analyze,buffers) select * from t_pay where createdate >='2017-06-01' and createdate<'2017-07-01' and userid=302283 ; query plan ------------------------------------------------------------------------------------------- append (cost=0.42..12.47 rows=2 width=20) (actual time=0.042..0.046 rows=2 loops=1) buffers: shared hit=7 -> index scan using t_pay_201706_userid_idx on t_pay_201706 (cost=0.42..12.47 rows=2 width=20) (actual time=0.041..0.045 rows=2 loops=1) index cond: (userid = 302283) filter: ((createdate >= '2017-06-01'::date) and (createdate < '2017-07-01'::date)) buffers: shared hit=7 planning time: 0.818 ms execution time: 0.096 ms (8 rows) time: 1.499 ms
这是分区表最大的优势体现了,性能提升不是一般的大
索引维护
--非分区表
postgres=# reindex index t_pay_all_createdate_idx; reindex time: 11827.344 ms (00:11.827)
--分区表
postgres=# reindex index t_pay_201706_createdate_idx; reindex time: 930.439 ms postgres=#
这个也是分区表的优势,可以针对某个分区的索引进行重建。
删除整个分区数据
--非分区表
postgres=# delete from t_pay_all where createdate >='2017-06-01' and createdate<'2017-07-01'; delete 824865 time: 5775.545 ms (00:05.776)
--分区表
postgres=# truncate table t_pay_201706; truncate table time: 177.809 ms
个也是分区表的优势,可以对某个分区直接truncate
全表扫描
--非分区表
postgres=# explain analyze select count(1) from t_pay; query plan --------------------------------------------------------------------------------------------- finalize aggregate (cost=107370.96..107370.97 rows=1 width=8) (actual time=971.561..971.561 rows=1 loops=1) -> gather (cost=107370.75..107370.96 rows=2 width=8) (actual time=971.469..971.555 rows=3 loops=1) workers planned: 2 workers launched: 2 -> partial aggregate (cost=106370.75..106370.76 rows=1 width=8) (actual time=967.378..967.378 rows=1 loops=3) -> append (cost=0.00..96800.40 rows=3828141 width=0) (actual time=0.019..698.882 rows=3061712 loops=3) -> parallel seq scan on t_pay_201701 (cost=0.00..8836.14 rows=349414 width=0) (actual time=0.017..48.716 rows=279531 loops=3) -> parallel seq scan on t_pay_201702 (cost=0.00..8119.94 rows=321094 width=0) (actual time=0.007..33.072 rows=256875 loops=3) -> parallel seq scan on t_pay_201703 (cost=0.00..9079.47 rows=359047 width=0) (actual time=0.006..37.153 rows=287238 loops=3) -> parallel seq scan on t_pay_201704 (cost=0.00..8672.67 rows=342968 width=0) (actual time=0.006..35.317 rows=274374 loops=3) -> parallel seq scan on t_pay_201705 (cost=0.00..8975.23 rows=354923 width=0) (actual time=0.006..36.571 rows=283938 loops=3) -> parallel seq scan on t_pay_201706 (cost=0.00..20.00 rows=1000 width=0) (actual time=0.000..0.000 rows=0 loops=3) -> parallel seq scan on t_pay_201707 (cost=0.00..8957.04 rows=354204 width=0) (actual time=0.006..36.393 rows=283363 loops=3) -> parallel seq scan on t_pay_201708 (cost=0.00..8970.38 rows=354738 width=0) (actual time=0.006..37.015 rows=283791 loops=3) -> parallel seq scan on t_pay_201709 (cost=0.00..8692.14 rows=343714 width=0) (actual time=0.006..35.187 rows=274971 loops=3) -> parallel seq scan on t_pay_201710 (cost=0.00..8964.94 rows=354494 width=0) (actual time=0.006..36.566 rows=283595 loops=3) -> parallel seq scan on t_pay_201711 (cost=0.00..8685.86 rows=343486 width=0) (actual time=0.006..35.198 rows=274789 loops=3) -> parallel seq scan on t_pay_201712 (cost=0.00..8826.59 rows=349059 width=0) (actual time=0.006..36.523 rows=279247 loops=3) planning time: 0.706 ms execution time: 977.364 ms (20 rows) time: 978.705 ms postgres=#
--分区表
postgres=# explain analyze select count(1) from t_pay_all; query plan ------------------------------------------------------------------------------------------------- finalize aggregate (cost=116900.63..116900.64 rows=1 width=8) (actual time=644.093..644.093 rows=1 loops=1) -> gather (cost=116900.42..116900.63 rows=2 width=8) (actual time=644.035..644.087 rows=3 loops=1) workers planned: 2 workers launched: 2 -> partial aggregate (cost=115900.42..115900.43 rows=1 width=8) (actual time=640.587..640.587 rows=1 loops=3) -> parallel seq scan on t_pay_all (cost=0.00..105473.33 rows=4170833 width=0) (actual time=0.344..371.965 rows=3061712 loops=3) planning time: 0.164 ms execution time: 645.438 ms (8 rows) time: 646.027 ms
全扫描时分区表落后,但还基本上能接收。
增加新的分区并导入数据
--生成新的分区数据
copy (select userid,pay_money,createdate+31 as createdate from t_pay_201712) to '/home/pg/201801.txt';
--建立新的分区
create table t_pay_201801 partition of t_pay(id primary key,userid,pay_money,createdate) for values from ('2018-01-01') to ('2018-02-01'); create index t_pay_201801_createdate_idx on t_pay_201801 using btree(createdate); create index t_pay_201801_userid_idx on t_pay_201801 using btree(userid);
--非分区表
postgres=# copy t_pay_all(userid,pay_money,createdate) from '/home/pg/201801.txt'; copy 837741 time: 18105.024 ms (00:18.105)
--分区表
postgres=# copy t_pay(userid,pay_money,createdate) from '/home/pg/201801.txt'; copy 837741 time: 13864.950 ms (00:13.865) postgres=#
新的分区数据导入保持优势
到此这篇关于postgresql 10分区表详解及性能测试报告的文章就介绍到这了,更多相关postgresql 10分区表性能测试内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!