openGauss逻辑结构-索引管理
1.创建表,在表中创建索引
--创建表testtab01
omm=# create table testtab01(id serial primary key ,testnum serial);
NOTICE: CREATE TABLE will create implicit sequence "testtab01_id_seq" for serial column "testtab01.id"
NOTICE: CREATE TABLE will create implicit sequence "testtab01_testnum_seq" for serial column "testtab01.testnum"
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "testtab01_pkey" for table "testtab01"
CREATE TABLE
--创建索引
omm=# create index idx_testtab01_testnum on testtab01(testsum);
ERROR: column "testsum" does not exist
omm=# create index idx_testtab01_testnum on testtab01(testnum);
CREATE INDEX
--查看索引
omm=# \di
List of relations
Schema | Name | Type | Owner | Table | Storage
--------+-----------------------+-------+-------+-----------+---------
public | idx_testtab01_testnum | index | omm | testtab01 |
public | testtab01_pkey | index | omm | testtab01 |
(2 rows)
2.通过hint使用索引
--创建测试表
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);
--执行计划
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_testtab01_testnum rename to idx_testtab01_testnum01;
ALTER INDEX
4.重建索引
omm=# alter index idx_testtab01_testnum01 rebuild;
REINDEX
omm=# reindex index idx_testtab01_testnum01 ;
REINDEX
omm=# reindex table testtab01;
REINDEX
5.移动索引到其他表空间
--创建测试index
omm=# create tablespace myindex_ts relative location 'tablespace/myindex_ts1';
CREATE TABLESPACE
--修改索引的表空间
omm=# alter index idx_testtab01_testnum01 set tablespace myindex_ts;
ALTER INDEX
--查看索引所在表空间
omm=# select * from pg_indexes where tablename='testtab01';
schemaname | tablename | indexname | tablespace | indexdef
------------+-----------+-------------------------+------------+---------------------------------------------------------------------------------
--------------
public | testtab01 | testtab01_pkey | | CREATE UNIQUE INDEX testtab01_pkey ON testtab01 USING btree (id) TABLESPACE pg_d
efault
public | testtab01 | idx_testtab01_testnum01 | myindex_ts | CREATE INDEX idx_testtab01_testnum01 ON testtab01 USING btree (testnum) TABLESPA
CE myindex_ts
(2 rows)
--查看索引所在表空间
omm=# select * from pg_indexes where indexname='idx_testtab01_testnum01';
schemaname | tablename | indexname | tablespace | indexdef
------------+-----------+-------------------------+------------+---------------------------------------------------------------------------------
--------------
public | testtab01 | idx_testtab01_testnum01 | myindex_ts | CREATE INDEX idx_testtab01_testnum01 ON testtab01 USING btree (testnum) TABLESPA
CE myindex_ts
(1 row)
6.删除索引
omm=# drop index idx_testtab01_testnum01;
DROP INDEX
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




