为啥用户表的大小远小于 PostgreSQL 数据库中其他对象的大小

Posted

技术标签:

【中文标题】为啥用户表的大小远小于 PostgreSQL 数据库中其他对象的大小【英文标题】:Why the size of user tables is much less that the size of other objects in a PostgreSQL database为什么用户表的大小远小于 PostgreSQL 数据库中其他对象的大小 【发布时间】:2016-10-10 16:07:52 【问题描述】:

我需要帮助来了解我的数据库的大小。我想这个问题很友好 对数据库专业人士来说很简单。我不是专业人士,但我想了解。我用 PostgreSQL。还有一个细节。我的应用程序是用 java 编写并部署在 PWS 中 试用帐户中的云。最大可用数据库大小为 20MB。这足以 测试和开发。现在经过几个月的开发,我的数据库 大小超过了限制,变成了大约 25MB。但从用户的 透视(实际上是从我的角度来看)我的数据大小要小得多 超过 25MB。所以我想知道是什么占用了空间。

所以,这里是数据库结构:

# \d
                 List of relations
 Schema |        Name        |   Type    
--------+--------------------+----------
 public | hibernate_sequence | sequence 
 public | post               | table    
 public | site               | table    
 public | user_account       | table    
 public | user_authority     | table    
 public | userconnection     | table    
(6 rows)


# \d post
                   Table "public.post"
     Column     |            Type             | Modifiers 
----------------+-----------------------------+-----------
 id             | bigint                      | not null
 author         | character varying(255)      | 
 content        | text                        | not null
 creation_time  | timestamp without time zone | not null
 tags           | character varying(255)      | 
 title          | text                        | not null
 url            | character varying(255)      | not null
 site_id        | bigint                      | not null
 published_date | timestamp without time zone | not null
 uri            | character varying(255)      | not null
Indexes:
    "post_pkey" PRIMARY KEY, btree (id)
Foreign-key constraints:
    "fkadd3gkkmgwsi1dy825qkfd62e" FOREIGN KEY (site_id) REFERENCES site(id)


# \d site 
                  Table "public.site"
    Column    |            Type             | Modifiers 
--------------+-----------------------------+-----------
 id           | bigint                      | not null
 last_visited | timestamp without time zone | 
 title        | text                        | 
 url          | character varying(255)      | not null
 description  | text                        | 
Indexes:
    "site_pkey" PRIMARY KEY, btree (id)
Referenced by:
    TABLE "post" CONSTRAINT "fkadd3gkkmgwsi1dy825qkfd62e" FOREIGN KEY (site_id) REFERENCES site(id)


# \d user_account
                 Table "public.user_account"
      Column       |            Type             | Modifiers 
-------------------+-----------------------------+-----------
 id                | bigint                      | not null
 email             | character varying(255)      | not null
 first_name        | character varying(255)      | 
 last_name         | character varying(255)      | 
 last_request_time | timestamp without time zone | not null
 password          | character varying(255)      | not null
 registration_time | timestamp without time zone | not null
Indexes:
    "user_account_pkey" PRIMARY KEY, btree (id)
    "uk_hl02wv5hym99ys465woijmfib" UNIQUE CONSTRAINT, btree (email)
Referenced by:
    TABLE "user_authority" CONSTRAINT "fkn48a3n0mb8d8njshjf75kgsv" FOREIGN KEY (user_id) REFERENCES user_account(id)


# \d userconnection
            Table "public.userconnection"
     Column     |          Type          | Modifiers 
----------------+------------------------+-----------
 userid         | character varying(255) | not null
 providerid     | character varying(255) | not null
 provideruserid | character varying(255) | not null
 rank           | integer                | not null
 displayname    | character varying(255) | 
 profileurl     | character varying(512) | 
 imageurl       | character varying(512) | 
 accesstoken    | character varying(512) | not null
 secret         | character varying(512) | 
 refreshtoken   | character varying(512) | 
 expiretime     | bigint                 | 
Indexes:
    "userconnection_pkey" PRIMARY KEY, btree (userid, providerid, provideruserid)
    "userconnectionrank" UNIQUE, btree (userid, providerid, rank)


# \d user_authority
         Table "public.user_authority"
  Column   |          Type          | Modifiers 
-----------+------------------------+-----------
 authority | character varying(255) | not null
 user_id   | bigint                 | not null
Indexes:
    "user_authority_pkey" PRIMARY KEY, btree (user_id, authority)
Foreign-key constraints:
    "fkn48a3n0mb8d8njshjf75kgsv" FOREIGN KEY (user_id) REFERENCES user_account(id)


Here's the databases's size:

    select pg_database_size('rodiahug');

    pg_database_size
    25714860

这是我用来确定表大小的查询:

SELECT
   relname as "Table",
   pg_size_pretty(pg_total_relation_size(relid)) As "Size",
   pg_size_pretty(pg_total_relation_size(relid) - pg_relation_size(relid)) as "External Size"
   FROM pg_catalog.pg_statio_user_tables ORDER BY pg_total_relation_size(relid) DESC;


Table                 Size              External Size
site                  64 kB             56 kB
user_account          48 kB             40 kB
post                  48 kB             24 kB
userconnection        48 kB             40 kB
user_authority        24 kB             16 kB

正如我们所见,添加所有大小远小于 24MB。

天真的谷歌搜索对象大小的查询给出了查询:

SELECT
   relname AS objectname,
   relkind AS objecttype,
   reltuples AS "#entries", pg_size_pretty(relpages::bigint*8*1024) AS size
   FROM pg_class
   WHERE relpages >= 1 AND reltuples > 0
   ORDER BY relpages DESC;

objectname                          objecttype  #entries    size
pg_largeobject                      r           129767.0    6152 kB
pg_largeobject_metadata             r           131790.0    5704 kB
pg_shdepend_depender_index          i           71390.0     4368 kB
pg_shdepend                         r           71390.0     4296 kB
pg_largeobject_metadata_oid_index   i           131790.0    2912 kB
pg_largeobject_loid_pn_index        i           129767.0    2872 kB
pg_shdepend_reference_index         i           71390.0     2576 kB
pg_database                         r           5048.0      1496 kB
pg_proc                             r           3473.0      712 kB
pg_depend                           r           11312.0     672 kB
pg_authid                           r           2921.0      448 kB
pg_attribute                        r           2573.0      376 kB
pg_depend_depender_index            i           11312.0     368 kB
pg_depend_reference_index           i           11312.0     368 kB
pg_description                      r           3876.0      272 kB
pg_proc_proname_args_nsp_index      i           3473.0      264 kB
pg_database_datname_index           i           4214.0      176 kB
pg_authid_rolname_index             i           2901.0      168 kB
pg_statistic                        r           422.0       144 kB
pg_operator                         r           907.0       136 kB
pg_attribute_relid_attnam_index     i           2573.0      136 kB
pg_database_oid_index               i           4214.0      136 kB
pg_description_o_c_o_index          i           3876.0      136 kB
pg_rewrite                          r           115.0       96 kB
pg_proc_oid_index                   i           3473.0      96 kB
pg_attribute_relid_attnum_index     i           2573.0      96 kB
pg_authid_oid_index                 i           2901.0      88 kB
pg_type                             r           416.0       80 kB
pg_collation                        r           303.0       80 kB
pg_amop                             r           1025.0      72 kB
pg_class                            r           337.0       72 kB
pg_amproc                           r           904.0       56 kB
pg_class_relname_nsp_index          i           337.0       56 kB
pg_amproc_fam_proc_index            i           904.0       48 kB
pg_operator_oprname_l_r_n_index     i           907.0       48 kB
pg_amop_opr_fam_index               i           1025.0      48 kB
pg_type_typname_nsp_index           i           416.0       48 kB
pg_amop_fam_strat_index             i           1025.0      48 kB
pg_class_tblspc_relfilenode_index   i           337.0       40 kB
pg_amop_oid_index                   i           1025.0      40 kB
pg_operator_oid_index               i           907.0       40 kB
pg_amproc_oid_index                 i           904.0       40 kB
pg_class_oid_index                  i           337.0       40 kB
pg_ts_config_map_index              i           304.0       32 kB
pg_statistic_relid_att_inh_index    i           422.0       32 kB
pg_collation_name_enc_nsp_index     i           303.0       32 kB
pg_type_oid_index                   i           416.0       32 kB
pg_opclass_am_name_nsp_index        i           219.0       32 kB
pg_opclass                          r           219.0       32 kB
pg_conversion                       r           132.0       24 kB
post                                r           67.0        24 kB
pg_toast_2619                       t           15.0        24 kB
pg_index                            r           131.0       24 kB
pg_opfamily                         r           172.0       24 kB
pg_rewrite_oid_index                i           115.0       16 kB
pg_rewrite_rel_rulename_index       i           115.0       16 kB
pg_trigger_tgconstraint_index       i           8.0         16 kB
pg_trigger_tgrelid_tgname_index     i           8.0         16 kB
pg_namespace_nspname_index          i           6.0         16 kB
pg_namespace_oid_index              i           6.0         16 kB
pg_ts_config_cfgname_index          i           16.0        16 kB
pg_ts_config_oid_index              i           16.0        16 kB
pg_ts_dict_dictname_index           i           19.0        16 kB
pg_ts_dict_oid_index                i           19.0        16 kB
pg_ts_parser_prsname_index          i           1.0         16 kB
pg_ts_parser_oid_index              i           1.0         16 kB
pg_ts_template_tmplname_index       i           8.0         16 kB
pg_ts_template_oid_index            i           8.0         16 kB
pg_foreign_data_wrapper_oid_index   i           1.0         16 kB
pg_foreign_data_wrapper_name_index  i           1.0         16 kB
pg_extension_oid_index              i           24.0        16 kB
pg_range_rngtypid_index             i           6.0         16 kB
site_pkey                           i           3.0         16 kB
userconnectionrank                  i           4.0         16 kB
userconnection_pkey                 i           4.0         16 kB
pg_index_indexrelid_index           i           131.0       16 kB
pg_am_oid_index                     i           6.0         16 kB
pg_aggregate_fnoid_index            i           140.0       16 kB
pg_index_indrelid_index             i           131.0       16 kB
pg_cast_oid_index                   i           224.0       16 kB
pg_cast_source_target_index         i           224.0       16 kB
pg_conversion_default_index         i           132.0       16 kB
pg_conversion_name_nsp_index        i           132.0       16 kB
pg_conversion_oid_index             i           132.0       16 kB
pg_constraint_contypid_index        i           13.0        16 kB
pg_constraint_conrelid_index        i           13.0        16 kB
pg_collation_oid_index              i           303.0       16 kB
pg_pltemplate_name_index            i           8.0         16 kB
pg_constraint_conname_nsp_index     i           13.0        16 kB
pg_shdescription_o_c_index          i           1.0         16 kB
pg_tablespace_oid_index             i           2.0         16 kB
pg_tablespace_spcname_index         i           2.0         16 kB
pg_toast_2619_index                 i           15.0        16 kB
user_authority_pkey                 i           5.0         16 kB
pg_opfamily_am_name_nsp_index       i           172.0       16 kB
pg_opfamily_oid_index               i           172.0       16 kB
pg_opclass_oid_index                i           219.0       16 kB
pg_language_name_index              i           5.0         16 kB
pg_language_oid_index               i           5.0         16 kB
user_account_pkey                   i           3.0         16 kB
uk_hl02wv5hym99ys465woijmfib        i           3.0         16 kB
pg_cast                             r           224.0       16 kB
pg_ts_config_map                    r           304.0       16 kB
pg_extension_name_index             i           24.0        16 kB
post_pkey                           i           67.0        16 kB
pg_constraint_oid_index             i           13.0        16 kB
pg_aggregate                        r           140.0       16 kB
pg_trigger_oid_index                i           8.0         16 kB
pg_am_name_index                    i           6.0         16 kB
pg_ts_config                        r           16.0        8192 bytes
pg_am                               r           6.0         8192 bytes
pg_ts_dict                          r           19.0        8192 bytes
pg_ts_parser                        r           1.0         8192 bytes
pg_ts_template                      r           8.0         8192 bytes
pg_extension                        r           24.0        8192 bytes
pg_foreign_data_wrapper             r           1.0         8192 bytes
user_account                        r           3.0         8192 bytes
pg_range                            r           6.0         8192 bytes
pg_constraint                       r           13.0        8192 bytes
pg_tablespace                       r           2.0         8192 bytes
user_authority                      r           5.0         8192 bytes
pg_shdescription                    r           1.0         8192 bytes
site                                r           3.0         8192 bytes
pg_trigger                          r           8.0         8192 bytes
userconnection                      r           4.0         8192 bytes
pg_language                         r           5.0         8192 bytes
pg_namespace                        r           6.0         8192 bytes
pg_pltemplate                       r           8.0         8192 bytes
hibernate_sequence                  S           1.0         8192 bytes

在这里,我想了解为什么这些物体占用空间更多 比我的用户表? 例如,这些:

pg_largeobject                      r           129767.0    6152 kB
pg_largeobject_metadata             r           131790.0    5704 kB
pg_shdepend_depender_index          i           71390.0     4368 kB
pg_shdepend                         r           71390.0     4296 kB
pg_largeobject_metadata_oid_index   i           131790.0    2912 kB
pg_largeobject_loid_pn_index        i           129767.0    2872 kB
pg_shdepend_reference_index         i           71390.0     2576 kB
pg_database                         r           5048.0      1496 kB

这正常吗?我应该/我可以做些什么吗?

【问题讨论】:

【参考方案1】:

您最后一次吸尘是什么时候?它可以轻松减小开发数据库的大小。 此外,索引通常比它们索引的数据大,原因是数据对齐是为了速度,而不是空间效率。

PG 还有一组关于您的数据库的元数据和其他“幕后”表来跟踪事物。它跟踪有关您的数据的统计信息,例如最小值和最大值,以及有关您的数据和 postgres 的元数据/维护表中的特定经常出现的值的统计信息。

当您删除数据时,数据并没有真正从数据库中“删除”——因此,建议使用“真空满”。 Vacuum Full 将锁定您的表并重建它们,合并可用空间并消除以前删除的记录中的空间。像 pg_largeobject 这样的表实际上存储了来自数据库中“大”记录的数据,而且由于以前存储的数据现在被删除了,所以数据也可能很大。

我会做一个真空充满,看看你会得到什么结果。

【讨论】:

以上是关于为啥用户表的大小远小于 PostgreSQL 数据库中其他对象的大小的主要内容,如果未能解决你的问题,请参考以下文章

为什么符号大小之和远小于“ .text”节的大小?

为啥 boost::asio::read 缓冲区数据大小小于读取大小?

hibernate映射postgreSQL数据库中的表时,表名是大写的时候为啥hibernate不能映射实体

为啥 PostgreSQL 中的新用户可以连接到所有数据库?

为啥 Oracle Sql*Plus 打印许多不需要的标题?

loadrunner-检测到的响应时间远小于用户实际查询时间。 哪位大神能帮忙看看?急急急~!