PostgreSQL索引大小和值编号

ove*_*d_s 7 postgresql indexing postgresql-9.3

我正在尝试获取索引的统计信息.我正在寻找索引及其大小的总值.

我只能在表上找到所有索引的大小.表pg_class列relpages和reltuples显示表的值,而不是特定的索引级别.

此外,函数pg_indexes_size将表名作为参数,并返回该表的总索引大小.

有没有办法在索引级别获取大小和行号?我正在使用PostgreSQL 9.3.

在此先感谢您的帮助

小智 54

此 sql 将为您提供表和索引大小的详细信息

SELECT
   relname  as table_name,
   pg_size_pretty(pg_total_relation_size(relid)) As "Total Size",
   pg_size_pretty(pg_indexes_size(relid)) as "Index Size",
   pg_size_pretty(pg_relation_size(relid)) as "Actual Size"
   FROM pg_catalog.pg_statio_user_tables 
ORDER BY pg_total_relation_size(relid) DESC;
Run Code Online (Sandbox Code Playgroud)

  • 请注意,上面的“实际大小”表示“没有 TOAST 的表数据用于长列”。相反,您可能想使用 pg_size_pretty(pg_table_size(relid)) 作为“数据大小”,这将为您提供数据大小 + 索引大小 = 总大小。有关更多信息,请参阅 /sf/answers/4927844561/。 (2认同)

小智 51

这是检索以下信息的查询:

  • 总尺寸

  • 所有索引的总大小

  • 桌子尺寸

  • 索引大小

  • 估计表行数

     SELECT i.relname "Table Name",indexrelname "Index Name",
     pg_size_pretty(pg_total_relation_size(relid)) As "Total Size",
     pg_size_pretty(pg_indexes_size(relid)) as "Total Size of all Indexes",
     pg_size_pretty(pg_relation_size(relid)) as "Table Size",
     pg_size_pretty(pg_relation_size(indexrelid)) "Index Size",
     reltuples::bigint "Estimated table row count"
     FROM pg_stat_all_indexes i JOIN pg_class c ON i.relid=c.oid 
     WHERE i.relname='uploads'
    
    Run Code Online (Sandbox Code Playgroud)

也许对某人来说会有用。


Vao*_*sun 8

pg_table_size('index_name') 对于单个索引 - 但它只会显示磁盘上的大小,而不是数据量

count(1) 获得准确的当前行数

sum(pg_column_size(column_name)) from table_name 用于估计列数据量

你可以尝试自己像:

t=# \di+ tbl*
                                    List of relations
 Schema |         Name         | Type  |  Owner   |     Table      |  Size  | Description
--------+----------------------+-------+----------+----------------+--------+-------------
 public | tbl_pkey  | index | postgres | tbl | 156 MB |
 public | tbl_unpic | index | postgres | tbl | 46 MB  |
 public | tbl_x1    | index | postgres | tbl | 57 MB  |
(3 rows)

t=# \dt+ tbl
                        List of relations
 Schema |      Name      | Type  |  Owner   | Size  | Description
--------+----------------+-------+----------+-------+-------------
 public | tbl | table | postgres | 78 MB |
(1 row)

t=# select pg_size_pretty(pg_total_relation_size('tbl'));
 pg_size_pretty
----------------
 337 MB
(1 row)

t=# select 78+57+46+156;
 ?column?
----------
      337
(1 row)
Run Code Online (Sandbox Code Playgroud)

并检查psql如何获取单个索引大小,运行它与psql -E..

再一次 - 上面的函数与磁盘的大小有关 - 它可能/(可能不)与实际数据量有所不同.吸尘有助于此

更新 我不知道你在哪里直接得到索引中的"行"数,因此只能提供间接方式.例如,让我有部分索引,所以"行数"与表不同.我可以用EXPLAIN检查估计(当然你必须重复where子句)检查rows=66800in Index Only Scan using给我一个关于该索引中行数的想法(实际上它是rows=64910你可以得到explain analyze或只是运行count(1)).我在pg_stats中找不到相关信息 - 也许有一些公式......不知道.