第 17 天学习打卡,本节课程需掌握 openGauss DBMS 索引的管理:创建索引、删除索引、查询索引的信息、修改索引的信息。详细说明建议参考 openGauss 在线手册中索引的说明。
目录
作业
1. 创建表,在表中创建索引
-- 为表 test 的 testnum 列创建一个索引
su - omm
gsql -r
drop table if exists test;
create table test(id serial primary key,testnum serial);
create index idx_test_testnum on test(testnum);
-- 查看索引
\di

2. 通过 hint 使用索引
--测试准备,创建表 customer,并插入数据
CREATE TABLE customer
(
ca_address_sk integer NOT NULL ,
ca_address_id character(16),
ca_street_number character(10) ,
ca_street_name character varying(60) ,
ca_street_type character(15) ,
ca_suite_number character(10) ,
ca_city character varying(60) ,
ca_county character varying(30) ,
ca_state character(2) ,
ca_zip character(10) ,
ca_country character varying(20) ,
ca_gmt_offset numeric(5,2) ,
ca_location_type character(20)
);
insert into customer values
(1, 'AAAAAAAABAAAAAAA', '18', 'Jackson', 'Parkway', 'Suite 280', 'Fairfield', 'Maricopa County', 'AZ', '86192' ,'United States', -7.00, 'condo'),
(2, 'AAAAAAAACAAAAAAA', '362', 'Washington 6th', 'RD', 'Suite 80', 'Fairview', 'Taos County', 'NM', '85709', 'United States', -7.00, 'condo'),
(3, 'AAAAAAAADAAAAAAA', '585', 'Dogwood Washington', 'Circle', 'Suite Q', 'Pleasant Valley', 'York County', 'PA', '12477', 'United States', -5.00, 'single family');
-- 创建索引
create index customer_idx on customer(ca_address_sk);
-- 通过 hint 强制使用索引,查看执行计划
EXPLAIN SELECT /*+ indexscan(customer customer_idx ) */
* FROM customer WHERE ca_address_sk<100;

3. rename 索引
ALTER INDEX idx_test_testnum RENAME TO idx_test_testnum_new;

4. 重建索引
-- 重建一个单独索引
ALTER INDEX idx_test_testnum_new REBUILD;
REINDEX INDEX idx_test_testnum_new;
-- 重建所有索引
reindex table test;

5. 移动索引到其他表空间
-- 创建表空间 myindex_ts:
CREATE TABLESPACE myindex_ts RELATIVE LOCATION 'tablespace/myindex_ts1';
-- 将索引 idx_test_testnum_new 移动到表空间 myindex_ts:
ALTER INDEX idx_test_testnum_new SET TABLESPACE myindex_ts;
-- 查看索引所在的表空间
select * from pg_indexes where tablename = 'test';
-- 或
select * from pg_indexes where indexname = 'idx_test_testnum_new';

6. 删除索引
drop index idx_test_testnum_new;

命令
1. 创建索引
create index idx_test_testnum on test(testnum);
2. 查看索引
\di
3. 通过 hint 强制使用索引
EXPLAIN SELECT /*+ indexscan(customer customer_idx ) */
* FROM customer WHERE ca_address_sk<100;
4. 重命名索引
ALTER INDEX idx_test_testnum RENAME TO idx_test_testnum_new;
5. 移动索引至指定表空间
ALTER INDEX idx_test_testnum_new SET TABLESPACE myindex_ts;
6. 删除索引
drop index idx_test_testnum_new;
总结
本课内容学习了基本的索引操作,包括创建、查看、修改名称、修改表对应空间及删除。
周末愉快!
附录:历史打卡记录
openGauss 每日一练第 1 天|openGauss 数据库状态查看
openGauss 每日一练第 2 天|学习 openGauss 客户端工具 gsql 的使用
openGauss 每日一练第 3 天|openGauss 中一个数据库集簇对应多个数据库
openGauss 每日一练第 4 天|openGauss 中一个数据库可以被多个用户访问
openGauss 每日一练第 5 天|openGauss 中一个用户可以访问多个数据库
openGauss 每日一练第 6 天|openGauss 中用户一次只能连接到一个数据库
openGauss 每日一练第 7 天|openGauss 中一个数据库中可以创建多个模式
openGauss 每日一练第 8 天|openGauss 中一个数据库可以存储在多个表空间中
openGauss 每日一练第 9 天|openGauss 中一个表空间可以存储多个数据库
openGauss 每日一练第 10 天|openGauss 逻辑结构:表空间管理
openGauss 每日一练第 11 天|openGauss 逻辑结构:数据库管理
openGauss 每日一练第 12 天|openGauss 逻辑结构:模式管理
openGauss 每日一练第 13 天|openGauss 逻辑结构:表管理 1
openGauss 每日一练第 14 天|openGauss 逻辑结构:表管理 2
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




