打卡第十七天,索引管理
创建索引
create index idx_test_testnum on test(testnum);
复制
通过hint使用索引
EXPLAIN SELECT /*+ indexscan(customer customer_idx ) */
* FROM customer WHERE ca_address_sk<100;
复制
rename索引
ALTER INDEX idx_test_testnum RENAME TO idx_test_testnum_new;
复制
重建索引 (语法比oracle 多)
--重建一个单独索引
ALTER INDEX idx_test_testnum_new REBUILD;
REINDEX INDEX idx_test_testnum_new;
--重建所有索引
reindex table test;
复制
移动索引到其他表空间
ALTER INDEX idx_test_testnum_new SET TABLESPACE myindex_ts;
复制
删除索引
drop index idx_test_testnum_new;
复制
课程作业
1.创建表,在表中创建索引
omm=# create table t (id serial,col char(20));
NOTICE: CREATE TABLE will create implicit sequence "t_id_seq" for serial column "t.id"
CREATE TABLE
omm=# create index idx_col on t(col);
CREATE INDEX
omm=# \di idx_col
List of relations
Schema | Name | Type | Owner | Table | Storage
--------+---------+-------+-------+-------+---------
public | idx_col | index | omm | t |
(1 row)
复制
2.通过hint使用索引
omm=# explain select /*+ indexscan(t idx_col) */ * from t where col='1';
omm=# QUERY PLAN
-------------------------------------------------------------------
[Bypass]
Index Scan using idx_col on t (cost=0.00..16.30 rows=3 width=88)
Index Cond: (col = '1'::bpchar)
(3 rows)
复制
3.rename索引
omm=# alter index idx_col rename to idx_cc;
ALTER INDEX
复制
4.重建索引
omm=# alter index idx_cc rebuild;
REINDEX
omm=# rebuil index idx_cc index idx_cc index idx_cc;^C
omm=# reindex index idx_cc ;
REINDEX
omm=# reindex table t;
REINDEX
omm=#
复制
5.移动索引到其他表空间
omm=# select * from pg_indexes where indexname='idx_cc';
schemaname | tablename | indexname | tablespace | indexdef
------------+-----------+-----------+------------+----------------------------------------------------------------
--
public | t | idx_cc | | CREATE INDEX idx_cc ON t USING btree (col) TABLESPACE pg_defaul
t
(1 row)
omm=# alter index idx_cc set tablespace tbs_t;
ALTER INDEX
omm=# select * from pg_indexes where indexname='idx_cc';
schemaname | tablename | indexname | tablespace | indexdef
------------+-----------+-----------+------------+-------------------------------------------------------------
public | t | idx_cc | tbs_t | CREATE INDEX idx_cc ON t USING btree (col) TABLESPACE tbs_t
(1 row)
复制
6.删除索引
omm=# drop index idx_cc ;
DROP INDEX
复制
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论

2年前

评论
相关阅读
2025年3月国产数据库大事记
墨天轮编辑部
605次阅读
2025-04-03 15:21:16
内蒙古公司成功完成新一代BOSS云原生系统割接上线
openGauss
201次阅读
2025-03-24 09:40:40
第4期 openGauss 中级认证OGCP直播班招生中!3月30日开课
墨天轮小教习
162次阅读
2025-03-17 15:48:40
openGauss 7.0.0-RC1 版本正式发布!
Gauss松鼠会
147次阅读
2025-04-01 12:27:03
openGauss 7.0.0-RC1 版本体验:一主一备快速安装指南
孙莹
128次阅读
2025-04-01 10:30:07
从数据库源码比较 PostgreSql和OpenGauss的启动过程
maozicb
86次阅读
2025-03-24 15:55:04
一文快速上手openGauss
进击的CJR
80次阅读
2025-03-26 16:12:54
openGauss HASH JOIN原理
lbsswhu
61次阅读
2025-03-18 10:45:01
openGauss 学习之路:集群部署实战探索
openGauss
55次阅读
2025-03-21 10:34:13
openGauss问题记录:开启备机归档且备机stop情况下,执行gs_probackup失败
zym
45次阅读
2025-03-18 19:06:13