一、学习目标
在普通表索引的基础上学习openGauss分区表的索引
二、索引(INDEX)
- 简介
索引是对数据库表中一列或多列的值进行排序的一种结构,使用索引可快速访问数据库表中的特定信息
三、openGauss基础操作
- openGauss连接
su - omm gsql -r
复制
- 查看openGauss表空间信息
// 显示表信息(包含索引) \d // 显示表更多信息 \d+ // 显示指定表更多信息 \d+ table_name
复制
四、openGauss普通表的索引操作
- 创建分区表索引
CREATE TABLESPACE example1 RELATIVE LOCATION 'tablespace1/tablespace_1'; CREATE TABLESPACE example2 RELATIVE LOCATION 'tablespace2/tablespace_2'; CREATE TABLESPACE example3 RELATIVE LOCATION 'tablespace3/tablespace_3'; CREATE TABLESPACE example4 RELATIVE LOCATION 'tablespace4/tablespace_4'; create schema tpcds; CREATE TABLE tpcds.customer_address_p1 ( 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 example1, PARTITION p3 VALUES LESS THAN (MAXVALUE) TABLESPACE example2 ); // 创建分区表索引ds_customer_address_p1_index1,不指定索引分区的名称 CREATE INDEX ds_customer_address_p1_index1 ON tpcds.customer_address_p1(CA_ADDRESS_SK) LOCAL; // 创建分区表索引ds_customer_address_p1_index2,并指定索引分区的名称 CREATE INDEX ds_customer_address_p1_index2 ON tpcds.customer_address_p1(CA_ADDRESS_SK) LOCAL ( PARTITION CA_ADDRESS_SK_index1, PARTITION CA_ADDRESS_SK_index2 TABLESPACE example3, PARTITION CA_ADDRESS_SK_index3 TABLESPACE example4 ); // 创建GLOBAL分区索引 CREATE INDEX ds_customer_address_p1_index3 ON tpcds.customer_address_p1(CA_ADDRESS_ID) GLOBAL; // 不指定关键字,默认创建GLOBAL分区索引 CREATE INDEX ds_customer_address_p1_index4 ON tpcds.customer_address_p1(CA_ADDRESS_ID);
复制
- 修改分区表索引
// 修改分区表索引CA_ADDRESS_SK_index2的表空间为example1 ALTER INDEX tpcds.ds_customer_address_p1_index2 MOVE PARTITION CA_ADDRESS_SK_index2 TABLESPACE example1; // 修改分区表索引CA_ADDRESS_SK_index3的表空间为example2 ALTER INDEX tpcds.ds_customer_address_p1_index2 MOVE PARTITION CA_ADDRESS_SK_index3 TABLESPACE example2; // 重命名索引 ALTER INDEX tpcds.ds_customer_address_p1_index2 RENAME PARTITION CA_ADDRESS_SK_index1 TO CA_ADDRESS_SK_index4;
复制
- 重建索引
// 重建一个单独索引 reindex index tpcds.ds_customer_address_p1_index1 PARTITION p1_ca_address_sk_idx; // 重建所有索引 reindex table tpcds.customer_address_p1 PARTITION p1;
复制
- 删除索引
DROP INDEX index_name
DROP INDEX tpcds.ds_customer_address_p1_index1; DROP INDEX tpcds.ds_customer_address_p1_index2; DROP INDEX tpcds.ds_customer_address_p1_index3; DROP INDEX tpcds.ds_customer_address_p1_index4;
复制
五、课后作业
- 创建范围分区表products, 为表创建分区表索引1,不指定索引分区的名称,创建分区表索引2,并指定索引分区的名称,创建GLOBAL分区索引3
// 创建表空间 create tablespace test1 relative location 'tablespace/tablespace_1'; create tablespace test2 relative location 'tablespace/tablespace_2'; // 创建模式 create schema tpcds; // 创建范围分区表 create table tpcds.products ( sum int, name char(30) ) partition by range(sum) ( partition p1 values less than (100), partition p2 values less than (200), partition p3 vlaues less than (300) ); // 创建分区表索引1,不指定索引分区的名称 CREATE INDEX index1 ON tpcds.products(sum) LOCAL; // 创建分区表索引2,并指定索引分区的名称 CREATE INDEX index2 ON tpcds.products(sum) LOCAL ( PARTITION sum_index1 tablespace test1, PARTITION sum_index2, PARTITION sum_index3 ); // 创建GLOBAL分区索引3 CREATE INDEX index3 ON tpcds.products(sum) GLOBAL;
复制
- 在分区表索引1上,修改分区表索引的表空间,重命名分区表索引
// 修改表空间 ALTER INDEX tpcds.index1 MOVE PARTITION sum_index1 TABLESPACE test2; // 重命名 ALTER INDEX tpcds.index1 RENAME PARTITION sum_index1 TO sum_index11;
复制
- 在分区表索引2上,重建单个索引分区和分区上的所有索引
// 重建单个索引 reindex index tpcds.index2 PARTITION p1_ca_address_sk_idx; // 重建所有索引 reindex table tpcds.products PARTITION p1;
复制
- 使用\d+、系统视图pg_indexes和pg_partition查看索引信息
\d+ tpcds.products select * from pg_indexes where tablename = 'products'; reindex table tpcds.customer_address_p1 PARTITION p1;
复制
- 删除索引、表和表空间
DROP INDEX tcpds.index_11; DROP INDEX tcpds.index_2; DROP INDEX tcpds.index_3; DROP table if exists tcpds.products; drop tablespace test1; drop tablespace test2;
复制
最后修改时间:2021-12-20 22:04:42
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
openGauss荣获中国软件行业协会多奖项,技术升级再创行业新高度
openGauss
549次阅读
2025-04-30 14:30:58
MogDB 发布更新,解决 openGauss 数据库在长事务情况下Ustore表膨胀问题
MogDB
307次阅读
2025-04-17 10:41:41
MogDB 发布更新,解决 openGauss 数据库在长事务情况下Ustore表膨胀问题
云和恩墨
201次阅读
2025-04-16 09:52:02
GitCode 成 openGauss 新归宿,国产开源数据库里程碑事件
严少安
172次阅读
2025-04-27 11:37:53
荣誉时刻!openGauss认证证书快递已发,快来看看谁榜上有名!
墨天轮小教习
162次阅读
2025-04-23 17:39:13
单个执行机并行执行MySQL到openGauss数据迁移子任务
Clipnosis
150次阅读
2025-04-30 16:39:58
openGauss6.0.0适配操作系统自带的软件,不依赖三方库
来杯拿铁
95次阅读
2025-04-18 10:49:53
Postgresql数据库单个Page最多存储多少行数据
maozicb
92次阅读
2025-04-23 16:02:19
openGauss新特性 | openGauss-DataVec向量数据库特性介绍
openGauss
65次阅读
2025-04-17 10:41:47
RISC-V 首迎 openGauss 7.0.0-RC1 全量版适配!数据库核心功能完整落地开源架构
openGauss
49次阅读
2025-04-16 10:33:59