第17天-openGauss逻辑结构:索引管理
学习目标
掌握openGauss DBMS索引的管理:创建索引、删除索引、查询索引的信息、修改索引的信息。
课程学习
索引是一个指向表中数据的指针。一个数据库中的索引与一本书的索引目录是非常相似的。
索引可以用来提高数据库查询性能,但是不恰当的使用将导致数据库性能下降。
- 创建索引
--为表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
- 通过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;
- rename索引
ALTER INDEX idx_test_testnum RENAME TO idx_test_testnum_new;
- 重建索引
--重建一个单独索引
ALTER INDEX idx_test_testnum_new REBUILD;
REINDEX INDEX idx_test_testnum_new;
--重建所有索引
reindex table test;
- 移动索引到其他表空间
--创建表空间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';
- 删除索引
--执行下面的命令,删除表test上的索引idx_test_testnum_new:
drop index idx_test_testnum_new;
课程作业
- 创建表,在表中创建索引
#建表
enmdb=# drop table if exists test;
NOTICE: table "test" does not exist, skipping
DROP TABLE
enmdb=# create table test(
enmdb(# id serial primary key,
enmdb(# num serial,
enmdb(# name varchar(10)
enmdb(# );
NOTICE: CREATE TABLE will create implicit sequence "test_id_seq" for serial column "test.id"
NOTICE: CREATE TABLE will create implicit sequence "test_num_seq" for serial column "test.num"
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "test_pkey" for table "test"
CREATE TABLE
enmdb=# insert into test(name) values('aaa');
INSERT 0 1
enmdb=# insert into test(name) values('bbb');
INSERT 0 1
enmdb=# insert into test(name) values('ccc');
INSERT 0 1
enmdb=# select * from test;
id | num | name
----+-----+------
1 | 1 | aaa
2 | 2 | bbb
3 | 3 | ccc
(3 rows)
#创建索引
enmdb=# create index idx_test_num on test(num);
CREATE INDEX
enmdb=#
#查看索引
enmdb=# \di
enmdb=# List of relations
Schema | Name | Type | Owner | Table | Storage
--------+--------------+-------+-------+-------+---------
public | idx_test_num | index | omm | test |
public | test_pkey | index | omm | test |
(2 rows)
- 通过hint使用索引
enmdb=# explain select * from test where num < 100 and id < 100;
(5 rows)
QUERY PLAN
-----------------------------------------------------------------------------
Bitmap Heap Scan on test (cost=6.96..22.33 rows=119 width=46)
Recheck Cond: (num < 100)
Filter: (id < 100)
-> Bitmap Index Scan on idx_test_num (cost=0.00..6.94 rows=358 width=0)
Index Cond: (num < 100)
#加hint, 跟oracle的hint非常相似
enmdb=# explain select /*+ indexscan(test test_pkey) */ * from test where num < 100 and id < 100;
QUERY PLAN
--------------------------------------------------------------------------
Index Scan using test_pkey on test (cost=0.00..51.41 rows=119 width=46)
Index Cond: (id < 100)
Filter: (num < 100)
(3 rows)
- rename索引
enmdb=# alter index idx_test_num rename to idx_test_num_new;
ALTER INDEX
#查看索引
enmdb=# \di
List of relations
Schema | Name | Type | Owner | Table | Storage
--------+------------------+-------+-------+-------+---------
public | idx_test_num_new | index | omm | test |
public | test_pkey | index | omm | test |
(2 rows)
- 重建索引
#重建指定索引,给出索引名,命令1
enmdb=# alter index idx_test_num_new rebuild;
REINDEX
#重建指定索引,给出索引名,命令2
enmdb=# reindex index idx_test_num_new;
REINDEX
#重建表上所有的索引,给出表名
enmdb=# reindex table test;
REINDEX
enmdb=# \di
List of relations
Schema | Name | Type | Owner | Table | Storage
--------+------------------+-------+-------+-------+---------
public | idx_test_num_new | index | omm | test |
public | test_pkey | index | omm | test |
(2 rows)
- 移动索引到其他表空间
enmdb=# create tablespace index_tbs relative location 'tablespace/indx_tbs1';
CREATE TABLESPACE
enmdb=# alter index idx_test_num_new set tablespace index_tbs;
ALTER INDEX
#查看索引所在的表空间, tablespace 空表示使用默认表空间,即跟表所占表空间一致,从indexdef列中的语句可以看出
enmdb=# 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 enmtbs
public | test | idx_test_num_new | index_tbs | CREATE INDEX idx_test_num_new ON test USING btree (num) TABLESPACE index_tbs
(2 rows)
- 删除索引
enmdb=# drop index idx_test_num_new;
DROP INDEX
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




