Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PostgreSQL Indexes

PostgreSQL Indexes

An index is a special table lookup that speeds up data retrieval by a search engine. Simply put, an index is a pointer to data in a table. An index in a database is very similar to the index of a book.

Take the table of contents (index) page of a Chinese dictionary as an example: we can quickly find the needed character through an index (directory) sorted by pinyin, stroke count, radical, and so on.

Indexes help speed up SELECT queries and WHERE clauses, but they slow down data entry when using UPDATE and INSERT statements. Indexes can be created or deleted without affecting the data.

Use the CREATE INDEX statement to create an index. It allows you to name the index, specify the table and one or more columns to be indexed, and indicate whether the index is sorted in ascending or descending order.

An index can also be unique, similar to a UNIQUE constraint, preventing duplicate entries on a column or a combination of columns.

The CREATE INDEX Command

The syntax of CREATE INDEX (create index) is as follows:

CREATE INDEX index_name ON table_name;

Index Types

Single-Column Indexes

A single-column index is an index created on only one column of a table; the basic syntax is as follows:

CREATE INDEX index_name

ON table_name (column_name);

Composite Indexes

A composite index is an index created on multiple columns of a table; the basic syntax is as follows:

CREATE INDEX index_name

ON table_name (column1_name, column2_name);

Whether it is a single-column index or a composite index, the index must be on columns that are used very frequently in the filter conditions of the WHERE clause.

If only one column is used, choose a single-column index; if there are multiple columns, use a composite index.

Unique Indexes

A unique index is used not only for performance but also for data integrity. A unique index does not allow any duplicate values to be inserted into the table. The basic syntax is as follows:

CREATE UNIQUE INDEX index_name

on table_name (column_name);

Partial Indexes

A partial index is an index built on a subset of a table; the subset is defined by a conditional expression. The index contains only the rows that satisfy the condition. The basic syntax is as follows:

CREATE INDEX index_name

ON table_name(column_list)

WHERE condition;

Here, index_name is the name of the index you want to create, table_name is the name of the table containing the columns you want to index, column_list is the list of columns you want to index, and condition is a Boolean expression that defines which rows will be included in the index.

Implicit Indexes

In PostgreSQL, an implicit index is an index automatically created by the database server when an object is created. Such indexes are usually created automatically for primary key constraints and unique constraints. When a column is declared as a primary key, a unique constraint, or a foreign key at table creation time, PostgreSQL automatically creates an implicit index for that column. This simplifies index management and improves database performance.

For example, if a column named "userid" is declared as the primary key when creating a table named "users", PostgreSQL automatically creates an implicit index on the "userid" column, which means that when new records are inserted, the database automatically generates a unique index value for the "userid" column.

The creation and management of implicit indexes is done automatically by PostgreSQL and requires no manual intervention by the user, making database management simpler and more efficient.

Example

The following example creates an index on the SALARY column of the COMPANY table:

# CREATE INDEX salary_index ON COMPANY (salary);

Now, use the \d company command to list all indexes on the COMPANY table:

# \d company

The result obtained is as follows; company_pkey is an implicit index created when the table was created:

runoobdb=# \d company

                  Table "public.company"

 Column  |     Type      | Collation | Nullable | Default 

---------+---------------+-----------+----------+---------

 id      | integer       |           | not null | 

 name    | text          |           | not null | 

 age     | integer       |           | not null | 

 address | character(50) |           |          | 

 salary  | real          |           |          | 

Indexes:

    "company_pkey" PRIMARY KEY, btree (id)

    "salary_index" btree (salary)

You can use the \di command to list all indexes in the database:

runoobdb=# \di

                    List of relations

 Schema |      Name       | Type  |  Owner   |   Table    

--------+-----------------+-------+----------+------------

 public | company_pkey    | index | postgres | company

 public | department_pkey | index | postgres | department

 public | salary_index    | index | postgres | company

(3 rows)

DROP INDEX (Drop an Index)

An index can be dropped using the DROP command in PostgreSQL.

DROP INDEX index_name;

You can use the following statement to drop the previously created index:

# DROP INDEX salary_index;

After dropping, you can see that salary_index has been removed from the list of indexes:

runoobdb=# \di

                    List of relations

 Schema |      Name       | Type  |  Owner   |   Table    

--------+-----------------+-------+----------+------------

 public | company_pkey    | index | postgres | company

 public | department_pkey | index | postgres | department

(2 rows)

When should indexes be avoided?

Although the purpose of indexes is to improve database performance, there are several situations where indexes should be avoided.

When using indexes, the following guidelines should be considered: