Sobes.tech
Junior

What is an index in a relational database?

sobes.tech AI

Answer from AI

An index in a relational database is a data structure that improves the speed of data retrieval operations (SELECT), but slows down data modification operations (INSERT, UPDATE, DELETE).

It works similarly to a subject pointer in a book: it allows the DBMS to quickly find the needed rows without scanning the entire table.

Main types of indexes:

  • Primary Key: A unique index automatically created for columns designated as the primary key.
  • Unique: Ensures the uniqueness of values in indexed columns.
  • Non-unique: Does not guarantee the uniqueness of values. Used to improve search performance.
  • Full-text: Used for searching text data.

Example of creating an index in SQL:

CREATE INDEX idx_lastname ON Employees (LastName);

When creating an index, consider:

  • Having an index slows down write and data modification operations. Every time data in an indexed column is changed, the index must be updated.
  • An index consumes disk space.
  • Not all columns are beneficial to index. Indexes are effective for columns frequently used in WHERE, JOIN, and ORDER BY conditions.

Choosing columns for indexing depends on query specifics and data structure.