第十天学习openGauss分区表索引的创建,修改,重建,删除。
连接opengauss
su - omm gsql -r
1.创建范围分区表products, 为表创建分区表索引1,不指定索引分区的名称,创建分区表索引2,并指定索引分区的名称,创建global分区索引3
create tablespace tbs1 relative location 'tablespace1/tbs1';
create tablespace tbs2 relative location 'tablespace1/tbs2';
create tablespace tbs3 relative location 'tablespace1/tbs3';
create tablespace tbs4 relative location 'tablespace1/tbs4';
create schema schema1;
create table schema1.products
(
ca_address_sk integer not null,
ca_address_id char(16) not null,
ca_street_number char(10) ,
ca_street_name varchar(60) ,
ca_street_type char(15) ,
ca_suite_number char(10) ,
ca_city varchar(60) ,
ca_county varchar(30) ,
ca_state char(2) ,
ca_zip char(10) ,
ca_country varchar(20) ,
ca_gmt_offset decimal(5,2) ,
ca_location_type char(20)
)
partition by range(ca_address_sk)
(
partition p1 values less than (3000),
partition p2 values less than (5000) tablespace tbs1,
partition p3 values less than (maxvalue) tablespace tbs2
);
--创建分区表索引idx_products_index1,不指定索引分区的名称
create index idx_products_index1 on schema1.products(ca_address_sk) local;
--创建分区表索引idx_products_index2,并指定索引分区的名称
create index idx_products_index2 on schema1.products(ca_address_sk) local
(
partition ca_address_sk_index1,
partition ca_address_sk_index2 tablespace tbs1,
partition ca_address_sk_index3 tablespace tbs2
);
--创建global分区索引
create index idx_products_index3 on schema1.products(ca_address_id) global;
--不指定关键字,默认创建global分区索引
create index idx_products_index4 on schema1.products(ca_address_id);
2.在分区表索引1上,修改分区表索引的表空间,重命名分区表索引
alter index schema1.idx_products_index1 move partition p1_ca_address_sk_idx tablespace tbs1;
alter index schema1.idx_products_index1 rename partition p1_ca_address_sk_idx to p1_ca_address_sk_idx1;
3.在分区表索引2上,重建单个索引分区和分区上的所有索引
–重建单个索引分区 reindex index schema1.idx_products_index2 partition ca_address_sk_index1; –重建分区上的所有索引 reindex table schema1.products partition p1;
4.使用\d+、系统视图pg_indexes和pg_partition查看索引信息
\d+ schema1.products;
select * from pg_indexes where tablename = 'products';
select * from pg_partition;
5.删除索引、表和表空间
drop index schema1.idx_products_index1;
drop index schema1.idx_products_index2;
drop index schema1.idx_products_index3;
drop index schema1.idx_products_index4;
drop table schema1.products;
drop tablespace tbs1;
drop tablespace tbs2;
drop tablespace tbs3;
drop tablespace tbs4;
上面实际操作截图如下:






通过以上实操,学习到openGauss分区表索引的相关操作。了解了索引是对数据库表中一列或多列的值进行排序的一种结构,使用索引可快速访问数据库表中的特定信息。
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




