1、空间查询
(1)数据库占用空间查询
select pg_size_pretty(pg_database_size('db_data'));
(2)模式占用空间查询
select pg_size_pretty(sum(pg_relation_size(relid))) from pg_stat_user_tables where schemaname in ('stg');
(3)数据库中单个表的大小(不包含索引)
select pg_size_pretty(pg_relation_size('表名'));
(4)查出所有表(包含索引)并排序
(5)查出表大小按大小排序并分离data与index
SELECT table_name, pg_size_pretty(table_size) AS table_size, pg_size_pretty(indexes_size) AS indexes_size, pg_size_pretty(total_size) AS total_size FROM ( SELECT table_name, pg_table_size(table_name) AS table_size, pg_indexes_size(table_name) AS indexes_size, pg_total_relation_size(table_name) AS total_size FROM ( SELECT ('"' || table_schema || '"."' || table_name || '"') AS table_name FROM information_schema.tables ) AS all_tables ORDER BY total_size DESC ) AS pretty_sizes
(6)查询数据库db_lzhj存储容量
select pg_size_pretty(PG_DataBase_size('db_lzhj'));
2、数据表查询
(1)查询表信息
select * from pg_tables limit 20;
(2)查询分区信息
select * from user_tab_partitions limit 20;
(3)查询指定模式分区表
select schemaname,tablename,tableowner from pg_tables where schemaname='stg' and tableowner='owner_name'
and table_name in (select tablename from user_tab_partitions where schema='stg' and table_owner='owner_name')
3、表空间回收
-
#查询表脏页率大于10%,记录数大于0,schema='stg'的表 select * from pgxc_get_stat_dirty_tables(10,0,'stg') Vacuum tableName:不能立即回收。 Vacuum Analyze tableName:执行完成不立即回收。在大量增删改后,执行后可优化查询。建议经常清理(至少每晚一次)。 Vacuum Full tableName:执行完成立即回收。Full不清理索引,推荐周期性运行ReIndex命令。官方建议执行顺序:删除索引--=>Vacuum Full=>创建索引。 Vacuum Full Analy tableName:执行完成立即回收。
注意:之前数据不能立即回收,是vacuum_defer_cleanup_age,参数为初始化参数。
参数说明:指定VACUUM使用的事务的数目,VACUUM可以使用这些事务延缓清除无效的记录。
该参数属于SIGHUP类型参数,请参考表1中对应设置方法进行设置。
取值范围:整型,0~1000000
默认值:8000,表示无效记录被立即清除。
4、创建索引
对Libra表创建索引对于表查询效率提升非常明显,分区表创建索引需要再最后加入local关键字。创建索引时模型表空间为pg_default,可以根据需要指定表空间。
(1)分区表创建索引
create index idx_user_idcard on user(idcard) local;
(2)普通区表创建索引
create index idx_user_idcard on user(idcard);