暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

openGauss每日一练第10天 | 创建、修改和删除分区表索引

原创 YAN左使 2021-12-31
1257

openGauss每日一练第10天的学习笔记!

  • 学习目标:学习openGauss分区表索引。
  • 实验环境:openGuass 2.0.0,实验环境使用由墨天轮提供的数据库线上环境,非常方便!

以omm用户连接openGauss数据库:

su - omm gsql -r

image.png
omm用户是openGauss数据库安装和使用过程一个非常重要的用户。关于omm用户,请参考:

  • 了解安装用户及用户组,其中提到了两个重要的用户:omm为数据库管理员(也是运行openGauss的操作系统用户),dbgrp为运行openGauss的操作系统用户的群组名。在安装openGauss过程中运行“gs_install”时,会创建与安装用户同名的数据库用户,即数据库用户omm。此用户具备数据库的最高操作权限,此用户初始密码由用户指定。
  • 执行安装,安装openGauss的过程,需要确保omm用户需要拥有安装包所在目录及子目录的权限。安装脚本gs_install必须以前置脚本中指定的omm执行,否则,脚本执行会报错。

1.创建范围分区表和分区表索引

创建范围分区表products, 为表创建分区表索引1,不指定索引分区的名称,创建分区表索引

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 TABLE product_t ( p_product_id integer, p_product_name char(30), p_product_category char(20) ) partition by range (p_product_id) ( partition product_t_p1 values less than (50), partition product_t_p2 values less than (100) TABLESPACE example1, partition product_t_p3 values less than (150) TABLESPACE example2 );

image.png

CREATE INDEX product_t_index1 ON product_t(p_product_id) LOCAL; CREATE INDEX product_t_index2 ON product_t(p_product_id) LOCAL ( PARTITION p_product_id_index1, PARTITION p_product_id_index2 TABLESPACE example3, PARTITION p_product_id_index3 TABLESPACE example4 ); CREATE INDEX product_t_index3 ON product_t(p_product_name) GLOBAL;

image.png

2,并指定索引分区的名称,创建GLOBAL分区索引3

2.修改索引属性

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

ALTER INDEX product_t_index2 MOVE PARTITION p_product_id_index2 TABLESPACE example1; ALTER INDEX product_t_index2 RENAME PARTITION p_product_id_index1 TO p_product_id_index1_1;

image.png

3.重建索引

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

reindex index product_t_index2 PARTITION product_t_idx; reindex table product_t PARTITION p1;

image.png

4.查看索引信息

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

\d+ product_t select * from pg_indexes where tablename = 'product_t'; select * from pg_partition;

image.png
image.png
image.png

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

DROP INDEX product_t_index1; DROP INDEX product_t_index2; DROP INDEX product_t_index3; DROP TABLE product_t; DROP TABLESPACE IF EXISTS example1; DROP TABLESPACE IF EXISTS example2; DROP TABLESPACE IF EXISTS example3; DROP TABLESPACE IF EXISTS example4;

image.png

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论