资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

PG创建or修改索引

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

创新互联公司服务项目包括萨迦网站建设、萨迦网站制作、萨迦网页制作以及萨迦网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,萨迦网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到萨迦省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

 

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

 

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

 

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

由于PG的MVCC机制,当运行大量的更新操作后,会有索引膨胀的现象。这时候 可以通过 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工具进行定时的索引重建。

 


本文名称:PG创建or修改索引
文章路径:http://cdkjz.cn/article/jeisic.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220