9951 explained code solutions for 126 technologies


postgresqlHow do I create and use different types of indexes in PostgreSQL?


PostgreSQL supports a wide variety of index types, which can be used to improve the performance of queries. Here are the steps to create and use different types of indexes in PostgreSQL:

  1. Create a table with the desired columns.
CREATE TABLE student (
    id SERIAL PRIMARY KEY,
    name VARCHAR(100),
    age INT
);
  1. Create an index on the desired column.
CREATE INDEX student_name_idx ON student (name);
  1. To use the index, include the column in the WHERE clause of a query.
SELECT * FROM student WHERE name = 'John';

PostgreSQL supports different types of indexes, such as B-Tree, Hash, GiST, SP-GiST, and GIN. Each index type has its own advantages and disadvantages depending on the data and the query.

For more information, see the official PostgreSQL documentation:

Edit this code on GitHub