Junior — Middle
Could you explain what database indexes are and why they are needed?
sobes.tech AI
Answer from AI
Database indexes are special data structures that speed up the search and retrieval of data from tables. They work like pointers or tables of contents in a book: instead of examining the entire table, the DBMS refers to the index to quickly find the needed records.
Indexes are needed for:
- Accelerating SELECT operations with WHERE, JOIN, ORDER BY conditions.
- Ensuring the uniqueness of values (for example, unique keys).
However, indexes occupy additional space and slow down insert, update, and delete operations, as the index also needs to be updated.
Example of creating an index in SQL:
CREATE INDEX idx_user_email ON users(email);