数据库对象大小统计脚本
获取数据库排名前20的表
select t.table_catalog as db,
n.nspname as schemaname,
c.relname,
c.reltuples::numeric as rowcount,
sys_size_pretty(sys_table_size ( '"' || nspname || '"."' || relname || '"' )) as table_size,
sys_size_pretty(sys_indexes_size ( '"' || nspname || '"."' || relname || '"' )) as indexes_size,
sys_size_pretty (sys_total_relation_size ( '"' || nspname || '"."' || relname || '"' )) as total_size
from sys_class c left join sys_namespace n on ( n.oid = c.relnamespace )
left join information_schema.tables t on (n.nspname= t.table_schema and c.relname=t."table_name" )
where nspname not in ( 'sys_catalog', 'information_schema' ) and relkind in ('r','p') order by reltuples desc limit 20;
统计数据库对象类型和数量
select * from (
select
nsp.nspname as schemaname,
case c.relkind
when 'r' then 'table'
when 'm' then 'materialized_view'
when 'i' then 'index'
when 'S' then 'sequence'
when 'v' then 'view'
when 'c' then 'composite type'
when 't' then 'toast'
when 'f' then 'foreign table'
when 'p' then 'partitioned_table'
when 'I' then 'partitioned_index'
else c.relkind::text
end as objecttype,
count(*) total
from sys_class c
join sys_namespace nsp
on nsp.oid = c.relnamespace
where nsp.nspname not in ('information_schema', 'sys_catalog')
and nsp.nspname not like 'sys_toast%'
group by nsp.nspname,c.relkind
union all
select n.nspname as schemaname,
case p.prokind
when 'a' then 'agg'
when 'w' then 'window'
when 'p' then 'proc'
else 'func'
end as "type",
count(*) total
from sys_catalog.sys_proc p
left join sys_catalog.sys_namespace n on n.oid = p.pronamespace
where sys_catalog.sys_function_is_visible(p.oid)
and n.nspname not in ('information_schema', 'sys_catalog')
group by n.nspname ,p.prokind) order by schemaname;
KINGBASE研究院
分类:
SQL
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!