1.创建分区表,并用generate_series(1,N)函数对表插入数据
omm=# create table tom
(
id int,
c2 CHAR(2)
)
partition by range (id)
(
partition tom_p0 values less than (1000),
partition tom_p1 values less than (2000),
partition tom_p2 values less than (3000),
omm-# partition tom_p3 values less than (4000),
partition tom_p4 values less than (500000)
);
CREATE TABLE
omm=# insert into tom values(generate_series(10, 100000));
INSERT 0 99991
2.收集表统计信息
omm=# select relname, relpages, reltuples from pg_class where relname = 'tom';
relname | relpages | reltuples
---------+----------+-----------
tom | 0 | 0
(1 row)
omm=# analyze VERBOSE tom;
INFO: analyzing "public.tom"(gaussdb pid=1)
INFO: ANALYZE INFO : "tom": scanned 5 of 5 pages, containing 990 live rows and 0 dead rows; 338 rows in sample, 990 estimated total rows(gaussdb pid=1)
INFO: ANALYZE INFO : "tom": scanned 5 of 5 pages, containing 1000 live rows and 0 dead rows; 338 rows in sample, 1000 estimated total rows(gaussdb pid=1)
INFO: ANALYZE INFO : "tom": scanned 5 of 5 pages, containing 1000 live rows and 0 dead rows; 338 rows in sample, 1000 estimated total rows(gaussdb pid=1)
INFO: ANALYZE INFO : "tom": scanned 5 of 5 pages, containing 1000 live rows and 0 dead rows; 338 rows in sample, 1000 estimated total rows(gaussdb pid=1)
INFO: ANALYZE INFO : "tom": scanned 425 of 425 pages, containing 96001 live rows and 0 dead rows; 28648 rows in sample, 96001 estimated total rows(gaussdb pid=1)
ANALYZE
omm=# select relname, relpages, reltuples from pg_class where relname = 'tom';
relname | relpages | reltuples
---------+----------+-----------
tom | 445 | 99991
(1 row)
3.显示简单查询的执行计划;建立索引并显示有索引条件的执行计划
omm=# explain select * from tom where id=1000;
QUERY PLAN
-------------------------------------------------------------------------
Partition Iterator (cost=0.00..1338.89 rows=1 width=16)
Iterations: 1
-> Partitioned Seq Scan on tom (cost=0.00..1338.89 rows=1 width=16)
Filter: (id = 1000)
Selected Partitions: 2
(5 rows)
omm=# create index inx_id on tom(id);
CREATE INDEX
omm=# explain select * from tom where id=1000;
QUERY PLAN
-------------------------------------------------------------------
Index Scan using inx_id on tom (cost=0.00..8.27 rows=1 width=16)
Index Cond: (id = 1000)
(2 rows)
4.更新表数据,并做垃圾收集
omm=# update tom set id=id+200 ;
UPDATE 99991
omm=# vacuum (verbose,analyze) tom;
INFO: vacuuming "public.tom"(gaussdb pid=1)
INFO: index "inx_id" now contains 1780 row versions in 827 pages(gaussdb pid=1)
DETAIL: 0 index row versions were removed.
0 index pages have been deleted, 0 are currently reusable.
CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: "tom": found 0 removable, 1780 nonremovable row versions in 8 out of 8 pages(gaussdb pid=1)
DETAIL: 990 dead row versions cannot be removed yet.
There were 0 unused item pointers.
0 pages are entirely empty.
CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: vacuuming "public.tom"(gaussdb pid=1)
INFO: index "inx_id" now contains 2000 row versions in 827 pages(gaussdb pid=1)
DETAIL: 0 index row versions were removed.
0 index pages have been deleted, 0 are currently reusable.
CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: "tom": found 0 removable, 2000 nonremovable row versions in 9 out of 9 pages(gaussdb pid=1)
DETAIL: 1000 dead row versions cannot be removed yet.
There were 0 unused item pointers.
0 pages are entirely empty.
CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: vacuuming "public.tom"(gaussdb pid=1)
INFO: index "inx_id" now contains 2000 row versions in 827 pages(gaussdb pid=1)
DETAIL: 0 index row versions were removed.
0 index pages have been deleted, 0 are currently reusable.
CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: "tom": found 0 removable, 2000 nonremovable row versions in 9 out of 9 pages(gaussdb pid=1)
DETAIL: 1000 dead row versions cannot be removed yet.
There were 0 unused item pointers.
0 pages are entirely empty.
CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: vacuuming "public.tom"(gaussdb pid=1)
INFO: index "inx_id" now contains 2000 row versions in 827 pages(gaussdb pid=1)
DETAIL: 0 index row versions were removed.
0 index pages have been deleted, 0 are currently reusable.
CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: "tom": found 0 removable, 2000 nonremovable row versions in 9 out of 9 pages(gaussdb pid=1)
DETAIL: 1000 dead row versions cannot be removed yet.
There were 0 unused item pointers.
0 pages are entirely empty.
CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: vacuuming "public.tom"(gaussdb pid=1)
INFO: index "inx_id" now contains 192202 row versions in 827 pages(gaussdb pid=1)
DETAIL: 0 index row versions were removed.
0 index pages have been deleted, 0 are currently reusable.
CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: "tom": found 0 removable, 192202 nonremovable row versions in 851 out of 851 pages(gaussdb pid=1)
DETAIL: 96001 dead row versions cannot be removed yet.
There were 0 unused item pointers.
0 pages are entirely empty.
CPU 0.00s/0.02u sec elapsed 0.03 sec.
INFO: scanned index "inx_id" to remove 0.000000 invisible rows(gaussdb pid=1)
DETAIL: CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: analyzing "public.tom"(gaussdb pid=1)
INFO: ANALYZE INFO : "tom": scanned 8 of 8 pages, containing 790 live rows and 990 dead rows; 271 rows in sample, 790 estimated total rows(gaussdb pid=1)
INFO: ANALYZE INFO : "tom": scanned 9 of 9 pages, containing 1000 live rows and 1000 dead rows; 305 rows in sample, 1000 estimated total rows(gaussdb pid=1)
INFO: ANALYZE INFO : "tom": scanned 9 of 9 pages, containing 1000 live rows and 1000 dead rows; 305 rows in sample, 1000 estimated total rows(gaussdb pid=1)
INFO: ANALYZE INFO : "tom": scanned 9 of 9 pages, containing 1000 live rows and 1000 dead rows; 305 rows in sample, 1000 estimated total rows(gaussdb pid=1)
INFO: ANALYZE INFO : "tom": scanned 851 of 851 pages, containing 96201 live rows and 96001 dead rows; 28814 rows in sample, 96201 estimated total rows(gaussdb pid=1)
VACUUM
5.清理数据
omm=# drop table tom;
DROP TABLE
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




