Indexing

What is a Index in Database

  • An index help improve the access of data in our database

  • Indexed tuple point to the table page where the tuple is stored on disk.

  • An Index is a data structure that allows faster access to the underlying table so that specific tuples can be found quickly . Here "quickly" means much faster than scanning the entire table and analysing every single tuple.

  • They add a cost to running a query, consume more memory to maintain the data structure.

INDEX        : idx_table_name_column_name 
UNIQUE INDEX : idx_u_table_name_column_name

Syntax

create index index_name on table_name (col1,col2,.....)

-- create index for only unique values in column
create unique index index_name on table_name (col1,col2,.....)

-- more descriptive method
create index index_name on table_name [USING method]
(
    column_name [ASC|DESC] [NULLS {FIRST | LAST}],
    ...
);

Get All Indexes

Size of Indexes

Stats about indexes

Drop Index

  • CASCADE: If object has dependent objects, you will also drop the dependent ones after dropping it.

  • RESTRICT : It denies the user to drop the index if a dependency exists

  • CONCURRENTLY : PostgreSQL will require exclusive lock over the whole table and block access until index is removed

Vacuum analyze

When a vacuum process runs, the space occupied by these dead tuples is marked reusable by other tuples.

An “analyze” operation does what its name says – it analyzes the contents of a database's tables and collects statistics about the distribution of values in each column of every table.

Rebuilding Indexes

Last updated

Was this helpful?