5

以下 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

我已经到了一个看不到所有树木的森林的地步,它开始变得有点笨拙。

我想知道其中是否有任何可以简化或冗余的内容?如果可以使查询变得更简单,则列不必保持不变。

4

4 回答 4

3

使用此查询,我得到了可比较的结果(具有不同的格式):

select
    nspname as schema,
    relname as name,
    pg_relation_size(pg_class.oid) as size,
    pg_indexes_size(pg_class.oid) as index,
    pg_total_relation_size(pg_class.oid) as total,
    100 * case when relkind = 'i' then pg_relation_size(pg_class.oid) 
                                  else pg_indexes_size(pg_class.oid) end 
        / pg_total_relation_size(pg_class.oid) as i_ratio
from 
    pg_class
    join pg_namespace on relnamespace = pg_namespace.oid
order by 5 desc
于 2012-04-05T16:21:46.550 回答
1

首先为什么不使用 CTE,它们会让你的代码更具可读性。然后你不返回 is_index 所以它似乎是多余的

with p as (
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)
),
pp as (
SELECT *,
        case when st = s then 0 else s end as size,
        case when st = s then s else st-s end as index

   FROM p
)
select schema,
       name,
       pg_size_pretty(size) as size,        
       pg_size_pretty(index) as index,
       (case st 
             when 0 then 0 
             else index*100 / st 
        end) || '%' ratio,
       st total
 from pp
 order by st desc limit 30;
于 2012-04-04T07:24:23.857 回答
1

我真正想做的是指出quzary的响应应该使用oid而不是创建无法解析回oid的字符串。

现在我必须写一个合适的帖子(也许这是阻止新手评论的重点?)这是另一个经过整理和修饰的版本:

WITH p AS (
  SELECT n.nspname AS schema,
         c.relname AS name,
         pg_relation_size(c.oid) AS s,
         pg_total_relation_size(c.oid) AS st
  FROM pg_class c, pg_namespace n
  WHERE c.relnamespace = n.oid
)
SELECT schema, name,
  pg_size_pretty(s) AS size,        
  pg_size_pretty(st - s) AS index,
  (100.0 * s / NULLIF(st, 0))::numeric(10,1) AS "% data of total",
  st AS total
FROM p
ORDER BY st DESC
LIMIT 30;

请注意,添加以下行可能很有用:

AND c.relkind = 'r'

WHERE从句中p。这会将其限制为仅关系/表,并使代码对表大小的一般摘要有用。

于 2012-06-26T15:24:56.723 回答
0

不要忘记这一点,pg_relation_size并且pg_total_relation_size不区分大小写!

pg_relation_size(nspname || '.' || relname)

实际上应该是:

pg_relation_size('"' || nspname || '.' || relname || '"')

所以它也适用于大写。(我花了一段时间才弄清楚这个)

于 2012-07-19T09:35:48.623 回答