What are the main types of database indexes, and how do they differ?

Instruction: List and explain different types of indexes that can be created in a database.

Context: This question aims to test the candidate's knowledge of indexing strategies and their applicability in various scenarios to optimize query performance.

Updated

Official answer

Index types and supported operators depend on the engine. PostgreSQL examples include:

  • B-tree for many equality, range and ordered-access cases.
  • Hash for equality lookups.
  • GIN for component membership, such as suitable array, JSONB or full-text operators.
  • GiST and SP-GiST for supported search strategies, including selected spatial/range cases; the operator class determines what works.
  • BRIN for compact block-range summaries, often useful when values correlate with physical row order in a large table.

A PostgreSQL bitmap scan is an execution strategy that can combine index results; it is not a separately stored bitmap index of the kind some other engines provide. Unique, composite, partial and expression indexes describe additional properties, not interchangeable physical algorithms.

Choose from the actual predicates and data distribution. Test selectivity, ordering, update rate and storage, and inspect whether the planner uses the structure. Indexes can speed suitable reads but cost space and write maintenance; more indexes do not guarantee a faster database.

Related Questions