GaussDB Cloud Database SQL Application Series—Index Management

Table of contents

I. Introduction

2. Matters needing attention

3. Index creation

1. Create a common index

2. Create a unique index

3. Create a multi-field index

4. Create a partial index

5. Create an expression index

4. Index Management

1. View index information

2. Delete the index

Summarize

I. Introduction

With the rapid development of the Internet, the amount of data has exploded. How to efficiently manage and query these data has become an important issue facing enterprises. As one of the key means of database optimization, database index plays an important role in improving database performance and query efficiency. This article will introduce the index creation and management methods of the GaussDB cloud database, and analyze it in combination with actual application scenarios.

2. Matters needing attention

Indexes are built on columns in database tables. Therefore, the following creation factors need to be paid attention to:

  • Created on columns that need to be queried frequently , it can speed up the search .
  • Create an index on a column that acts as a primary key, enforcing the uniqueness of that column and organizing the arrangement of data in the table.
  • Create indexes on frequently used columns to speed up joins.
  • Create indexes on columns that often need to be searched by range, because the index is sorted and its specified range is contiguous.
  • Create indexes on columns that often need to be sorted, because the indexes are already sorted, and the sorting of the indexes can be used to speed up the sorting query when querying.
  • Build indexes for fields that often appear after the keywords ORDER BY, GROUP BY, and DISTINCT.

illustrate:

  • After the index is created, the system will actively judge when to reference the index. Indexes are used when the system believes that using an index is faster than a sequential scan.
  • After the index is created, it will be synchronized with the table to ensure that new data can be found accurately, which increases the load of data operations. Therefore, useless indexes need to be cleaned up regularly.

3. Index creation

Create a test table

--销售信息全量表(sell_info_full),
CREATE TABLE sell_info_full
(
 sell_id int PRIMARY KEY
,sell_date date not null
,goods_id char(20) not null
,goods_name char(20) not null
,goods_number int not null
,sell_goods_amount int not null
,etl_date date not null
);

1. Create a common index

Grammar format:

CREATE INDEX index_name ON table_name (column_name);
--例如,用户经常根据销售的商品编号(good_id)进行查询,那么可在sell_info_full表中创建一个名为idx_good_id的普通索引:
CREATE INDEX idx_good_id ON sell_info_full (goods_id);

2. Create a unique index

Grammar format:

CREATE UNIQUE INDEX unique_index_name ON table_name(column_name);
--例如,在sell_info_full表中创建一个名为idx_sell_id的唯一索引:
CREATE UNIQUE INDEX idx_sell_id ON sell_info_ful(sell_id);

3. Create a multi-field index

Grammar format:

CREATE INDEX more_column_index ON table_name(column_name1, column_name2);
--例如,用户经常根据销售的商品数量和商品金额范围进行查询访问,则可在sell_info_full表中创建一个多字段索引。
CREATE INDEX more_column_index ON sell_info_full(goods_number,sell_goods_amount);

4. Create a partial index

--如果经常需要查询“某个商品”,例如,goods_name=’商品名称’(替换成具体的名称),可以创建部分索引来提升查询效率。
CREATE INDEX part_index ON sell_info_full(goods_name) WHERE goods_name='商品名称';

5. Create an expression index

--假如经常需要查询商品加个大于100元的商品销售信息,查询SQL如下(引用一个函数trunc):
SELECT * FROM sell_info_full WHERE trunc(sell_goods_amount)>100;

--创建表达式索引
CREATE INDEX fun_index ON sell_info_full(trunc(sell_goods_amount));

4. Index Management

1. View index information

Method 1: Obtain all indexes defined by the system and users through the system table PG_CLASS, field relkind='i' 

--获取系统和用户定义的所有索引
SELECT RELNAME FROM PG_CLASS WHERE RELKIND='i';

Method 2: Obtain through the system view PG_INDEXES

--例如,查看sell_info_full表中的索引信息:
SELECT * FROM PG_INDEXES  WHERE TABLENAME = 'sell_info_full' AND INDEXNAME IS NOT NULL;

 

2. Delete the index

Grammar format:

DROP INDEX [ IF EXISTS ] 
    index_name [, ...] [ CASCADE | RESTRICT ];
--例如,删除sell_info_full表中的idx_user_name索引:
DROP INDEX idx_good_id;

 

Summarize

This article briefly introduces the index creation, query, deletion and other operations of the GaussDB cloud database with a test table.

Indexes can improve data access speed, but also increase the processing time of insert, update and delete operations. Therefore, creating an index is determined by analyzing the application's business processing, data usage, frequently used as query conditions, or fields that are required to be sorted.

By creating and maintaining indexes reasonably, you can improve database performance and query efficiency, and provide powerful support for enterprise data management and applications.

--Finish

Guess you like

Origin blog.csdn.net/GaussDB/article/details/131181194