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

openGauss每日一练第9天 | 创建、修改和删除普通表索引

原创 YAN左使 2021-12-31
2336

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

  • 学习目标:学习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, 分别为表创建一个unique索引1,指定b-tree索引2和表达式索引3

CREATE TABLE product_t ( p_product_id integer, p_product_name char(30), p_product_category char(20) ) ; CREATE UNIQUE INDEX product_t_index1 ON product_t(p_product_id); CREATE INDEX product_t_index2 ON product_t USING btree(p_product_id); CREATE INDEX product_t_index3 ON product_t(SUBSTR(p_product_name,1 ,4)); \d+ product_t select * from pg_indexes where tablename = 'product_t';

image.png
image.png
image.png

2.修改索引属性

设置索引1不可用,修改索引2的表空间,重命名索引3

ALTER INDEX product_t_index1 UNUSABLE; CREATE TABLESPACE example0 RELATIVE LOCATION 'tablespace1/tablespace_0'; ALTER INDEX product_t_index2 SET tablespace example0; ALTER INDEX product_t_index3 RENAME TO product_t_index3_1; \d+ product_t select * from pg_indexes where tablename = 'product_t';

image.png
image.png

3.重建索引

重建索引2和products的所有索引

ALTER INDEX product_t_index2 REBUILD; REINDEX TABLE product_t;

image.png

4.查看索引信息

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

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

image.png

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

DROP INDEX product_t_index1; DROP INDEX product_t_index2; DROP INDEX product_t_index3_1; DROP TABLE product_t; DROP TABLESPACE IF EXISTS example0;

image.png

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

评论