为特定模式选择表大小和行数

  • 本文关键字:模式 选择 postgresql
  • 更新时间 :
  • 英文 :


我需要一个查询,它将为我提供特定schema中每个表的信息。信息是size of the table (best in Mb)row counts。我准备了一些查询如下,但不确定结果是否在megabytes。此外,我不知道如何从information_schema.tables中获得row counts。有人能帮忙吗?

这是我当前的查询:

select table_name, pg_relation_size(quote_ident(table_name)) 
from information_schema.tables
where table_schema = 'myschema'
order by 2;

编辑:

通过这个,我可以得到行计数,但不知道如何过滤基于on特定的模式,以及如何添加表大小(Mb)到它。

select nspname as schema, relname as tablename, 
reltuples as rowcounts
from pg_class c JOIN pg_catalog.pg_namespace n
ON n.oid = c.relnamespace where relkind='r'
and relname like 't%'
order by nspname, reltuples desc;

您可以使用pg_relation_size函数以兆字节为单位获得表的大小。默认情况下,该函数将以字节为单位返回结果,但您可以将结果转换为兆字节。此外,要按特定的方案进行过滤,请使用information_schema.tables中的table_schema条目。

SELECT 
table_name,
(pg_relation_size(quote_ident(table_name)) / 1024 /1024) size,
reltuples as rowcounts FROM information_schema.tables ist
left join pg_class pc on pc.relname = ist.table_name
WHERE table_schema = 'public'

sql<>daddy.io

最新更新