Junior — Middle
Can you explain what data indexing in a database is and how it affects performance?
sobes.tech AI
Answer from AI
Indexing in a database is the creation of special data structures (indexes) that speed up data search and retrieval. An index is similar to a pointer or a table of contents in a book: instead of examining the entire table, the DBMS refers to the index to quickly find the needed records.
Advantages of indexing:
- Significantly speeds up SELECT operations with WHERE, JOIN conditions, and sorting.
- Helps efficiently perform searches by keys and other fields.
However, indexes occupy additional space and slow down insert, update, and delete operations, as the index also needs to be updated.
Example in SQL:
CREATE INDEX idx_user_email ON users(email);
This creates an index on the email field of the users table, which speeds up searching for users by email.