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 UNIQUEINDEX : idx_u_table_name_column_name
Syntax
createindexindex_nameon table_name (col1,col2,.....)-- create index for only unique values in columncreateunique indexindex_nameon table_name (col1,col2,.....)-- more descriptive methodcreateindexindex_nameon table_name [USING method]( column_name [ASC|DESC] [NULLS {FIRST | LAST}], ...);
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.
vacuum analyze table_name;
Rebuilding Indexes
REINDEX ( VERBOSE ) INDEX concurrently idx_orders_ship_city;