学习目标
掌握openGauss DBMS索引的管理:创建索引、删除索引、查询索引的信息、修改索引的信息。
课程学习
索引是一个指向表中数据的指针。一个数据库中的索引与一本书的索引目录是非常相似的。
索引可以用来提高数据库查询性能,但是不恰当的使用将导致数据库性能下降。
课程作业
1.创建表,在表中创建索引
root@modb:~# su - omm omm@modb:~$ gsql -r failed to connect Unknown:5432. omm@modb:~$ gsql -r gsql ((openGauss 3.0.0 build 02c14696) compiled at 2022-04-01 18:12:00 commit 0 last mr ) Non-SSL connection (SSL connection is recommended when requiring high-security) Type "help" for help. 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)
复制
2.通过hint使用索引
omm=# 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) ); CREATE TABLE omm=# 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', '85709', 'United States', -7.00, 'condo'), ounty', 'PA', '12477', 'United States', -5.00, 'single family'); INSERT 0 3 omm=# create index customer_idx on customer(ca_address_sk); CREATE INDEX 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) (3 rows)
复制
3.rename索引
omm=# ALTER INDEX idx_test_testnum RENAME TO idx_test_testnum_new; ALTER INDEX
复制
4.重建索引
omm=# ALTER INDEX idx_test_testnum_new REBUILD; REINDEX omm=# REINDEX INDEX idx_test_testnum_new; REINDEX omm=# reindex table test; REINDEX
复制
5.移动索引到其他表空间
omm=# CREATE TABLESPACE myindex_ts RELATIVE LOCATION 'tablespace/myindex_ts1'; CREATE TABLESPACE omm=# ALTER INDEX idx_test_testnum_new SET TABLESPACE myindex_ts; ALTER INDEX omm=# select * from pg_indexes where tablename = 'test'; schemaname | tablename | indexname | tablespace | indexd ef ------------+-----------+----------------------+------------+-------------- 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 US ING btree (testnum) TABLESPACE myindex_ts (2 rows)
复制
6.删除索引
omm=# drop index idx_test_testnum_new; DROP INDEX
复制
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
2025年3月国产数据库大事记
墨天轮编辑部
484次阅读
2025-04-03 15:21:16
内蒙古公司成功完成新一代BOSS云原生系统割接上线
openGauss
191次阅读
2025-03-24 09:40:40
第4期 openGauss 中级认证OGCP直播班招生中!3月30日开课
墨天轮小教习
154次阅读
2025-03-17 15:48:40
openGauss 7.0.0-RC1 版本正式发布!
Gauss松鼠会
124次阅读
2025-04-01 12:27:03
openGauss 7.0.0-RC1 版本体验:一主一备快速安装指南
孙莹
100次阅读
2025-04-01 10:30:07
从数据库源码比较 PostgreSql和OpenGauss的启动过程
maozicb
75次阅读
2025-03-24 15:55:04
一文快速上手openGauss
进击的CJR
71次阅读
2025-03-26 16:12:54
openGauss HASH JOIN原理
lbsswhu
58次阅读
2025-03-18 10:45:01
openGauss 学习之路:集群部署实战探索
openGauss
49次阅读
2025-03-21 10:34:13
openGauss问题记录:开启备机归档且备机stop情况下,执行gs_probackup失败
zym
42次阅读
2025-03-18 19:06:13