Middle+
What are indexes in a database, their advantages and disadvantages?
sobes.tech AI
Answer from AI
Database indexes are special data structures that speed up the search and retrieval of records based on certain table columns. They are similar to pointers or a table of contents in a book, allowing quick access to needed information without scanning all rows.
Advantages of indexes:
- Significantly accelerate SELECT operations with conditions on indexed columns.
- Improve the performance of JOINs and ORDER BY sorting on indexed columns.
- Help maintain data uniqueness (unique indexes).
Disadvantages of indexes:
- Take up additional disk space.
- Slow down INSERT, UPDATE, and DELETE operations because index structures need to be updated.
- Excessive indexes can lead to overall performance degradation.
Example of creating an index in SQL:
CREATE INDEX idx_user_email ON users(email);
This index will speed up searching for users by the email field.