Junior — Middle
How can an index be added to a database to speed up data retrieval?
sobes.tech AI
Answer from AI
To speed up data retrieval in a database, you can add an index — a special data structure that allows quick lookup of records based on certain fields.
In SQL, this is done using the CREATE INDEX command. For example, if you have a users table with an email column, and you often search for users by email, you can create an index:
CREATE INDEX idx_users_email ON users(email);
Indexes can be:
- Simple — on a single column.
- Composite — on multiple columns.
Indexes speed up SELECT operations but may slow down insertions and updates, as the index needs to be updated.
In a Java application, indexes are usually created at the database level, and in the code, you simply use queries that utilize these indexes for quick searching.