tho*_*iha 6 postgresql indexing reindex
使用此查询:
我有数百个无效索引:
SELECT * FROM pg_class, pg_index WHERE pg_index.indisvalid = false AND pg_index.indexrelid = pg_class.oid;
public | post_aggregates_pkey_ccnew_ccnew_ccnew1
public | post_aggregates_post_id_key_ccnew_ccnew_ccnew1
public | idx_post_aggregates_stickied_hot_ccnew_ccnew_ccnew1
public | idx_post_aggregates_hot_ccnew_ccnew_ccnew1
...
Run Code Online (Sandbox Code Playgroud)
它们似乎没有被使用,而且我不知道为什么要创建它们(在我看来它们不应该保留),因为原始索引仍然存在。
do $$
declare
rec record;
begin
for rec in
select relnamespace::regnamespace as namespace, relname
from pg_index i
join pg_class c on c.oid = i.indexrelid
where not indisvalid
loop
execute format('drop index %s.%s', rec.namespace, rec.relname);
-- optionally:
-- raise notice '%', format('drop index %s.%s', rec.namespace, rec.relname);
end loop;
end $$;
Run Code Online (Sandbox Code Playgroud)
CREATE TABLE当在或中创建或更改表约束时,Postgres 会自动创建索引ALTER TABLE。除此之外,它从不自行创建索引。
无效索引最可能的原因是不小心使用命令CREATE [UNIQUE] INDEX CONCURRENTLY。当命令在并行事务中执行时,很有可能出现死锁,从而导致命令失败并留下无效索引。当并发创建唯一索引时,唯一性违规也可能导致失败。
并发索引应该受到了解这些问题的管理员的严格控制,特别是当它定期自动执行时。
请阅读文档中的更多内容。
| 归档时间: |
|
| 查看次数: |
5857 次 |
| 最近记录: |