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

 


相关内容

热门资讯

老人在直播间疯狂刷火箭 周佳欣、郑凯丹、朱娅、蒋婉、黄铃、王佳、李婉晴/文64岁的王雅达又往直播间里刷了一枚“火箭”。屏幕上...
汉坦病毒阳性乘客在乌斯怀亚港登... 自世卫组织(WHO)5月6日公布荷兰泛海探险公司(Oceanwide Expeditions)旗下极...
冰箱不制冷的原因有哪几种 冰箱的电压在正常的情况下,压缩机没有正常的运转,而且有噪声和旋转的声音,压缩机一旦出现故障,冰箱就会...
电视有哪几种方法 电视是我们生活中非常重要的一种媒介,它可以为我们带来大量的信息、乐趣和娱乐。在现代社会中,电视的使用...
装修风格有哪几种(今年流行的装... 摘要:装修的方式有很多种,下面这些就是我们在装修中常用的一种。①现代风,也叫功能派风,这个装修风格的...
大红鹰瓷砖哪里生产的 最佳回答 大红鹰瓷砖是广东佛山非常有名的一个瓷砖老品牌,据说这个品牌的瓷砖是在1974年成立的,老一...
走廊墙面怎么装饰 走廊的装修一定要简洁,因为是每天都会路过的地方,一般走廊装修都只是刷一个白墙或者是对走廊的颜色进行处...
苏巧慧阵营影射李四川家族涉黑,... 海峡导报综合报道 新北市长选战硝烟渐起,身处苏巧慧阵营的新北市议员翁震州发文质疑李四川家族在小琉球做...
红场阅兵后,普京同军官握手致意 据凤凰卫视报道,5月9日,俄罗斯胜利日阅兵接近尾声,普京与军官一一握手致意。
车间里走出“准工程师”——黄河... 3500元项目津贴,三个真实项目研发经历,一份实习期月薪8000多元的录用通知——这是工学部2022...