学习目标
掌握openGauss DBMS索引的管理:创建索引、删除索引、查询索引的信息、修改索引的信息。
课程作业
1.创建表,在表中创建索引
omm=# drop table if exists test;
NOTICE: table "test" does not exist, skipping
DROP TABLE
--创建测试表
omm=# 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
--创建索引
omm=# create index idx_test_testnum on test(testnum);
CREATE INDEX
--查看索引
omm=# \di
List of relations
Schema | Name | Type | Owner | Table | Storage
--------+------------------+-------+-------+-------+---------
public | idx_test_testnum | index | omm | test |
public | test_pkey | index | omm | test |
(2 rows)
omm=#
复制
2.通过hint使用索引
--测试准备,创建表customer,并插入数据
omm=# CREATE TABLE customer
omm-# (
omm(# ca_address_sk integer NOT NULL ,
omm(# ca_address_id character(16),
omm(# ca_street_number character(10) ,
omm(# ca_street_name character varying(60) ,
omm(# ca_street_type character(15) ,
omm(# ca_suite_number character(10) ,
omm(# ca_city character varying(60) ,
omm(# ca_county character varying(30) ,
omm(# ca_state character(2) ,
omm(# ca_zip character(10) ,
omm(# ca_country character varying(20) ,
omm(# ca_gmt_offset numeric(5,2) ,
omm(# ca_location_type character(20)
omm(# );
CREATE TABLE
--插入数据
omm=# insert into customer values
omm-# (1, 'AAAAAAAABAAAAAAA', '18', 'Jackson', 'Parkway', 'Suite 280', 'Fairfield', 'Maricopa County', 'AZ', '86192' ,'United States', -7.00, 'condo'),
omm-# (2, 'AAAAAAAACAAAAAAA', '362', 'Washington 6th', 'RD', 'Suite 80', 'Fairview', 'Taos County', 'NM', '85709', 'United States', -7.00, 'condo'),
omm-# (3, 'AAAAAAAADAAAAAAA', '585', 'Dogwood Washington', 'Circle', 'Suite Q', 'Pleasant Valley', 'York County', 'PA', '12477', 'United States', -5.00, 'single family');
INSERT 0 3
omm=#
omm=# omm=#
--创建索引
omm=# create index customer_idx on customer(ca_address_sk);
CREATE INDEX
omm=#
--通过hint强制使用索引,查看执行计划
omm-# EXPLAIN SELECT /*+ indexscan(customer customer_idx ) */
* FROM customer WHERE ca_address_sk<100;
QUERY PLAN
-------------------------------------------------------------------------------
[Bypass]
Index Scan using customer_idx on customer (cost=0.00..8.27 rows=1 width=788)
Index Cond: (ca_address_sk < 100)omm=#
(3 rows)
omm=#
复制
3.rename索引
omm=# ALTER INDEX idx_test_testnum RENAME TO idx_test_testnum_new;
ALTER INDEX
omm=# \di
List of relations
Schema | Name | Type | Owner | Table | Storage
--------+----------------------+-------+-------+----------+---------
public | customer_idx | index | omm | customer |
public | idx_test_testnum_new | index | omm | test |
public | test_pkey | index | omm | test |
(3 rows)
omm=#
复制
4.重建索引
omm=# omm=# --重建一个单独索引
ALTER INDEX idx_test_testnum_new REBUILD;
REINDEX
omm=#
omm=# REINDEX INDEX idx_test_testnum_new;
REINDEX
omm=# --重建所有索引
omm=# reindex table test;
REINDEX
omm=#
复制
5.移动索引到其他表空间
omm=# --将索引idx_test_testnum_new移动到表空间myindex_ts:
omm=# ALTER INDEX idx_test_testnum_new SET TABLESPACE myindex_ts;
ALTER INDEX
omm=#
omm=# --查看索引所在的表空间
omm=# select * from pg_indexes where tablename = 'test';
schemaname | tablename | indexname | tablespace |
indexdef
------------+-----------+----------------------+------------+--------------------------------------
-------------------------------------------------
public | test | test_pkey | | CREATE UNIQUE INDEX test_pkey ON test
USING btree (id) TABLESPACE pg_default
public | test | idx_test_testnum_new | myindex_ts | CREATE INDEX idx_test_testnum_new ON
test USING btree (testnum) TABLESPACE myindex_ts
(2 rows)
omm=# --或
omm=# select * from pg_indexes where indexname = 'idx_test_testnum_new';
schemaname | tablename | indexname | tablespace |
indexdef
------------+-----------+----------------------+------------+--------------------------------------
-------------------------------------------------
public | test | idx_test_testnum_new | myindex_ts | CREATE INDEX idx_test_testnum_new ON
test USING btree (testnum) TABLESPACE myindex_ts
(1 row)
omm=#
复制
6.删除索引
omm=# drop index idx_test_testnum_new;
DROP INDEX
omm=# \di
List of relations
Schema | Name | Type | Owner | Table | Storage
--------+--------------+-------+-------+----------+---------
public | customer_idx | index | omm | customer |
public | test_pkey | index | omm | test |
(2 rows)
omm=#
复制
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论

2年前

评论
相关阅读
2025年3月国产数据库大事记
墨天轮编辑部
891次阅读
2025-04-03 15:21:16
MogDB 发布更新,解决 openGauss 数据库在长事务情况下Ustore表膨胀问题
MogDB
288次阅读
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 版本体验:一主一备快速安装指南
孙莹
180次阅读
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适配操作系统自带的软件,不依赖三方库
来杯拿铁
76次阅读
2025-04-18 10:49:53
opengauss使用gs_probackup进行增量备份恢复
进击的CJR
70次阅读
2025-04-09 16:11:58
Postgresql数据库单个Page最多存储多少行数据
maozicb
62次阅读
2025-04-23 16:02:19
热门文章
oracle11g单实例ASM启动失败ORA-01078,ORA-29701
2021-12-30 1200浏览
openGauss每日一练第6天 | openGauss创建模式、修改模式属性和删除模式
2021-12-09 1112浏览
centos 7.6 升级openssl_1.1.1j和openssh_8.6p1
2021-12-31 1089浏览
openGauss每日一练第18天| openGauss逻辑结构:视图管理
2022-12-11 666浏览
openGauss每日一练第19天 |openGauss收集统计信息、打印执行计划、垃圾收集和checkpoint
2021-12-20 659浏览