pg索引膨胀问题---重建索引

问题:发现数据库中很多表的索引大小超过数据大小。经检查,生产CA、CZ、MU、HU、PSG、RIUE库都存在这个现象。

原因:据运行同事介绍索引膨胀问题无法避免,频繁更新就会带来这个问题。

解决方法:对于大的索引可以采用重建的方式解决。以下两种方法推荐第一种。

 

方法一:停止应用(这个操作会锁表),重建索引(注:重建完索引名称不变)

sql:reindex index 索引名称

时间:速度较快。2G大小的表,基本上1分钟左右可以建完索引。

 

还可以针对表重建索引,这个操作会加排他锁 :

 reindex table 表名 

 

方法二:在线建新索引,再把旧索引删除

sql:根据不同索引采用不同的建索引命令,例如:

 

       普通索引

   create index concurrently  idx_tbl_2 on tbl(id);
   drop index idx_tbl_1;

    唯一索引

create unique index concurrently user_info_username_key_1 on user_info(username);
begin;
alter table user_info drop constraint user_info_username_key;
alter table user_info add constraint user_info_username_key unique using index user_info_username_key_1;
end;

 主键索引

create unique index concurrently user_info_pkey_1 on user_info(id);
begin;
alter table user_info drop constraint user_info_pkey;
alter table user_info add constraint user_info_pkey primary key using index user_info_pkey_1;
end;

时间:不停应用的话,业务忙的时候可能会非常长的时间。

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值