PostgreSQL Indexes

  • Indexes are a common way to enhance database performance. An index allows the database server to find and retrieve specific rows much faster than it could do without an index. But indexes also add overhead to the database system as a whole, so they should be used sensibly.
  • An index defined on a column that is part of a join condition can also significantly speed up queries with joins.
  • Creating an index on a large table can take a long time.

Syntax:

CREATE [ UNIQUE ] INDEX [ CONCURRENTLY ] [ name ] ON table_name [ USING method ]
( { column_name | ( expression ) } [ COLLATE collation ] [ opclass ] [ ASC | DESC ] [ NULLS [ WITH ( storage_parameter = value [, … ] ) ]
[ TABLESPACE tablespace_name ]
[ WHERE predicate ]

Create Index on a table:

Create a table:

k2info=# create table emp(empno int, empname varchar(50), dept int);
CREATE TABLE

Create index on column of emp table:

k2info=# create index empno_index on emp(empno);
CREATE INDEX

Drop an Index
DROP INDEX drops an existing index from the database system. To execute this command we must be the owner of the index.

Example:

k2info=# drop index empno_index;
DROP INDEX

Powered by k2schools