openGauss 每日一练第 10 天打卡,我来了!又可以学习,真开心~

学习

今天第 10 课,学习openGauss分区表索引!

索引是对数据库表中一列或多列的值进行排序的一种结构,使用索引可快速访问数据库表中的特定信息。

课后作业打卡

1.创建范围分区表products, 为表创建分区表索引1,不指定索引分区的名称,创建分区表索引2,并指定索引分区的名称,创建GLOBAL分区索引3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
--创建分区表 products
CREATE TABLE products
(
id int,
name CHAR(20)
)
partition by range (id)
(
partition products_p0 values less than (50),
partition products_p1 values less than (100),
partition products_p2 values less than (150),
partition products_p3 values less than (200),
partition products_p4 values less than (250)
);

1
2
3
4
5
6
7
8
9
10
11
12
13
-- 创建分区表索引1
create index products_p1_index1 ON products(id) LOCAL;
-- 创建分区表索引2
create index products_p1_index2 ON products(id) LOCAL
(
PARTITION id_index1,
PARTITION id_index2 TABLESPACE example3,
PARTITION id_index3 TABLESPACE example4,
PARTITION id_index4 TABLESPACE example3,
PARTITION id_index5 TABLESPACE example4
);
-- 创建GLOBAL分区索引3
create index products_p1_index3 ON products(name) GLOBAL;

2.在分区表索引1上,修改分区表索引的表空间,重命名分区表索引

1
2
alter index products_p1_index2 move partition id_index1 tablespace example3;
alter index products_p1_index2 rename partition id_index1 to id_index0;

3.在分区表索引1上,重建单个索引分区和分区上的所有索引

1
2
3
reindex index products_p1_index1 partition products_p1_id_idx;
reindex index products_p1_index1;
reindex table products partition products_p0;

4.使用\d+、系统视图pg_indexes和pg_partition查看索引信息

1
2
3
\d+ products
select * from pg_indexes where tablename = 'products';
select * from pg_partition;

5.删除索引、表和表空间

1
2
3
drop index products_p1_index1;
drop index products_p1_index2;
drop index products_p1_index3;

写在最后

今天的作业打卡结束!🎉