Skip to main content
Documents
Basic SyntaxMath FunctionsDate FunctionsJSON FunctionsDatabasesTables & Schema ManagementString FunctionsTriggersIndexes

PostgreSQL CREATE INDEX

Indexes in PostgreSQL are database objects that improve the speed of data retrieval operations. They serve as pointers to quickly locate rows in a table, enhancing the performance of queries.

Usage

Indexes are used to optimize the performance of SELECT queries and WHERE clauses. They are especially useful for large tables where quick data access is required.

CREATE INDEX index_name
ON table_name (column1, column2, ...);

In this syntax, `CREATE INDEX` defines a new index named `index_name` on the specified `table_name` and column(s).

Examples

1. Basic Index Creation

CREATE INDEX idx_customer_name
ON customers (customer_name);

This example creates an index on the `customer_name` column of the `customers` table, which speeds up searches involving this column.

2. Composite Index

CREATE INDEX idx_order_customer
ON orders (customer_id, order_date);

This composite index on the `orders` table uses both `customer_id` and `order_date` columns, enhancing the performance of queries filtering by these columns. Composite indexes are beneficial when queries frequently involve multiple columns in their WHERE clauses.

3. Unique Index

CREATE UNIQUE INDEX idx_unique_email
ON users (email);

A unique index ensures that all values in the indexed column `email` are distinct across the `users` table, preventing duplicate entries and optimizing unique searches.

4. Partial Index

CREATE INDEX idx_active_users
ON users (last_login_date)
WHERE active = true;

A partial index is created on the `last_login_date` column for only those rows where `active` is true, optimizing queries that target this subset of data.

5. Full-text Index

CREATE INDEX idx_document_content
ON documents USING gin(to_tsvector('english', content));

This example creates a full-text index on the `content` column of the `documents` table, improving the performance of full-text search queries.

Impact of Indexing

Indexes can significantly improve read query performance but can also impact the performance of write operations such as `INSERT`, `UPDATE`, and `DELETE`. The trade-off involves balancing faster data retrieval with potentially slower data modification.

Tips and Best Practices

  • Index Selectively. Only index columns that are frequently used in query conditions to avoid unnecessary overhead.
  • Monitor and Maintain. Regularly analyze and maintain indexes to ensure they are being utilized effectively.
  • Beware of Over-indexing. Excessive indexing can slow down data modifications like `INSERT`, `UPDATE`, and `DELETE`.
  • Use Partial Indexes. Consider partial indexes for columns with a high number of NULLs or a specific subset of data.
  • Leverage Unique Indexes. Use unique indexes to enforce data integrity and enhance query performance.
  • Advanced Features. Explore indexing options like expression indexes for calculated values or covering indexes to include additional columns in the index.
  • Use REINDEX. Periodically use the `REINDEX` command to rebuild corrupted indexes or improve performance after significant data changes.