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 工具进行定时的索引重建。

 


相关内容

热门资讯

官方回应博主浦东机场打车遭抛客 7月25日,知名博主“花式足球克里斯”世界杯观赛后回国,在浦东机场打车,遭遇出租车司机谎称车辆故障中...
女孩正颌手术被做反,主导医生称... 近日,武汉。24岁女孩小雨仅2毫米轻微地包天,本可正畸改善,却被建议做正颌手术,原定下颌后移2毫米,...
台风“红霞”逼近,揭阳全力筑牢... 7月25日9时,省防汛防旱防风总指挥部将防风Ⅲ级应急响应提升为防风Ⅱ级应急响应。台风“红霞”正以25...
原创 王... 王虹夺得数学界的诺贝尔奖——菲尔兹奖。她的导师、母校、同学等等,都成为了全网讨论的对象。 而她的导师...
电池技术对二手车残值影响大吗? 随着新能源汽车市场的迅猛扩张,潜在购车者在关注新车性能的同时,也越来越重视后续流通环节的价值表现。其...
韩国一监狱百发子弹下落不明,已... 【环球网报道 记者 姜蔼玲】据韩联社援引韩国法律界7月26日消息,针对大田一所监狱100发子弹下落不...
Anthropic发布Opus... Anthropic正式发布Claude Opus 5人工智能模型。官方宣称,该模型在智能水平接近前沿...
从菲尔兹奖看广东的“育才密码” □李妹妍 7月23日,2026年国际数学家大会传来喜讯,中国籍学者邓煜、王虹双双摘得国际数学界最高荣...
神舟二十三号乘组太空之旅满两月... 神二十三号乘组朱杨柱、张志远、黎家盈在轨已满两月,空间站内各项科学实(试)验进展顺利。上周,三名航天...
紧张关系进一步升级,巴西外交部... 【环球网报道】美国《纽约时报》当地时间25日报道称,据美巴两国官员透露,巴西政府阻止了一个美国代表团...