以下 Postgres SQL 查询将列出来自所有模式的所有表及其大小和索引大小。如果一个表只是一个索引表,它将显示为 100% 索引。
SELECT schema,
name,
pg_size_pretty(CASE WHEN is_index THEN 0 ELSE s END) AS size,
pg_size_pretty(CASE WHEN is_index THEN s ELSE st - s END) AS index,
CASE WHEN st = 0 THEN 0
WHEN is_index THEN 100
ELSE 100 - ((s*100) / st) END || '%' as ratio,
pg_size_pretty(st) as total
FROM (SELECT *,
st = s AS is_index
FROM (SELECT nspname as schema,
relname as name,
pg_relation_size(nspname || '.' || relname) as s,
pg_total_relation_size(nspname || '.' || relname) as st
FROM pg_class
JOIN pg_namespace ON (relnamespace = pg_namespace.oid)) AS p)
AS pp
ORDER BY st DESC LIMIT 30;
它将给出以下结果:
schema | name | size | index | ratio | total
----------------+------------------------+---------+---------+-------+---------
public | conf | 4072 kB | 4360 kB | 52% | 8432 kB
archive | product_param | 4048 kB | 3968 kB | 50% | 8016 kB
public | conf_pkey | 0 bytes | 4320 kB | 100% | 4320 kB
archive | product_value | 1568 kB | 1136 kB | 43% | 2704 kB
public | param_mapping | 1472 kB | 832 kB | 37% | 2304 kB
archive | supplie_price | 944 kB | 896 kB | 49% | 1840 kB
public | product_param_param_id | 0 bytes | 1552 kB | 100% | 1552 kB
archive | product_param_id | 0 bytes | 1536 kB | 100% | 1536 kB
我已经到了一个看不到所有树木的森林的地步,它开始变得有点笨拙。
我想知道其中是否有任何可以简化或冗余的内容?如果可以使查询变得更简单,则列不必保持不变。