PostgreSQL索引膨胀

最后编辑时间:2022年1月23日00:07:27

通常是因为数据乱序写入导致的,索引页中的数据是有序的,而索引字段乱序写入,会导致索引频繁分裂,使得索引页并不是百分百填满,自然出现索引膨胀的情况。

示例:
先创建索引,在乱序写入数据

create table t(id int);
create index idx_t on t using btree(id);
insert into t select random()*1000000 from generate_series(1,1000000);
\di+ idx_t;
[23:03:37]postgres@postgres=>create table t(id int);
[23:03:38]CREATE TABLE
[23:04:02]postgres@postgres=>create index idx_t on t using btree(id);
[23:04:02]CREATE INDEX
[23:04:45]postgres@postgres=>insert into t select random()*1000000 from generate_series(1,1000000);
[23:04:55]INSERT 0 1000000
[23:05:10]postgres@postgres=>\di+ idx_t;
[23:05:10]                        List of relations
[23:05:10] Schema | Name  | Type  |  Owner   | Table | Size  | Description
[23:05:10]--------+-------+-------+----------+-------+-------+-------------
[23:05:10] public | idx_t | index | postgres | t     | 28 MB |
[23:05:10](1 row)
[23:05:10]

先创建索引,在顺序写入数据

truncate table t;
insert into select generate_series(1,1000000);
\di+ idx_t;
[23:05:21]postgres@postgres=>truncate table t;
[23:05:21]TRUNCATE TABLE
[23:05:47]postgres@postgres=>insert into t select generate_series(1,1000000);
[23:05:50]INSERT 0 1000000
[23:05:59]postgres@postgres=>\di+ idx_t;
[23:05:59]                        List of relations
[23:05:59] Schema | Name  | Type  |  Owner   | Table | Size  | Description
[23:05:59]--------+-------+-------+----------+-------+-------+-------------
[23:05:59] public | idx_t | index | postgres | t     | 21 MB |
[23:05:59](1 row)
[23:05:59]

先插入数据再创建索引

drop index idx_t;
create index idx_t on t using btree(id);
\di+ idx_t;
[23:06:13]postgres@postgres=>drop index idx_t;
[23:06:13]DROP INDEX
[23:06:35]postgres@postgres=>create index idx_t on t using btree(id);
[23:06:36]CREATE INDEX
[23:06:45]postgres@postgres=>\di+ idx_t;
[23:06:45]                        List of relations
[23:06:45] Schema | Name  | Type  |  Owner   | Table | Size  | Description
[23:06:45]--------+-------+-------+----------+-------+-------+-------------
[23:06:45] public | idx_t | index | postgres | t     | 21 MB |
[23:06:45](1 row)
[23:06:45]

可以发现,顺序写入数据的索引大小和重建索引后的索引大小一样,说明:顺序写入并不会导致索引膨胀

PostgreSQL索引膨胀相关推荐

  1. PostgreSQL 收缩膨胀表或索引 - pg_squeeze or pg_repack

    PostgreSQL 收缩膨胀表或索引 - pg_squeeze or pg_repack 作者 digoal 日期 2016-10-30 标签 PostgreSQL , pg_repack , pg ...

  2. postgresql索引_PostgreSQL中的索引— 10(Bloom)

    postgresql索引 indexing engine and the interface of access methods, as well as 索引引擎和访问方法的接口,以及hash ind ...

  3. postgresql索引_PostgreSQL中的索引— 8(RUM)

    postgresql索引 indexing engine, the interface of access methods, and main access methods, such as: 索引引 ...

  4. postgresql索引_PostgreSQL中的索引— 6(SP-GiST)

    postgresql索引 indexing engine, 索引引擎 , the interface of access methods, and three methods: 访问方法的接口以及三种 ...

  5. PostgreSQL索引介绍

    梦中彩虹 博客园 首页 新随笔 联系 管理 随笔 - 131  文章 - 1  评论 - 14 PostgreSQL索引介绍 INDEX 索引是增强数据库性能的常用方法.索引使得数据库在查找和检索数据 ...

  6. 掌握查询利器 深入理解PostgreSQL索引原理与优化

    文章目录 一.前言 1.1 PostgreSQL索引的重要性 1.2 本文的结构和目的 二.索引概述 2.1 什么是索引 2.2 为什么要使用索引 2.3 索引如何工作 三.索引类型 3.1 B-tr ...

  7. 关于PostgreSQL空间膨胀的研究

    首先,我们先启用一个数据库自带的控件方便对数据情况进行分析 create extension pgstattuple; 然后,还需要一个存储过程方便快速的制造数据 create function f1 ...

  8. PostgreSQL表膨胀终结者

    点击上方蓝字关注我 PostgreSQL数据库表在删除数据后磁盘空间未释放,该怎么办? 主流的压缩表工具有哪些?该如何选择? 1.从空间未释放说起 近期生产环境出现一张表占用size已达2T,且会定期 ...

  9. PostgreSQL索引页

    本页目的,是起到索引其他所有本人所写文档的作用: 分类一:PostgreSQL基础知识与基本操作--------------------  PostgreSQL基础知识与基本操作索引页 分类二:Pos ...

最新文章

  1. 解决 Could not autowire. No beans of ‘UserDao‘ type found 问题
  2. textview html字体颜色变粗,安卓使用Html实现textview部分字体变色加粗的效果(使用Html实现多种字体效果)-Go语言中文社区...
  3. hdu 1081 To The Max(最大子段和的升级版,二维)
  4. WinForm窗体缩放动画
  5. 批处理命令set截取字符详解
  6. mysql中的order by
  7. 打印某个user在指定时间段内做过的personalization detail
  8. c语言多线程面试题,iOS面试题--网络多线程
  9. python绘制时间轴_MatPlotLib动态时间轴
  10. 别再瞎学 Python 了!
  11. 深入理解 WordPress 数据库中的用户数据 wp_user
  12. 9-15 redis-trib构建集群
  13. 剑指offer面试题38. 字符串的排列(回溯)
  14. v4l2-ctl 控制命令
  15. python解释器使用函数可以进入帮助系统_使用help()命令可以进入帮助系统。
  16. Android Design 1: Back键和Up键在App导航中的表现
  17. 使用maven搭建ssm项目配置+tomact
  18. 2021年PayPal要如何绑定到国内?
  19. greedy算法计算金字塔分数问题
  20. 如何解决Css属性text-overflow:ellipsis 不起作用(文本溢出显示省略号)

热门文章

  1. CV之Face Change:基于人工智能实现国内众多一线美女明星换脸(基于Face++输出4*106个特征点定位+融合代码、deepfake技术)
  2. 全方位揭秘!大数据从0到1的完美落地之Linux二进制软件安装
  3. 未名企鹅极客 | 医药流向的数据仓库建模
  4. CSS开启硬件加速 hardware accelerated
  5. 完美的Windows Server 2008 R2 SP1 模板
  6. 重要的不是你正在做什么,而是你在想什么
  7. 第七章 - 类的详细设计
  8. 再见ip.taobao,全网显示 IP 归属地,用上这个开源库,实现也太简单了!
  9. [含文档+PPT+源码等]基于SSM个人财务记账账单收入支出统计管理系统[包运行成功]
  10. 关于CV_8U和CV_32F