Indexes accelerate the data access speed but also add the processing time of the insert, update, and delete operations. Therefore, before creating an index, consider whether it is necessary and determine the columns where indexes will be created. You can determine whether to add an index for a table by analyzing the service processing and data use of applications, as well as columns that are frequently used as search criteria or need to be sorted.
Row-based tables support the following index types: btree (default), gin, and gist. Column-based tables support the following index types: Psort (default), btree, and gin.
Create a B-tree index for point queries.
Indexes are created based on columns in database tables. When creating indexes, you need to determine the columns, which can be:
GaussDB(DWS) supports four methods for creating indexes. For details, see Table 1.
Indexing Method |
Description |
---|---|
Unique index |
Refers to an index that constrains the uniqueness of an index attribute or an attribute group. If a table declares unique constraints or primary keys, GaussDB(DWS) automatically creates unique indexes (or composite indexes) for columns that form the primary keys or unique constraints. Currently, only B-tree can create a unique index in GaussDB(DWS). |
Composite index |
Refers to an index that can be defined for multiple attributes of a table. Currently, composite indexes can be created only for B-tree in GaussDB(DWS) and a maximum of 32 columns can share a composite index. |
Partial index |
Refers to an index that can be created for subsets of a table. This indexing method contains only tuples that meet condition expressions. |
Expression index |
Refers to an index that is built on a function or an expression calculated based on one or more attributes of a table. An expression index works only when the queried expression is the same as the created expression. |
1 | CREATE TABLE tpcds.customer_address_bak AS TABLE tpcds.customer_address; |
1 | SELECT ca_address_sk FROM tpcds.customer_address_bak WHERE ca_address_sk=14888; |
Generally, the database system needs to scan the tpcds.customer_address_bak table row by row to find all matched tuples. If the size of the tpcds.customer_address_bak table is large but only a few (possibly zero or one) of the WHERE conditions are met, the performance of this sequential scan is low. If the database system uses an index to maintain the ca_address_sk attribute, the database system only needs to search a few tree layers for the matched tuples. This greatly improves data query performance. Furthermore, indexes can improve the update and delete operation performance in the database.
Run the following command to create an index:
1 | CREATE INDEX index_wr_returned_date_sk ON tpcds.customer_address_bak (ca_address_sk); |
If a table declares a unique constraint or primary key, GaussDB(DWS) automatically creates a unique index (possibly a multi-column index) on the columns that form the primary key or unique constraint. If no unique constraint or primary key is specified during table creation, you can run the CREATE INDEX statement to create an index.
1 | CREATE UNIQUE INDEX unique_index ON tpcds.customer_address_bak(ca_address_sk); |
1 | SELECT ca_address_sk,ca_address_id FROM tpcds.customer_address_bak WHERE ca_address_sk = 5050 AND ca_street_number < 1000; |
1 | CREATE INDEX more_column_index ON tpcds.customer_address_bak(ca_address_sk ,ca_street_number ); |
If you only want to find records whose ca_address_sk is 5050, you can create a partial index to facilitate your query.
1 | CREATE INDEX part_index ON tpcds.customer_address_bak(ca_address_sk) WHERE ca_address_sk = 5050; |
1 | SELECT * FROM tpcds.customer_address_bak WHERE trunc(ca_street_number) < 1000; |
1 | CREATE INDEX para_index ON tpcds.customer_address_bak (trunc(ca_street_number)); |
1 | SELECT RELNAME FROM PG_CLASS WHERE RELKIND='i'; |
1 | \di+ index_wr_returned_date_sk |
1 | REINDEX INDEX index_wr_returned_date_sk; |
1 | REINDEX TABLE tpcds.customer_address_bak; |
1 | DROP INDEX index_wr_returned_date_sk; |