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

时间:2016-10-10 16:07:53

标签: database postgresql size postgresql-9.4

我需要帮助来理解我的数据库的大小。我想这个问题是好的 对于数据库专业人员来说简单。我不是专业人士,但我想了解。我用 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 个答案:

答案 0 :(得分:0)

当你最后一次真空充满时?它可以轻松地缩小开发数据库的大小。 此外,索引通常大于它们索引的数据,原因是数据是为了速度而不是空间效率而对齐的。

PG还有一系列关于您的数据库和其他"幕后的元数据"用于跟踪事物的表格。它会跟踪您的数据统计信息,例如最小值和最大值以及有关数据和postgres的元数据/维护表中特定常见值的统计信息。

数据没有真正被删除"删除数据库时关闭数据库 - 因此,建议"真空吸尘"。 Vacuum Full将锁定您的表并重建它们,整合可用空间并消除先前删除的记录的空间。像pg_largeobject这样的表实际上存储来自" large"数据库中的记录,由于以前存储的数据现在已被删除,因此可能会很大。

我做了一个真空吸尘器,看看你得到了什么结果。