博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PostgreSQL的索引膨胀
阅读量:6312 次
发布时间:2019-06-22

本文共 3058 字,大约阅读时间需要 10 分钟。

磨砺技术珠矶,践行数据之道,追求卓越价值

回到上一级页面:    回到顶级页面:

索引膨胀,主要是针对B-tree而言。

索引膨胀的几个来源:

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

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

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

如何找出 膨胀的索引,参见:

 

CREATE OR REPLACE VIEW bloat AS      SELECT        schemaname, tablename, reltuples::bigint, relpages::bigint, otta,        ROUND(CASE WHEN otta=0 THEN 0.0 ELSE sml.relpages/otta::numeric END,1) AS tbloat,        relpages::bigint - otta AS wastedpages,        bs*(sml.relpages-otta)::bigint AS wastedbytes,        pg_size_pretty((bs*(relpages-otta))::bigint) AS wastedsize,        iname, ituples::bigint, ipages::bigint, iotta,        ROUND(CASE WHEN iotta=0 OR ipages=0 THEN 0.0 ELSE ipages/iotta::numeric END,1) AS ibloat,        CASE WHEN ipages < iotta THEN 0 ELSE ipages::bigint - iotta END AS wastedipages,        CASE WHEN ipages < iotta THEN 0 ELSE bs*(ipages-iotta) END AS wastedibytes,        CASE WHEN ipages < iotta THEN pg_size_pretty(0) ELSE pg_size_pretty((bs*(ipages-iotta))::bigint)                   END AS wastedisize      FROM (        SELECT          schemaname, tablename, cc.reltuples, cc.relpages, bs,          CEIL((cc.reltuples*((datahdr+ma-            (CASE WHEN datahdr%ma=0 THEN ma ELSE datahdr%ma END))+nullhdr2+4))/(bs-20::float)) AS otta,                  COALESCE(c2.relname,'?') AS iname, COALESCE(c2.reltuples,0) AS ituples,                   COALESCE(c2.relpages,0) AS ipages,                   COALESCE(CEIL((c2.reltuples*(datahdr-12))/(bs-20::float)),0) AS iotta                             -- very rough approximation, assumes all cols        FROM (          SELECT            ma,bs,schemaname,tablename,            (datawidth+(hdr+ma-(case when hdr%ma=0 THEN ma ELSE hdr%ma END)))::numeric AS datahdr,            (maxfracsum*(nullhdr+ma-(case when nullhdr%ma=0 THEN ma ELSE nullhdr%ma END))) AS nullhdr2          FROM (            SELECT              schemaname, tablename, hdr, ma, bs,              SUM((1-null_frac)*avg_width) AS datawidth,              MAX(null_frac) AS maxfracsum,              hdr+(                SELECT 1+count(*)/8                FROM pg_stats s2                WHERE null_frac<>0 AND s2.schemaname = s.schemaname AND s2.tablename = s.tablename              ) AS nullhdr            FROM pg_stats s, (              SELECT                (SELECT current_setting('block_size')::numeric) AS bs,                CASE WHEN substring(v,12,3) IN ('8.0','8.1','8.2') THEN 27 ELSE 23 END AS hdr,                CASE WHEN v ~ 'mingw32' THEN 8 ELSE 4 END AS ma              FROM (SELECT version() AS v) AS foo            ) AS constants            GROUP BY 1,2,3,4,5          ) AS foo        ) AS rs        JOIN pg_class cc ON cc.relname = rs.tablename        JOIN pg_namespace nn ON cc.relnamespace = nn.oid AND nn.nspname = rs.schemaname        LEFT JOIN pg_index i ON indrelid = cc.oid        LEFT JOIN pg_class c2 ON c2.oid = i.indexrelid      ) AS sml      WHERE sml.relpages - otta > 0 OR ipages - iotta > 10      ORDER BY wastedbytes DESC, wastedibytes DESC;

 

回到上一级页面:    回到顶级页面:

磨砺技术珠矶,践行数据之道,追求卓越价值

转载地址:http://yyhxa.baihongyu.com/

你可能感兴趣的文章
在网页中加入百度搜索框实例代码
查看>>
在Flex中动态设置icon属性
查看>>
采集音频和摄像头视频并实时H264编码及AAC编码
查看>>
3星|《三联生活周刊》2017年39期:英国皇家助产士学会于2017年5月悄悄修改了政策,不再鼓励孕妇自然分娩了...
查看>>
高级Linux工程师常用软件清单
查看>>
堆排序算法
查看>>
folders.cgi占用系统大量资源
查看>>
路由器ospf动态路由配置
查看>>
zabbix监控安装与配置
查看>>
python 异常
查看>>
last_insert_id()获取mysql最后一条记录ID
查看>>
可执行程序找不到lib库地址的处理方法
查看>>
bash数组
查看>>
Richard M. Stallman 给《自由开源软件本地化》写的前言
查看>>
oracle数据库密码过期报错
查看>>
修改mysql数据库的默认编码方式 .
查看>>
zip
查看>>
How to recover from root.sh on 11.2 Grid Infrastructure Failed
查看>>
rhel6下安装配置Squid过程
查看>>
《树莓派开发实战(第2版)》——1.1 选择树莓派型号
查看>>