宝哥软件园

PostgreSQL查看数据库,索引,表,表空间大小实例代码

编辑:宝哥软件园 来源:互联网 时间:2021-08-02

类型:数据库类大小:3.5M语言:英文评分:5.0标签:立即下载一种数据库系统提供了多个系统管理函数来查看表,索引,表空间及数据库的大小,下面详细介绍一下。

一、数据库对象尺寸函数

函数名返回类型描述pg_column_size(任意)int存储一个指定的数值需要的字节数(可能压缩过)pg_database_size(oid)bigint指定似…的的数据库使用的磁盘空间pg_database_size(名称)bigint指定名称的数据库使用的磁盘空间pg _ indexes _ size(reg类)大整数关联指定表似…的或表名的表索引的使用总磁盘空间pg _ relationship _ size(关系regclass,分叉文本)bigint指定似…的或名的表或索引,通过指定分叉(“主”、“fsm”或虚拟机')所使用的磁盘空间pg _ relationship _ size(关系reg class)bigintpg _ relationship _ size(."主")的缩写pg _ size _蛮(bigint)文本将表示为64位整数的字节大小转换为人类可读的格式,其中包含大小单位spg _ size _蛮(数字)文本把以字节计算的数值转换成一个人类易读的尺寸单位pg_table_size(regclass)bigint指定表似…的或表名的表使用的磁盘空间,除去索引(但是包含TOAST,自由空间映射和可视映射)pg_tablespace_size(oid)bigint指定似…的的表空间使用的磁盘空间pg_tablespace_size(名称)bigint指定名称的表空间使用的磁盘空间pg _ total _ relation _ size(reg类)大整数指定表似…的或表名使用的总磁盘空间,包括所有索引和烤数据二、实例讲解

2.1 查看存储一个指定的数值需要的字节数

David=# select pg _ column _ size(1);

pg_column_size - 4(1行)David=# select pg _ column _ size(10000);pg_column_size - 4(1行)David=# select pg _ column _ size(' David ');pg_column_size - 6(1行)David=# select pg _ column _ size(' hello,world ');pg_column_size - 12(1行)David=# select pg _ column _ size(' 2013-04-18 15:17:21.622885 08 ');pg_column_size - 30(1行)david=# select pg_column_size('中国');pg_column_size - 7(1行)大卫=#

2.2 查看数据库大小

查看原始数据

大卫=# d测试

表“public.test”列|类型|修饰符----id |整数|名称|字符变量(20) |性别|布尔|加入_日期|日期|部门|字符(4) |索引:“idx _ join _ date _ test”b树(join _ date)“idx _ test”b树(id)David=#从测试中选择计数(1);计数- 1835008(1行)大卫=#

查看大卫数据库大小

David=# select pg _ database _ size(' David ');

pg_database_size - 190534776(1行)大卫=#

查看所有数据库大小

大卫=#从pg _数据库中选择pg_database.datname,pg _ database _ size(pg _ database。datname)AS大小;

datname | size-template 0 | 6513156 postgres | 6657144 JBoss | 6521348 bug | 6521348 David | 190534776 BMCV3 | 28147135608 mydb | 10990712 template 1 | 6521348(8行)大卫=#

这样查出来的结果,看上去太长了,不太容易读数。

2.3 以人性化的方式显示大小

david=# select pg _ size _蛮(pg _ database _ size(' David ');

pg _ size _挺- 182兆字节(1行)大卫=#

2.4 查看单索引大小

David=# select pg _ relation _ size(' idx _ test ');

pg _ relationship _ size-41238528(1行)david=# select pg _ size _蛮(pg _ relationship _ size(' idx _ test ');pg _ size _挺- 39兆字节(1行)大卫=#

david=# select pg _ size _蛮(pg _ relation _ size(' idx _ join _ date _ test ');

pg _ size _挺- 39兆字节(1行)大卫=#

2.5 查看指定表中所有索引大小

David=# select pg _ indexes _ size(' test ');

pg_indexes_size - 82477056(1行)david=# select pg _ size _蛮(pg _ indexes _ size(' test '));pg _ size _挺- 79兆字节(1行)大卫=#

idx_test和idx_join_date_test两个索引大小加起来差不多等于上面pg_indexes_size()查询出来的索引大小。

2.6 查看指定计划里所有的索引大小,按从大到小的顺序排列。

大卫=#选择*从pg _ namespace

nspname | NSP所有者| NSP ACL-pg _ toast | 10 | pg _ temp _ 1 | 10 | pg _ toast _ temp _ 1 | 10 | pg _ catalog | 10 | { postgres=UC/postgres,=U/postgres } information _ schema | 10 | { postgres=UC/postgres,=U/postgres } public | 10 | { postgres=UC/postgres,=UC/postgres}(6行)David=# select indexrelname indexrelname | pg _ size _蛮-idx _ join _ date _ test | 91mb idx _ test | 91mb test table _ idx | 1424 kB city _ pkey | 256 kB city 11 | 256 kB country language _ pkey | 56kb sale _ pkey | 8192字节track_pkey | 8192字节TBL _分区_ 201211 _加入日期| 8192字节TBL _分区_ 201212 _加入日期| 8192字节

2.7 查看指定表大小

David=# select pg _ relation _ size(' test ');

pg _ relationship _ size-95748096(第一行)david=# select pg _ size _挺(pg _ relationship _ size(' test '));pg _ size _挺- 91兆字节(1行)大卫=#

使用pg_table_size()函数查看

David=# select pg _ table _ size(' test ');

pg_table_size - 95789056(1行)david=# select pg _ size _蛮(pg _ table _ size(' test '));pg _ size _挺- 91兆字节(1行)大卫=#

2.8 查看指定表的总大小

David=# select pg _ total _ relation _ size(' test ');

pg _ total _ relation _ size-178266112(1行)david=# select pg _ size _挺(pg _ total _ relationship _ size(' test '));pg _ size _挺- 170兆字节(1行)大卫=#

2.9 查看指定计划里所有的表大小,按从大到小的顺序排列。

大卫=#从pg_stat_user_tables中选择relname,pg _ size _挺(pg _ relation _ size(relid)),其中schemaname=' public ' order by pg _ relation _ size(relid)desc;

relname | pg _ size _蛮-test | 91mb测试表| 1424 kB城市| 256 kB国家语言| 56kb国家| 40kb测试计数| 8192字节tbl_partition_201302 | 8192字节tbl_partition_201303 | 8192字节人| 8192字节客户| 8192字节美国州| 8192字节tbl_david | 8192字节emp | 8192字节TBL分区8192字节tbl_xulie | 8192字节tbl_partition_201301 | 8192字节出售| 8192字节项目| 8192字节曲目| 8192字节tbl_partition_201306 | 0字节tbl_partition_201305 | 0字节人员2 | 0字节(28行)大卫=#

2.10 查看表空间大小

大卫=#从pg _表空间中选择spcname

spcname - pg_default pg_global(2行)David=# select pg _ table space _ size(' pg _ default ');pg _表空间_大小-28381579760(1行)david=# select pg _ size _挺(pg _ table space _ size(' pg _ default ');pg _ size _挺- 26 GB(1行)大卫=#

另一种查看方法:

大卫=#选择pg _ table space _ SIZE(' pg _ default ')/1024/1024作为尺寸M ';

尺寸M - 27066(1行)大卫=#选择pg _ table space _ SIZE(' pg _ default ')/1024/1024/1024作为尺寸G’;尺寸G - 26(1排)大卫=#

更多资讯
游戏推荐
更多+