PG创建or修改索引
admin
2023-03-12 11:02:10
0

pgsql中的索引不能重名,重名的创建失败。

 

创建二级索引的命令:create index CONCURRENTLY idx_abc on tb1(a,b);

 

注意:reindex 重建索引的过程是阻塞的,一般大表不建议使用这个命令,可以重建一个索引,然后删除老的索引。

 

下面看一个《PostgreSQL实战》书上page202的例子:

由于PGMVCC机制,当运行大量的更新操作后,会有索引膨胀的现象。这时候 可以通过 create index concurrently 不阻塞查询和更新的情况下,在线重新重建索引,创建好索引之后,再删除原先的索引,减少索引的尺寸,提高查询速度。对于主键也可以使用这种方式进行,例如:

db1=# \d testdata

                 Table "public.testdata"

  Column  |     Type     | Collation | Nullable | Default

----------+--------------+-----------+----------+---------

 id       | integer      |           | not null |

 course   | integer      |           |          |

 grade    | numeric(4,2) |           |          |

 testtime | date         |           |          |

Indexes:

    "testdata_pkey" PRIMARY KEY, btree (id)

 

db1=# create unique index concurrently on testdata using btree(id);

 

db1=# select

schemaname,

relname,

indexrelname,

pg_relation_size (indexrelid) as index_size,

idx_scan,

idx_tup_read,

idx_tup_fetch

from pg_stat_user_indexes where

indexrelname in (select indexname from pg_indexes where schemaname ='public' and tablename='testdata');

 schemaname | relname  |  indexrelname   | index_size | idx_scan | idx_tup_read | idx_tup_fetch

------------+----------+-----------------+------------+----------+--------------+---------------

 public     | testdata | testdata_pkey   |      16384 |        2 |      5999998 |       5999998

 public     | testdata | testdata_id_idx |      16384 |        0 |            0 |             0

(2 rows)

 

db1=# begin;

BEGIN

 

db1=# alter table testdata drop constraint testdata_pkey;

ALTER TABLE

 

db1=# alter table testdata add constraint testdata_id_idx primary key using index testdata_id_idx ;

ALTER TABLE

 

db1=# end;

COMMIT

 

db1=# \d testdata

                 Table "public.testdata"

  Column  |     Type     | Collation | Nullable | Default

----------+--------------+-----------+----------+---------

 id       | integer      |           | not null |

 course   | integer      |           |          |

 grade    | numeric(4,2) |           |          |

 testtime | date         |           |          |

Indexes:

    "testdata_id_idx" PRIMARY KEY, btree (id)

 

db1=# select

schemaname,

relname,

indexrelname,

pg_relation_size (indexrelid) as index_size,

idx_scan,

idx_tup_read,

idx_tup_fetch

from pg_stat_user_indexes where

indexrelname in (select indexname from pg_indexes where schemaname ='public' and tablename='testdata');

schemaname | relname  |  indexrelname   | index_size | idx_scan | idx_tup_read | idx_tup_fetch

------------+----------+-----------------+------------+----------+--------------+---------------

 public     | testdata | testdata_id_idx |      16384 |        0 |            0 |             0

(1 row)

 

 

这样就完成了主键索引的重建,对于大规模的数据库集群,可以通过 pg_repack 工具进行定时的索引重建。

 


相关内容

热门资讯

音乐作品被白宫用于宣传打击伊朗... 据美国《国会山报》《华盛顿邮报》等多家媒体报道,美国流行歌手“水果姐”凯蒂·佩里当地时间25日在社交...
特朗普暂停空袭,美媒曝光核心原... 据《纽约时报》报道,在连续13晚对伊朗发动袭击后,7月25日,美军没有宣布任何夜间打击行动。据知情人...
一年内18次操纵证券市场,方某... 7月24日,根据中国证券监督管理委员会山东监管局发布的行政处罚决定书,方某军因一年内18次操纵市场的...
电热水器红色蓝色那里漏水 因为电热水器的发热器密封圈出现了破损,就会导致红色蓝色那里漏水;也有可能是由于电热水器的内胆出现了故...
电热水器漏水弹出一个圆片 可能是密封圈,是因为漏水造成这一部位出现问题,如果刚开始仅仅是滴水,慢慢的相邻的位置就开始出现侵蚀,...
电热水器为什么热水口不出水 因为温控器发生了故障,会导致电热水器的热水口不会出水;也有可能是因为对温控器所设置的水的温度太低;当...
电视不小心按成蓝屏了无信号 家里是机顶盒装置的电视,可以按遥控器上面的“信源”按键进行设置,一般选择正确的输入路径即可。如果家里...
格兰仕空调本机按钮按了没反应 造成了这种情况可能是因为电源未接好或没电,电源导线插座接触不良,电源电压低,会导致空调无法正常使用,...
付费学员投资亏损,任泽平“不荐... 近期,一张“泽平宏观VIP群”学员亏损超千万的截图近日在网络流传,引发市场关注。截图显示,一名付费会...
韩国芯片“爆单”背后:离不开中... 韩国公布的最新数据显示,2026年6月该国半导体出口同比大增199.5%,达到448亿美元,占韩国出...