学习目标
掌握openGauss DBMS索引的管理:创建索引、删除索引、查询索引的信息、修改索引的信息。
课程作业
1.创建表,在表中创建索引
--在testnum列上创建索引
newdb1=# create table test(id serial primary key,testnum serial);
NOTICE: CREATE TABLE will create implicit sequence "test_id_seq" for serial column "test.id"
NOTICE: CREATE TABLE will create implicit sequence "test_testnum_seq" for serial column "test.testnum"
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "test_pkey" for table "test"
CREATE TABLE
newdb1=# create index idx_test_testnum on test(testnum);
CREATE INDEX
newdb1=# \di
List of relations
Schema | Name | Type | Owner | Table | Storage
--------+------------------+-------+-------+-------+---------
testsm | idx_test_testnum | index | omm | test |
testsm | test_pkey | index | omm | test |
(2 rows)
复制
2.通过hint使用索引
--插入数据
newdb1=# insert into test values (1,101);
INSERT 0 1
newdb1=# insert into test values (2,201);
INSERT 0 1
newdb1=# insert into test values (3,301);
INSERT 0 1
newdb1=# insert into test values (4,401);
--hint 索引
newdb1=# explain select /*+indexscan(test idx_test_testnum) */ * from test where testnum > 1;
QUERY PLAN
--------------------------------------------------------------------------------
[Bypass]
Index Scan using idx_test_testnum on test (cost=0.00..56.78 rows=716 width=8)
Index Cond: (testnum > 1)
(3 rows)
复制
3.rename索引
^
newdb1=# alter index idx_test_testnum rename to idx_test_testnum2;
ALTER INDEX
newdb1=# \di
List of relations
Schema | Name | Type | Owner | Table | Storage
--------+-------------------+-------+-------+----------+---------
testsm | customer_idx | index | omm | customer |
testsm | idx_test_testnum2 | index | omm | test |
(2 rows)
newdb1=#
复制
4.重建索引
newdb1=# reindex index idx_test_testnum2;
REINDEX
复制
5.移动索引到其他表空间
--从默认表空间移出到其他表空间
newdb1=# select * from pg_indexes where indexname = 'idx_test_testnum2';
schemaname | tablename | indexname | tablespace | indexdef
------------+-----------+-------------------+------------+---------------------------------------------------------------------------------
testsm | test | idx_test_testnum2 | | CREATE INDEX idx_test_testnum2 ON test USING btree (testnum) TABLESPACE newtbs1
(1 row)
newdb1=# \db
List of tablespaces
Name | Owner | Location
------------+-------+------------------------
enmtbs | omm | tablespace/enmtbs_ts1
music_tbs | omm | tablespace/test_ts1
newtbs1 | omm | tablespace/newtbs1_ts1
pg_default | omm |
pg_global | omm |
(5 rows)
newdb1=# ALTER INDEX idx_test_testnum2 SET TABLESPACE enmtbs;
ALTER INDEX
newdb1=# select * from pg_indexes where indexname = 'idx_test_testnum2';
schemaname | tablename | indexname | tablespace | indexdef
------------+-----------+-------------------+------------+--------------------------------------------------------------------------------
testsm | test | idx_test_testnum2 | enmtbs | CREATE INDEX idx_test_testnum2 ON test USING btree (testnum) TABLESPACE enmtbs
(1 row)
复制
6.删除索引
newdb1=# drop index idx_test_testnum2
newdb1-# ;
DROP INDEX
复制
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论

2年前

评论
相关阅读
2025年3月国产数据库大事记
墨天轮编辑部
895次阅读
2025-04-03 15:21:16
MogDB 发布更新,解决 openGauss 数据库在长事务情况下Ustore表膨胀问题
MogDB
289次阅读
2025-04-17 10:41:41
openGauss 7.0.0-RC1 版本正式发布!
Gauss松鼠会
207次阅读
2025-04-01 12:27:03
MogDB 发布更新,解决 openGauss 数据库在长事务情况下Ustore表膨胀问题
云和恩墨
189次阅读
2025-04-16 09:52:02
openGauss 7.0.0-RC1 版本体验:一主一备快速安装指南
孙莹
181次阅读
2025-04-01 10:30:07
鲲鹏RAG一体机解决方案正式发布 openGauss DataVec向量数据库助力DeepSeek行业应用
Gauss松鼠会
124次阅读
2025-03-31 10:00:29
荣誉时刻!openGauss认证证书快递已发,快来看看谁榜上有名!
墨天轮小教习
113次阅读
2025-04-23 17:39:13
openGauss6.0.0适配操作系统自带的软件,不依赖三方库
来杯拿铁
77次阅读
2025-04-18 10:49:53
GitCode 成 openGauss 新归宿,国产开源数据库里程碑事件
严少安
70次阅读
2025-04-27 11:37:53
opengauss使用gs_probackup进行增量备份恢复
进击的CJR
70次阅读
2025-04-09 16:11:58