极乐门资源网 Design By www.ioogu.com

索引膨胀的几个来源:

1 大量删除发生后,导致索引页面稀疏,降低了索引使用效率。

2 PostgresQL 9.0之前的版本,vacuum full 会同样导致索引页面稀疏。

3 长时间运行的事务,禁止vacuum对表的清理工作,因而导致页面稀疏状态一直保持。

查看重复索引

SELECT pg_size_pretty(SUM(pg_relation_size(idx))::BIGINT) AS SIZE,
  (array_agg(idx))[1] AS idx1, (array_agg(idx))[2] AS idx2,
  (array_agg(idx))[3] AS idx3, (array_agg(idx))[4] AS idx4
FROM (
 SELECT indexrelid::regclass AS idx, (indrelid::text ||E'\n'|| indclass::text ||E'\n'|| indkey::text ||E'\n'||
           COALESCE(indexprs::text,'')||E'\n' || COALESCE(indpred::text,'')) AS KEY
 FROM pg_index) sub
GROUP BY KEY HAVING COUNT(*)>1
ORDER BY SUM(pg_relation_size(idx)) DESC;

表的大小和表中索引个数

SELECT
 t.tablename,
 indexname,
 c.reltuples AS num_rows,
 pg_size_pretty(pg_relation_size(quote_ident(t.tablename)::text)) AS table_size,
 pg_size_pretty(pg_relation_size(quote_ident(indexrelname)::text)) AS index_size,
 CASE WHEN indisunique THEN 'Y'
  ELSE 'N'
 END AS UNIQUE,
 idx_scan AS number_of_scans,
 idx_tup_read AS tuples_read,
 idx_tup_fetch AS tuples_fetched
FROM pg_tables t
LEFT OUTER JOIN pg_class c ON t.tablename=c.relname
LEFT OUTER JOIN
 ( SELECT c.relname AS ctablename, ipg.relname AS indexname, x.indnatts AS number_of_columns, idx_scan, idx_tup_read, idx_tup_fetch, indexrelname, indisunique FROM pg_index x
   JOIN pg_class c ON c.oid = x.indrelid
   JOIN pg_class ipg ON ipg.oid = x.indexrelid
   JOIN pg_stat_all_indexes psai ON x.indexrelid = psai.indexrelid )
 AS foo
 ON t.tablename = foo.ctablename
WHERE t.schemaname='public'
ORDER BY 1,2;

获取每个表的行数,索引和一些关于这些索引的信息(比较详细)

SELECT
 pg_class.relname,
 pg_size_pretty(pg_class.reltuples::BIGINT) AS rows_in_bytes,
 pg_class.reltuples AS num_rows,
 COUNT(indexname) AS number_of_indexes,
 CASE WHEN x.is_unique = 1 THEN 'Y'
  ELSE 'N'
 END AS UNIQUE,
 SUM(CASE WHEN number_of_columns = 1 THEN 1
    ELSE 0
   END) AS single_column,
 SUM(CASE WHEN number_of_columns IS NULL THEN 0
    WHEN number_of_columns = 1 THEN 0
    ELSE 1
   END) AS multi_column
FROM pg_namespace 
LEFT OUTER JOIN pg_class ON pg_namespace.oid = pg_class.relnamespace
LEFT OUTER JOIN
  (SELECT indrelid,
   MAX(CAST(indisunique AS INTEGER)) AS is_unique
  FROM pg_index
  GROUP BY indrelid) x
  ON pg_class.oid = x.indrelid
LEFT OUTER JOIN
 ( SELECT c.relname AS ctablename, ipg.relname AS indexname, x.indnatts AS number_of_columns FROM pg_index x
   JOIN pg_class c ON c.oid = x.indrelid
   JOIN pg_class ipg ON ipg.oid = x.indexrelid )
 AS foo
 ON pg_class.relname = foo.ctablename
WHERE 
  pg_namespace.nspname='public'
AND pg_class.relkind = 'r'
GROUP BY pg_class.relname, pg_class.reltuples, x.is_unique
ORDER BY 2;

补充:postgresql查看表膨胀

查看表膨胀(对所有表产进行膨胀率排序)

SQL文如下:

SELECT
 schemaname||'.'||relname as table_name,
 pg_size_pretty(pg_relation_size(schemaname||'.'||relname)) as table_size,
 n_dead_tup,
 n_live_tup,
 round(n_dead_tup * 100 / (n_live_tup + n_dead_tup),2) AS dead_tup_ratio
FROM
 pg_stat_all_tables
WHERE
 n_dead_tup >= 1000
ORDER BY dead_tup_ratio DESC
LIMIT 10;

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。

标签:
postgresql,表和索引,膨胀

极乐门资源网 Design By www.ioogu.com
极乐门资源网 免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
极乐门资源网 Design By www.ioogu.com

评论“postgresql查看表和索引的情况,判断是否膨胀的操作”

暂无postgresql查看表和索引的情况,判断是否膨胀的操作的评论...

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。