1.创建分区表,并用generate_series(1,N)函数对表插入数据
create table store
(id int,name varchar(20),addr varchar(50))
partition by range (id)
(
partition store_p0 values less than (1000),
partition store_p1 values less than (2000),
partition store_p2 values less than (3000),
partition store_p3 values less than (4000),
partition store_p4 values less than (5000)
);
insert into store values(generate_series(1, 4999),'tests111','aaaaa');
2.收集表统计信息
select relname, relpages, reltuples from pg_class where relname = 'store';
analyze VERBOSE store;
select relname, relpages, reltuples from pg_class where relname = 'store';
3.显示简单查询的执行计划;建立索引并显示有索引条件的执行计划
--使用默认的打印格式
SET explain_perf_mode=normal;
--显示表简单查询的执行计划
EXPLAIN SELECT * FROM store;
--以JSON格式输出的执行计划(explain_perf_mode为normal时)
EXPLAIN(FORMAT JSON) SELECT * FROM store;
--禁止开销估计的执行计划
EXPLAIN(COSTS FALSE)SELECT * FROM store;
--带有聚集函数查询的执行计划
EXPLAIN SELECT SUM(id) FROM store WHERE id<10;
--有索引条件的执行计划
CREATE INDEX store_index1 ON store(id) LOCAL;
EXPLAIN SELECT * FROM store WHERE id<100;
4.更新表数据,并做垃圾收集
update store set name = name || id where id <100;
VACUUM (VERBOSE, ANALYZE) store;
5.清理数据
drop table store;