暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

openGauss每日一练第 17 天 | openGauss 3.0.0数据库在线实训课程

原创 shunwah 2022-12-10
773

作者:马顺华

从事运维管理工作多年,目前就职于某科技有限公司,熟悉运维自动化、OceanBase部署运维、MySQL 运维以及各种云平台技术和产品。并已获得OceanBase认证OBCA、OBCP证书。

第17天 | openGauss逻辑结构:索引管理

学习目标

掌握openGauss DBMS索引的管理:创建索引、删除索引、查询索引的信息、修改索引的信息。

课程学习

索引是一个指向表中数据的指针。一个数据库中的索引与一本书的索引目录是非常相似的。
索引可以用来提高数据库查询性能,但是不恰当的使用将导致数据库性能下降。

1.创建索引

–为表test的testnum列创建一个索引

root@modb:~# 
root@modb:~# su - omm
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
omm=# 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=# 

image.png

–查看索引
\di

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)

image.png

2.通过hint使用索引

–测试准备,创建表customer,并插入数据

omm=# CREATE TABLE customer
omm-# (
omm(# ca_address_sk integer NOT NULL ,
omm(# ca_address_id character(16),
omm(# ca_street_number character(10) ,
omm(# ca_street_name character varying(60) ,
omm(# ca_street_type character(15) ,
omm(# ca_suite_number character(10) ,
omm(# ca_city character varying(60) ,
omm(# ca_county character varying(30) ,
omm(# ca_state character(2) ,
omm(# ca_zip character(10) ,
omm(# ca_country character varying(20) ,
omm(# omm(# ca_location_type character(20)
omm(# );ca_gmt_offset numeric(5,2) ,

CREATE TABLE
omm=# 
omm=# insert into customer values
omm-# (1, 'AAAAAAAABAAAAAAA', '18', 'Jackson', 'Parkway', 'Suite 280', 'Fairfield', 'Maricopa County', 'AZ', '86192' ,'United States', -7.00, 'condo'),
omm-# (2, 'AAAAAAAACAAAAAAA', '362', 'Washington 6th', 'RD', 'Suite 80', 'Fairview', 'Taos County', 'NM', '85709', 'United States', -7.00, 'condo'),
omm-# (3, 'AAAAAAAADAAAAAAA', '585', 'Dogwood Washington', 'Circle', 'Suite Q', 'Pleasant Valley', 'York County', 'PA', '12477', 'United States', -5.00, 'single family');
omm=# INSERT 0 3

omm=# 

image.png

–创建索引

omm=# 
omm=# create index customer_idx on  customer(ca_address_sk);
CREATE INDEX
omm=# 

image.png

–通过hint强制使用索引,查看执行计划

omm=# 
omm=# EXPLAIN SELECT /*+ indexscan(customer customer_idx ) */ 
omm-#  * 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)

omm=# 

image.png

3.rename索引

omm=# 
omm=# ALTER INDEX idx_test_testnum RENAME TO idx_test_testnum_new;
ALTER INDEX
omm=# 

image.png

4.重建索引

–重建一个单独索引

omm=# 
omm=# ALTER INDEX  idx_test_testnum_new REBUILD;
REINDEX
omm=# REINDEX INDEX  idx_test_testnum_new;
REINDEX
omm=# 

image.png
–重建所有索引

omm=# 
omm=# reindex table test;
REINDEX
omm=# 

image.png

5.移动索引到其他表空间

–创建表空间myindex_ts:

omm=# 
omm=# CREATE TABLESPACE myindex_ts RELATIVE LOCATION 'tablespace/myindex_ts1';
CREATE TABLESPACE
omm=# 

image.png

–将索引idx_test_testnum_new移动到表空间myindex_ts:

omm=# 
omm=# ALTER INDEX idx_test_testnum_new SET TABLESPACE myindex_ts;
ALTER INDEX
omm=# 

image.png

–查看索引所在的表空间
select * from pg_indexes where tablename = ‘test’;
–或
select * from pg_indexes where indexname = ‘idx_test_testnum_new’;

omm=# 
omm=# 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 pg_default
 public     | test      | idx_test_testnum_new | myindex_ts | CREATE INDEX idx_test_testnum_new ON test USING btree (testnum) TABLESPACE myin
dex_ts
(2 rows)

omm=# select * from pg_indexes where indexname = 'idx_test_testnum_new';
 schemaname | tablename |      indexname       | tablespace |                                       indexdef                                 
       
------------+-----------+----------------------+------------+--------------------------------------------------------------------------------
-------
 public     | test      | idx_test_testnum_new | myindex_ts | CREATE INDEX idx_test_testnum_new ON test USING btree (testnum) TABLESPACE myin
dex_ts
(1 row)

omm=# 

image.png

5.删除索引

–执行下面的命令,删除表test上的索引idx_test_testnum_new:

omm=# 
omm=# drop index idx_test_testnum_new;
DROP INDEX
omm=# 

image.png

课程作业

1.创建表,在表中创建索引

2.通过hint使用索引

3.rename索引

4.重建索引

5.移动索引到其他表空间

6.删除索引

1.创建索引

–为表test的testnum列创建一个索引

root@modb:~# 

root@modb:~# su - omm
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 my888;
NOTICE:  table "my888" does not exist, skipping
DROP TABLE
omm=# create table my888(id serial primary key,my888num serial);
NOTICE:  CREATE TABLE will create implicit sequence "my888_id_seq" for serial column "my888.id"
NOTICE:  CREATE TABLE will create implicit sequence "my888_my888num_seq" for serial column "my888.my888num"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "my888_pkey" for table "my888"
CREATE TABLE
omm=# create index idx_my888_my888num on my888(my888num);
CREATE INDEX
omm=# \di
                        List of relations
 Schema |        Name        | Type  | Owner |  Table   | Storage 
--------+--------------------+-------+-------+----------+---------
 public | customer_idx       | index | omm   | customer | 
 public | idx_my888_my888num | index | omm   | my888    | 
 public | my888_pkey         | index | omm   | my888    | 
 public | test_pkey          | index | omm   | test     | 
(4 rows)

omm=# 

image.png

2.通过hint使用索引

omm=# 
omm=# CREATE TABLE my666
omm-# (
omm(# ca_address_sk integer NOT NULL ,
omm(# ca_address_id character(16),
omm(# ca_street_number character(10) ,
omm(# ca_street_name character varying(60) ,
omm(# ca_street_type character(15) ,
omm(# ca_suite_number character(10) ,
omm(# ca_city character varying(60) ,
omm(# ca_county character varying(30) ,
omm(# ca_state character(2) ,
omm(# ca_zip character(10) ,
omm(# ca_country character varying(20) ,
omm(# ca_gmt_offset numeric(5,2) ,
omm(# ca_location_type character(20)
omm(# );
CREATE TABLE
omm=# insert into customer values
omm-# (1, 'AAAAAAAABAAAAAAA', '18', 'Jackson', 'Parkway', 'Suite 280', 'Fairfield', 'Maricopa County', 'AZ', '86192' ,'United States', -7.00, 'condo'),
omm-# (2, 'AAAAAAAACAAAAAAA', '362', 'Washington 6th', 'RD', 'Suite 80', 'Fairview', 'Taos County', 'NM', '85709', 'United States', -7.00, 'condo'),
omm-# (3, 'AAAAAAAADAAAAAAA', '585', 'Dogwood Washington', 'Circle', 'Suite Q', 'Pleasant Valley', 'York County', 'PA', '12477', 'United States', -5.00, 'single family');
INSERT 0 3
omm=# create index my666_idx on  my666(ca_address_sk);
CREATE INDEX
omm=# EXPLAIN SELECT /*+ indexscan(my666 my666_idx ) */ 
omm-#  * FROM my666 WHERE ca_address_sk<100;
                                QUERY PLAN                                 
---------------------------------------------------------------------------
 [Bypass]
 Index Scan using my666_idx on my666  (cost=0.00..44.83 rows=33 width=788)
   Index Cond: (ca_address_sk < 100)
(3 rows)

omm=# 

image.png

3.rename索引

omm=# ALTER INDEX idx_my888_my888num RENAME TO idx_my888_my888num_new;
ALTER INDEX
omm=# 

image.png

4.重建索引

omm=# ALTER INDEX  idx_my888_my888num_new REBUILD;
REINDEX
omm=# REINDEX INDEX  idx_my888_my888num_new;
REINDEX
omm=# reindex table my888;
REINDEX
omm=# 

image.png

5.移动索引到其他表空间

omm=# 
omm=# CREATE TABLESPACE openindex_ts RELATIVE LOCATION 'tablespace/openindex_ts1';
CREATE TABLESPACE
omm=# ALTER INDEX idx_my888_my888num_new SET TABLESPACE openindex_ts;
ALTER INDEX
omm=# select * from pg_indexes where tablename = 'my888';
 schemaname | tablename |       indexname        |  tablespace  |                                          indexdef                                  
         
------------+-----------+------------------------+--------------+------------------------------------------------------------------------------------
---------
 public     | my888     | my888_pkey             |              | CREATE UNIQUE INDEX my888_pkey ON my888 USING btree (id) TABLESPACE pg_default
 public     | my888     | idx_my888_my888num_new | openindex_ts | CREATE INDEX idx_my888_my888num_new ON my888 USING btree (my888num) TABLESPACE open
index_ts
(2 rows)

omm=# select * from pg_indexes where indexname = 'idx_my888_my888num_new';
 schemaname | tablename |       indexname        |  tablespace  |                                          indexdef                                  
         
------------+-----------+------------------------+--------------+------------------------------------------------------------------------------------
---------
 public     | my888     | idx_my888_my888num_new | openindex_ts | CREATE INDEX idx_my888_my888num_new ON my888 USING btree (my888num) TABLESPACE open
index_ts
(1 row)

omm=# 

image.png

6.删除索引

–执行下面的命令,删除表test上的索引idx_my888_my888num_new:

omm=# drop index idx_my888_my888num_new;
DROP INDEX
omm=# 

image.png

最后修改时间:2022-12-10 16:48:45
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论