PostgreSQL重建索引

阅读: 评论:0

PostgreSQL重建索引

PostgreSQL重建索引

今天遇到查询数据库表数据,报错需要重建索引

PSQLException: 错误: 索引"nnn"在块0上包含未期望的零页 建议:请重建索引 (REINDEX)

创建索引语法:

CREATE INDEX index_name ON table_name (column_name);

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

reindex index 索引名称

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

 reindex table 表名 

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

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

普通索引

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

唯一索引

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;

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

本文发布于:2024-01-27 21:33:21,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/17063623992761.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

上一篇:AOP
标签:索引   PostgreSQL
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23